開發(fā)中找到xml中對(duì)應(yīng)的ui并設(shè)置對(duì)應(yīng)的事件,一般用findViewById握童,然后設(shè)置onClick事件來實(shí)現(xiàn)来候,這樣寫起來比較麻煩,有沒更簡單的仅仆?Butterknife本場(chǎng)的主角閃亮登場(chǎng).
Butterknife作用:
通過注解的方式來對(duì)Android View進(jìn)行綁定.
Butterknife的Github源碼如下:
https://github.com/JakeWharton/butterknife
對(duì)它的介紹可以看下面鏈接:
http://jakewharton.github.io/butterknife/
下面根據(jù)文檔描述,簡單的介紹下如何使用:
- 注解實(shí)例變量view 取代findViewById
class ExampleActivity extends Activity {
@BindView(R.id.title)
TextView title;//取代findViewByid(R.id.title)
@BindView(R.id.subtitle)
TextView subtitle;//取代findViewByid(R.id.subtitle)
@BindView(R.id.footer)
TextView footer;//取代findViewByid(R.id.footer)
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);
ButterKnife.bind(this);//注意這里必須設(shè)置綁定當(dāng)前Activity
// TODO Use fields...
//后續(xù)就可以直接使用比如
title.setText("Just test");
}
}
上面代碼在butterknife內(nèi)部生產(chǎn)類似下面的code
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);
}
- 對(duì)資源綁定注解
在Butterknife中定義如下對(duì)資源的注解
@BindBool, @BindColor, @BindDimen, @BindDrawable, @BindInt, @BindString 上面的可以對(duì)應(yīng)獲取android res目錄下對(duì)應(yīng)的資源。
舉個(gè)??:
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
// ...
}
- 不在Activity中如何使用butterknife
- bind對(duì)應(yīng)的view通過它們的root view,舉個(gè)例子在Fragment中
public class FancyFragment extends Fragment {
@BindView(R.id.button1) Button button1;
@BindView(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);//注意這里傳入的是root view
// TODO Use fields...
return view;
}
}
或者是在ViewHolder中使用
static class ViewHolder {
@BindView(R.id.title) TextView name;
@BindView(R.id.job_title) TextView jobTitle;
public ViewHolder(View view) {
ButterKnife.bind(this, view);//這里對(duì)應(yīng)的是bind(this,view)
}
}
- 綁定View List
@BindViews({ R.id.first_name, R.id.middle_name, R.id.last_name })
List<EditText> nameViews;
- 綁定View的事件
對(duì)應(yīng)View onClick事件
@OnClick(R.id.submit)
public void submit(View view) {//其中submit方法中的參數(shù)是可選的
// TODO submit data to server...
}
對(duì)應(yīng)ViewOnItemSelected事件
@OnItemSelected(R.id.list_view)
void onItemSelected(int position) {
// TODO ...
}
當(dāng)然也可以多個(gè)view對(duì)應(yīng)一個(gè)click事件如下:
@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();
}
}
在自定義View的綁定事件更簡單如下:
public class FancyButton extends Button {
@OnClick
public void onClick() {
// TODO do something!
}
}
- 釋放對(duì)應(yīng)的bind資源
public class FancyFragment extends Fragment {
@BindView(R.id.button1) Button button1;
@BindView(R.id.button2) Button button2;
private Unbinder unbinder;//全局變量
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fancy_fragment, container, false);
unbinder = ButterKnife.bind(this, view);
// TODO Use fields...
return view;
}
@Override public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();//在onDestoryView中釋放對(duì)應(yīng)的資源
}
}
- 其他較為少用的方法
TextView firstName = ButterKnife.findById(view, R.id.first_name);
//替換findViewById 不需要強(qiáng)轉(zhuǎn)
//The apply method allows you to act on all the views in a list at once
ButterKnife.apply(nameViews, DISABLE); //馬上設(shè)置
ButterKnife.apply(nameViews, ENABLED, false);//設(shè)置
遇到版本問題叠荠,具體如下
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LogUtil.d("onCreate........................");
myLocationListener = new MyLocationListener(getLifecycle());
setContentView(R.layout.lifecycle_test);
ButterKnife.bind(this);
}
@OnClick(R.id.btnJump)
public void onClick(View view){
Intent intent = new Intent(this, DatabaseStudyActivity.class);
startActivity(intent);
}
發(fā)現(xiàn)點(diǎn)擊Button無法響應(yīng)對(duì)應(yīng)的onClick時(shí)間,什么原因呢扫责?google了下發(fā)現(xiàn)是build.grandle配置出現(xiàn)了問題榛鼎,下面是build.gradle中的配置
implementation 'com.jakewharton:butterknife:8.8.1'
implementation 'com.jakewharton:butterknife-annotations:8.8.1'
ButterKnife從7.x到8.x后,運(yùn)行時(shí)和編譯器被分開來了鳖孤,所以需要加入compiler在build.grandle中者娱,如下所示:
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
這樣整個(gè)build.gradle配置如下:
implementation 'com.jakewharton:butterknife:8.8.1'
implementation 'com.jakewharton:butterknife-annotations:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'