Android: 這可能是最適合新手使用ButterKnife的文章了

首先奉上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)始介紹使用方法了:

  1. Bind View
@BindView(R.id.tv)
TextView mTv;
@BindView(R.id.btn)
Button mBtn;
  1. 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;
  1. 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);
        }
    }
}
  1. 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;
    }
}
  1. Bind ListView
    這個(gè)名字起的不少,應(yīng)該說(shuō)是綁定容器類list吧
@OnItemSelected(R.id.mListView)
void onItemSelected(int position) {
  // 其他操作
}
  1. Bind OnLongClick
@OnLongClick( R.id.mButton )
    public boolean showToast2(){ // 里邊可以寫(xiě)上Button btn,如果需要的話
        Toast.makeText(this, "long click", Toast.LENGTH_SHORT).show();
        return true  ;
    }
  1. 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) {  
   // 操作
}
  1. Optional Bind
@Nullable @BindView(R.id.might_not_be_there) TextView mightNotBeThere;
@Optional @OnClick(R.id.maybe_missing) void onMaybeMissingClicked() {
  // 操作
}
  1. 解除綁定
    在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();
}
  1. 關(guān)于混淆
-keep class butterknife.** { *; }  
-dontwarn butterknife.internal.**  
-keep class **$$ViewBinder { *; }  
-keepclasseswithmembernames class * {  
   @butterknife.* <fields>;  
}  
-keepclasseswithmembernames class * {  
   @butterknife.* <methods>;  
} 
  1. 待續(xù)
    上邊只是一些平時(shí)用到的總結(jié),后期用到其他的方法再繼續(xù)添加,看官們?nèi)绻玫搅松鲜鰶](méi)有的方法或值得記錄的可以留言告訴我,歡迎交流。

最后奉上ButterKnife的github地址: https://github.com/JakeWharton/butterknife

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容