前言
本文中大部分示例都是Butter Knife官网直接拿来的,本文就是官方翻译版加了很少量的说明。可以直接去官网看教程 。
官网及文档:http://jakewharton.github.io/butterknife/
GitHub地址:https://github.com/JakeWharton/butterknife </span>
加入 android studio 项目中 (目前我使用的版本是7.0.1)
compile 'com.jakewharton:butterknife:(insert latest version)'
另外还需要在build.gradle 配置两个地方
//支持lint warning 检查机制
lintOptions {
disable 'InvalidPackage'
}
//为什么加入这个呢?防止冲突,比如我同时用了dagger-compiler就会报错,说下面这个`Processor`重复了
packagingOptions {
exclude 'META-INF/services/javax.annotation.processing.Processor'
}
顺便在Proguard 中配置一下混淆文件:
-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewBinder { *; }
-keepclasseswithmembernames class * {
@butterknife.* <fields>;
}
-keepclasseswithmembernames class * {
@butterknife.* <methods>;
}</span>
用法:
(1)简单绑定使用
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);
// TODO Use fields...
}
}
注:
ButterKnife可不是通过反射实现了,而是在编译的时候生成的代码,和我们自己写的其实原理一样,比如上面的Button的绑定的实现就类似下面的方法:
public void bind(ExampleActivity activity) {
activity.subtitle = (android.widget.TextView) activity.findViewById(2130968578);
activity.footer = (android.widget.TextView) activity.findViewById(2130968579);
activity.title = (android.widget.TextView) activity.findViewById(2130968577);
}
(2)绑定资源使用
class ExampleActivity extends Activity {
@BindString(R.string.title) String title;
@BindDrawable(R.drawable.graphic) Drawable graphic;
@BindColor(R.color.red) int red; // int or ColorStateList field
@BindDimen(R.dimen.spacer) Float spacer; // int (for pixel size) or float (for exact value) field
// ...
}
(3) 非Activity的绑定
比如说Fragment
中(得到View
,然后bind
方法传入这个View
的实例):
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;
}
}
(4) 在Adapter的绑定
经常会在Adapter中使用ViewHolder,其实你也可以在ViewHolder中使用ButterKnife:
在ViewHolder的构造函数中调用bind
,然后成员变量同样的使用@Bind
注解,原理都是差不多的 。
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("Hello,android");
// 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);
}
}
}
(5)View List 批量操作
如果我们有一系列的View放到了一个List里面,就可以进行批量操作了:批量绑定,批量设置属性等。
<span style="font-size:18px;">@Bind({ R.id.first_name, R.id.middle_name, R.id.last_name })
List<EditText> nameViews;</span>
<span style="font-size:18px;">//批量设置
ButterKnife.apply(nameViews, DISABLE);
ButterKnife.apply(nameViews, ENABLED, false);</span>
其中,
DIABLE
,
ENABLED
是定义的两个对象,一个是Action,负责执行操作,一个是Setter,负责将值设置为第三个参数:
<span style="font-size:18px;">tatic final ButterKnife.Action<View> DISABLE = new ButterKnife.Action<View>() {
@Override public void apply(View view, int index) {
view.setEnabled(false);
}
};
static final ButterKnife.Setter<View, Boolean> ENABLED = new ButterKnife.Setter<View, Boolean>() {
@Override public void set(View view, Boolean value, int index) {
view.setEnabled(value);
}
};</span>
An Android Property
can also be used with the apply
method.
<span style="font-size:18px;">ButterKnife.apply(nameViews, View.ALPHA, 0.0f);</span>
@OnClick(R.id.submit)
public void submit(View view) {
// TODO submit data to server...
}
@OnClick(R.id.submit)
public void submit() {
// TODO submit data to server...
}
@OnClick(R.id.submit)
public void sayHi(Button button) {
button.setText("Hello!");
}
<span style="font-size:18px;">@OnClick({ R.id.door1, R.id.door2, R.id.door3 })
public void pickDoor(DoorView door) {
if (door.hasPrizeBehind()) {
Toast.makeText(this, "You win!", LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Try again", LENGTH_SHORT).show();
}
}</span>
如果是自定义视图 ,可以通过不指定一个ID 绑定到其自己的侦听器。
<span style="font-size:18px;">public class FancyButton extends Button {
@OnClick
public void onClick() {
// TODO do something!
}
}</span>
(7)绑定重置
如果我们需要取消绑定的View,ButterKnife提供了一个unbind
方法给我们使用。
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;
}
@Override public void onDestroyView() {
super.onDestroyView();
ButterKnife.unbind(this);
}
}
还可以通过
findById
函数来简化查找View方法。
View view = LayoutInflater.from(context).inflate(R.layout.thing, null);
TextView firstName = ButterKnife.findById(view, R.id.first_name);
TextView lastName = ButterKnife.findById(view, R.id.last_name);
ImageView photo = ButterKnife.findById(view, R.id.photo);