[轉]Android Libraries 介紹 - Butter knife
Butter Knife 簡介

Butter Knife - Field and method binding for Android views。助你簡化程式碼,方便閱讀。
使用方法
開發 andriod app 的時候,一定有寫過類似的 code:
class ExampleActivity extends Activity {
TextView title;
TextView subtitle;
TextView footer;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);
title = (TextView)findViewById(R.id.title);
subtitle = (TextView)findViewById(R.id.subtitle);
footer = (TextView)findViewById(R.id.footer);
//other codes
}}
當一個 activity 有很多個 view 的話,便會發覺這些 findViewById() 的 code 變得很長很煩很長很煩很長很煩。
用了 Butter Kinfe ,便可以簡化成
class ExampleActivity extends Activity {
@Bind(R.id.title) TextView title;
@Bind(R.id.subtitle) TextView subtitle;
@Bind(R.id.footer) TextView footer;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);
ButterKnife.bind(this);
// other codes
}}
用 @Bind 加上 ButterKnife.bind() 自動指定對應的 view。是不是簡潔了很多?
Fragment 也可使用:
public class FancyFragment extends Fragment {
@Bind(R.id.button1) Button button1;
@Bind(R.id.button2) Button button2;
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fancy_fragment, container, false);
ButterKnife.bind(this, view);
// TODO Use fields...
return view;
}
}
Adapter 中的 ViewHolder 也沒問題:
public class MyAdapter extends BaseAdapter {
@Override public View getView(int position, View view, ViewGroup parent) {
ViewHolder holder;
if (view != null) {
holder = (ViewHolder) view.getTag();
} else {
view = inflater.inflate(R.layout.whatever, parent, false);
holder = new ViewHolder(view);
view.setTag(holder);
}
holder.name.setText("John Doe");
// etc...
return view;
}
static class ViewHolder {
@Bind(R.id.title) TextView name;
@Bind(R.id.job_title) TextView jobTitle;
public ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}
另外可以將幾個 TextView 放在 List 中:
@Bind({ R.id.first_name, R.id.middle_name, R.id.last_name })
List<EditText> nameViews;
也可以用來取代 OnClickListener:
@OnClick(R.id.submit)
public void submit(View view) {
// TODO submit data to server...
}
幾個 Button 當然可以用同一 method 啦
@OnClick({ R.id.door1, R.id.door2, R.id.door3 })
public void submit(View view) {
// TODO submit data to server...
}
安裝方法
到官網直接下載 jar 或者用 gradle
compile 'com.jakewharton:butterknife:7.0.1'
lintOptions {
disable 'InvalidPackage'
}
用 IntelliJ IDEA 的朋友可能要到 Setting > Annotation Processors 設定使用 Butter Knife。
如有使用 proguard,記得要加
-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewBinder { *; }
-keepclasseswithmembernames class * {
@butterknife.* <fields>;
}
-keepclasseswithmembernames class * {
@butterknife.* <methods>;
}
好處
令 code 精簡,這一點已經足夠了。阿們。
總結
Butter Knife 看似作用不大,實際上不使用它也不會有大影響。但是一用上它便會愛上它的簡潔和方便,現在每個 project 也有使用。它是屬於那種「不用不會有問題,但一用便令人上癮」的類別的 libraries。
連結
[轉]Android Libraries 介紹 - Butter knife的更多相关文章
- Android:Butter Knife 8.0.1配置
github地址:https://github.com/GarsonZhang/butterknife Butter Knife Field and method binding for Androi ...
- android注解[Jake Wharton Butter Knife]
Introduction Annotate fields with @InjectView and a view ID for Butter Knife to find and automatical ...
- Android RoboGuice开源框架、Butter Knife开源框架浅析
Google Guice on Android(RoboGuice) 今天介绍一下Google的这个开源框架RoboGuice, 它的作用跟之前讲过的Dagger框架差点儿是一样的,仅仅是Dagger ...
- android Butter Knife 使用详解
Butter Knife github连接:https://github.com/JakeWharton/butterknife 本文使用的butterknife版本7.0.1 butterknife ...
- Visual Studio 跨平台開發實戰(4) - Xamarin Android 基本控制項介紹 (转帖)
前言 不同於iOS, Xamarin 在Visual Studio中針對Android, 可以直接設計使用者介面. 在本篇教學文章中, 筆者會針對Android的專案目錄結構以及基本控制項進行介紹, ...
- Android Studio & Butter Knife —— 快速开发
Butter Knife是一个Android的注解框架,可以帮助用户快速完成视图.资源与对象的绑定,完成事件的监听.(也就是少写findViewById()) 具体的介绍可以参考官方主页: http: ...
- Linux Kernel 排程機制介紹
http://loda.hala01.com/2011/12/linux-kernel-%E6%8E%92%E7%A8%8B%E6%A9%9F%E5%88%B6%E4%BB%8B%E7%B4%B9/ ...
- QR Code於台灣各行業的行銷應用案例介紹
當走在東京的大街小巷時,在五花八門的廣告看板.雜誌.護照簽證.海關.宣傳品.廣告.旅遊和導覽手冊.產品包裝.甚至在餐廳菜單上,皆可看到上面有一組黑色神秘二維條碼圖案:QR Code,當看到有興趣的商品 ...
- <Android开源库 ~ 1> GitHub Android Libraries Top 100 简介
转载自GitHub Android Libraries Top 100 简介 本项目主要对目前 GitHub 上排名前 100 的 Android 开源库进行简单的介绍, 至于排名完全是根据 GitH ...
随机推荐
- U-Boot
U-Boot U-Boot,全称 Universal Boot Loader,是遵循GPL条款的开放源码项目.从FADSROM.8xxROM.PPCBOOT逐步发展演化而来.其源码目录.编译形式与Li ...
- JVM实用参数——新生代垃圾回收
JVM实用参数目录 JVM实用参数——新生代垃圾回收 概述 第1部分 新生代垃圾回收介绍 第2部分 参数介绍 参考 第1部分 新生代垃圾回收介绍 本部分,我们将关注堆(heap) 中一个主要区域, ...
- 20169212《Linux内核原理与分析》第二周作业
<Linux内核原理与分析>第二周作业 这一周学习了MOOCLinux内核分析的第一讲,计算机是如何工作的?由于本科对相关知识的不熟悉,所以感觉有的知识理解起来了有一定的难度,不过多查查资 ...
- mysql索引的使用[下]
接着上篇,我们继续来探究索引.这次我们主要来探究关于联合索引的使用和联合.多表查询的规范. 继续看一下数据: mysql> select * from student order by ID d ...
- Android doc打开太慢
C:\Windows\System32\drivers\etc\HOSTS 127.0.0.1 fonts.googleapis.com 127.0.0.1 www.google.com 127.0. ...
- PHP 时间 date,strtotime ,time计算1970开始的第几天
首先,需要看你的php时区配置参数 方式1:更改php配置文件,然后从其fast-cgi或者php调用的地方: 方式2:date_default_timezone_set('PRC'); date函数 ...
- JavaScript整合
JavaScript已经学完了,总体感觉良好,但是突然发现原来JS可以做的东西比我想象的还要多!我整理了一些JavaScript的基础知识,这些内容掌握好的话,对我们深入学习JavaScript会有很 ...
- 尾递归(Tail Recursion)和Continuation
递归: 就是函数调用自己. func() { foo(); func(); bar(); } 尾调用:就是在函数的最后,调用函数(包括自己). foo(){ return bar(); } 尾递归:就 ...
- PowerShell脚本:随机密码生成器
脚本名称:s随机密码生成器_v2.63.ps1脚本作用:产生随机密码.每密码字符个数,密码数量,存盘位置等可以自定义.脚本用法:脚本采用了硬编码,所以你需要打开脚本,修改如下变量:$生成密码总个数 = ...
- 苹果5S指纹扫描识别传感器Touch ID有利于iPhone的安全性
iPhone5S新增的指纹扫描识别传感器 Touch ID,黑客花了大量的时间表明指纹验证是可以被破解的.即使它可能被黑客攻击,对iPhone5S的安全性而言,仍然具有极大的好处. 为什么一个容易被破 ...