ButterKnife是JakeWharton完成的一個注解框架,它不會影響App的效率,被廣泛應用于Android開發(fā)。
項目地址:https://github.com/JakeWharton/butterknife
文檔地址:http://jakewharton.github.io/butterknife/
使用ButterKnife,我們可以不用寫很多的findViewById()語句,以及通過getResources獲取String、Color等資源,這可以讓我們的代碼更加簡潔,使用起來也很方便。下面來看怎么用吧!
首先當然是添加依賴(建議去github查看最新版本)
implementation 'com.jakewharton:butterknife:9.0.0-rc2'
annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-rc2'
接下來要在Activity中進行綁定:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
需要注意的是:
- Butterkinife.bind(this) 必須在setContentView()后面;
- setContentView()不能通過注解實現(xiàn);
- 在父類中綁定后,就不用在子類中再進行綁定了(因此,建議寫一個BaseActivity,進行Activity綁定,然后讓所有的Activity去繼承BaseActivity)。
綁定控件
通常操作:
Button button = (Button)findViewById(R.id.button);
使用ButterKnife:
@BindView(R.id.button) Button button;
這里需要注意:使用ButterKnife注解的控件或資源不能用private等修飾符。
綁定資源
通常操作:
String str = getResources().getString(R.string.app_name);
int color = getResources().getColor(R.color.colorPrimary);
Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher_background);
使用ButterKnife:
@BindString(R.string.app_name) String str;
@BindColor(R.color.colorPrimary) int color;
@BindDrawable(R.drawable.ic_launcher_background) Drawable drawable;
是不是高端大氣上檔次,簡潔明了不啰嗦?。?!
當然,在Fragment中使用也不是問題
public class LeftFragment extends Fragment {
@BindView(R.id.button)
Button button;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_left, container, false);
ButterKnife.bind(this, view);
return view;
}
}
不過在Fragment中使用ButterKinfe時,我們需要在onDestroy方法中對其進行解綁,因此在Fragment中使用時我們要這樣寫:
public class LeftFragment extends Fragment {
@BindView(R.id.button)
Button button;
private Unbinder unbinder;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_left, container, false);
unbinder = ButterKnife.bind(this, view);
return view;
}
@Override
public void onDestroy() {
super.onDestroy();
unbinder.unbind();
}
}
當我們調(diào)用 ButterKnife.bind() 方法時,會返回一個Unbinder對象,我們只需要重寫onDestroy()方法,調(diào)用Unbinder對象的unbind()方法,進行解綁。
在Adapter里面也是可以用的
public class ListAdapter extends BaseAdapter {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null){
convertView = mInflater.inflate(R.layout.item_article, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}else {
holder = (ViewHolder)convertView.getTag();
}
return convertView;
}
public final class ViewHolder{
@BindView(R.id.title) TextView mTitleText;
@BindView(R.id.inform) TextView mInformText;
public ViewHolder(View view){
ButterKnufe.bind(this, view)
}
}
}
總之,任何需要 findViewById() 的地方都可以使用ButterKnife來簡化代碼。
前面只說了綁定View和資源,接下來當然是Listener啦
單個View監(jiān)聽
@OnClick(R.id.show)
public void show(){
//TODO
}
@OnClick(R.id.show)
public void show(TextView textview){
//TODO
}
@OnClick(R.id.show)
public void show(View view){
//TODO
}
所有監(jiān)聽方法的參數(shù)都是可選的,可寫可不寫。
多個View監(jiān)聽
@OnClick({R.id.button1, R.id.button2, R.id.text})
public void show(View view){
switch (view.getId()){
case R.id.button1:
//TODO
break;
case R.id.button2:
//TODO
break;
case R.id.text:
//TODO
break;
default:
break;
}
}
當然,這里所列舉出來的都是一些比較常用的基本用法,如果你想了解更多關于ButterKnife的用法,可以去看看 JakeWharton 給出的文檔。
補充:
如果在運行時報這個錯誤:

只需要在 app 的 build.gradle 文件中的 android 標簽下 加上
android {
···
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
將JDK版本指定為1.8即可解決。