首先奉上ButterKnife的“圖標(biāo)”。

ButterKnife.png
官方解釋,很好懂的就不翻譯了。
Field and method binding for Android views which uses annotation processing to generate boilerplate code for you.
- Eliminate findViewById calls by using @BindView on fields.
- Group multiple views in a list or array. Operate on all of them at once with actions, setters, or properties.
- Eliminate anonymous inner-classes for listeners by annotating methods with @OnClick and others.
- Eliminate resource lookups by using resource annotations on fields.
本文基于ButterKnife8.4.0寫(xiě)的,如果和其他版本有沖突也是正常的
在項(xiàng)目中添加依賴
compile 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
使用的時(shí)候應(yīng)該在activity的onCreate方法中執(zhí)行ButterKnife.bind(this); 或者在Fragment的onCreateView中執(zhí)行ButterKnife.bind(this, view);如果在Adapter中使用稍后介紹。
好了,配置完成后就開(kāi)始介紹使用方法了:
- Bind View
@BindView(R.id.tv)
TextView mTv;
@BindView(R.id.btn)
Button mBtn;
- Bind Resource
@BindString(R.String.name)
String mName;
@BindDrawable(R.Drawable.icon)
Drawable mIcon;
@BindColor(R.color.red)
int mRed;
@BindDimen(R.dimen.space)
Float mSpace;
- Bind Adapter
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");
// 其他操作
return view;
}
static class ViewHolder {
@BindView(R.id.title) TextView name;
@BindView(R.id.job_title) TextView jobTitle;
public ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}
- Bind Views
@BindViews({ R.id.name1, R.id.name2, R.id.name3 })
List<EditText> mNameViews;
// 可以對(duì)其進(jìn)行其他設(shè)置,比如:
ButterKnife.apply(mNameViews, DISABLE);
ButterKnife.apply(mNameViews, View.ALPHA, 0.0f);
// 或者
@OnClick({R.id.tv,R.id.tv2})
public void viewClick(TextView tv){ // 如果不需要區(qū)分是哪個(gè)控件被點(diǎn)擊了,里邊的參數(shù)可以不要
switch (tv.getId()){
case R.id.tv:
mTextView.setText(++i+"");
break;
case R.id.tv2:
mTextView2.setText(++i+"");
break;
}
}
-
Bind ListView
這個(gè)名字起的不少,應(yīng)該說(shuō)是綁定容器類list吧
@OnItemSelected(R.id.mListView)
void onItemSelected(int position) {
// 其他操作
}
- Bind OnLongClick
@OnLongClick( R.id.mButton )
public boolean showToast2(){ // 里邊可以寫(xiě)上Button btn,如果需要的話
Toast.makeText(this, "long click", Toast.LENGTH_SHORT).show();
return true ;
}
- Bind OnTextChanged
@OnTextChanged(value = R.id.mEditText, callback = OnTextChanged.Callback.BEFORE_TEXT_CHANGED)
void beforeTextChanged(CharSequence s, int start, int count, int after) {
// 操作
}
@OnTextChanged(value = R.id.mEditText, callback = OnTextChanged.Callback.TEXT_CHANGED)
void onTextChanged(CharSequence s, int start, int before, int count) {
// 操作
}
@OnTextChanged(value = R.id.mEditText, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
void afterTextChanged(Editable s) {
// 操作
}
- Optional Bind
@Nullable @BindView(R.id.might_not_be_there) TextView mightNotBeThere;
@Optional @OnClick(R.id.maybe_missing) void onMaybeMissingClicked() {
// 操作
}
-
解除綁定
在Activity的onDestroy中unregister,如果是Fragment則是在onDestroyView中unbind。
// 這個(gè)是Activity的
@Override
protected void onDestroy() {
EventBus.getDefault().unregister(this);
super.onDestroy();
}
// 下邊是Fragment的
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
- 關(guān)于混淆
-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewBinder { *; }
-keepclasseswithmembernames class * {
@butterknife.* <fields>;
}
-keepclasseswithmembernames class * {
@butterknife.* <methods>;
}
-
待續(xù)
上邊只是一些平時(shí)用到的總結(jié),后期用到其他的方法再繼續(xù)添加,看官們?nèi)绻玫搅松鲜鰶](méi)有的方法或值得記錄的可以留言告訴我,歡迎交流。
最后奉上ButterKnife的github地址: https://github.com/JakeWharton/butterknife