版权所有,转载请注明出处:linzhiyong https://blog.csdn.net/u012527802/article/details/81059568 https://www.jianshu.com/p/b1fc5b0e9e87
目录
ButterKnife是一个轻量级的注解框架,作用于Android视图的字段和方法、资源的绑定。本文只做一些基本的举例,让没接触过ButterKnife的同学可以简单粗暴的上手。
Github:https://github.com/JakeWharton/butterknife
ButterKnifeDemo传送门:https://github.com/linzhiyong/ButterKnifeDemo
Field and method binding for Android views which uses annotation processing to generate boilerplate code for you.
1. Eliminate findViewById calls by using @BindView on fields.
2. Group multiple views in a list or array. Operate on all of them at once with actions, setters, or properties.
3. Eliminate anonymous inner-classes for listeners by annotating methods with @OnClick and others.
4. Eliminate resource lookups by using resource annotations on fields.
Android视图的字段和方法绑定,使用注释处理生成样板代码。
1. 在字段上使用@BindView消除findViewById调用。
2. 将列表或数组中的多个视图分组。 使用操作,设置器或属性一次操作所有这些操作。
3. 通过使用@OnClick和其他方法注释方法来消除侦听器的匿名内部类。
4. 通过在字段上使用资源注释来消除资源查找。
AndroidStudio -> File -> Settings -> Plugins -> Browse repositories -> 搜索ButterKnife,找到Android ButterKnife Zeleany点击Install,重启AndroidStudio 。
在build.gradle文件中添加:
dependencies {
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
}
如果是在Library中使用ButterKnife,则需要在项目根目录下的build.gradle中添加:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.jakewharton:butterknife-gradle-plugin:8.8.1'
}
}
同时修改Libirary下的build.gradle
apply plugin: 'com.android.library'
apply plugin: 'com.jakewharton.butterknife'
注解名称 | 注解说明 |
---|---|
@BindView | 绑定一个view,@BindView(R.id.listView1) ListView listView; |
@BindViews | 绑定多个view,@BindViews({R.id.textView1, R.id.textView2}) List viewList |
@BindAnim | 绑定一个动画资源,@BindAnim(R.anim.anim_translate_1) Animation animation |
@BindArray | 绑定res/string.xml下的array数组,@BindArray(R.array.list) String[] array |
@BindBitmap | 绑定图片资源为Bitmap,@BindBitmap( R.mipmap.ic_launcher ) Bitmap iconBitmap |
@BindBool | 绑定boolean |
@BindColor | 绑定color,@BindColor(R.color.colorAccent) int colorAccent |
@BindDimen | 绑定Dimen,@BindDimen(R.dimen.width) int width |
@BindDrawable | 绑定Drawable@BindDrawable(R.drawable.ic_launcher_background) Drawable drawable |
@BindFloat | 绑定float |
@BindFont | 绑定font |
@BindInt | 绑定int |
@BindString | 绑定一个String,@BindString( R.string.app_name ) String appName |
注解名称 | 注解说明 |
---|---|
@OnCheckedChanged | CheckBox 选中、取消选中事件绑定 |
@OnClick | view点击事件 |
@OnEditorAction | 软键盘的功能键 |
@OnFocusChange | EditText等 焦点改变事件 |
@OnItemClick | ListView、GridView等组件item点击事件 |
@OnItemLongClick | ListView、GridView等组件item长按事件,返回类型boolean型,返回true可以拦截onItemClick |
@OnItemSelected | Spinner item选择事件 |
@OnLongClick | view 长按事件 |
@OnPageChange | ViewPager 页面改变事件 |
@OnTextChanged | EditText 文本变化事件 |
@OnTouch | view 触摸事件 |
@Optional | 可选绑定,默认情况下,@bind和监听器绑定都必须有一个目标view,当butter knife找不到对应的view时会抛出一个异常。为了防止这种异常情况的发生,可以在绑定的字段前面使用@Nullable注解,在绑定的方法前面则可使用@Option注解,来表明对应的是一个可选绑定。 |
private Unbinder unbinder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
unbinder = ButterKnife.bind(this); // 返回一个Unbinder对象
}
@Override
protected void onDestroy() {
super.onDestroy();
if (unbinder != null) {
unbinder.unbind();
}
}
public class HomeFragment extends Fragment {
private Unbinder unbinder;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main, container, false);
unbinder = ButterKnife.bind(this, view);
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
if (unbinder != null) {
unbinder.unbind();
}
}
}
final class ViewHolder {
@BindView(R.id.nameView)
TextView nameView;
@BindView(R.id.ageView)
TextView ageView;
ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
// 绑定多view
@BindViews({R.id.textView1, R.id.textView2, R.id.textView3})
List viewList;
// 单个绑定
@BindView(R.id.editText1)
EditText editText;
@BindView(R.id.listView1)
ListView listView;
鼠标选中R.layout.XXX –>> 右键 –>> 选择Generate –>> 选择Generate ButterKnife Injections –>> confirm完成
@BindString(R.string.app_name)
String appName;
@BindArray(R.array.list)
String[] array;
@BindBitmap(R.mipmap.ic_launcher)
Bitmap icon;
@BindColor(R.color.colorAccent)
int colorAccent;
@BindDimen(R.dimen.width)
int width;
@BindAnim(R.anim.anim_translate_1)
Animation translateAnimation;
// 绑定单个按钮
@OnClick(R.id.button1)
public void onClick(View view) {
showToast("点击了按钮:" + view.toString());
}
// 绑定多个按钮
@OnClick({R.id.button1, R.id.button2, R.id.button3})
public void onClickEx(View view) {
switch (view.getId()) {
case R.id.button1:
break;
case R.id.button2:
break;
case R.id.button3:
break;
}
}
@OnTouch(R.id.textView1)
public boolean onTouch(View view) {
showToast("touch:" + view.toString());
return true;
}
@OnFocusChange(R.id.editText1)
public void onFocusChange(View view, boolean flag) {
showToast("焦点改变...");
}
@OnItemClick(R.id.listView1)
public void onItemClickListener(int position) {
showToast("ListView点击位置:" + position);
}
@OnItemLongClick(R.id.listView1)
public boolean onItemLongClickListener(int position) {
showToast("ListView长按位置:" + position);
return true;
}
@OnItemSelected(R.id.spinner1)
public void onItemSelectedClickListener(int position) {
showToast("Spinner选择位置:" + position);
}
@OnItemSelected(value = R.id.spinner1, callback = OnItemSelected.Callback.NOTHING_SELECTED)
public void onNothingSelectedClickListener() {
showToast("Spinner未选择item");
}