一、 ButterKnife介绍
注解中相对简单易懂的很不错的开源框架
1. 强大的View绑定和Click事件处理功能,简化代码,提升开发效率
2. 方便的处理Adaper里的ViewHolder绑定问题
3. 运行时不会影响app效率,使用配置方便
4. 代码清晰,可读性强
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.
事件注解
一共有12个
名称 | 作用 |
---|---|
@OnClick | 点击事件 |
@OnCheckedChanged | 选中,取消选中 |
@OnEditorAction | 软键盘的功能键 |
@OnFocusChange | 焦点改变 |
@OnItemClick | item被点击(注意这里有坑,如果item里面有Button等这些有点击的控件事件的,需要设置这些控件属性focusable为false) |
@OnItemLongClick | item长按(返回真可以拦截onItemClick) |
@OnItemSelected | item被选择事件 |
@OnLongClick | 长按事件 |
@OnPageChange | 页面改变事件 |
@OnTextChanged | EditText里面的文本变化事件 |
@OnTouch | 触摸事件 |
@Optional | 选择性注入,如果当前对象不存在,就会抛出一个异常,为了压制这个异常,可以在变量或者方法上加入一下注解,让注入变成选择性的,如果目标View存在,则注入, 不存在,则什么事情都不做=如下代码 |
绑定注解
有11种
名称 | 解析 |
---|---|
@BindViews | 绑定多个view id为一个view的list变量 |
@BindView | 绑定一个view id为一个view 变量 |
@BindArray | 绑定string里面array数组,@BindArray(R.array.city ) String[] citys ; |
@BindBitmap | 绑定图片资源为Bitmap,@BindBitmap( R.mipmap.wifi ) Bitmap bitmap; |
@BindBool | 绑定真假boolean |
@BindColor | 绑定color,@BindColor(R.color.colorAccent) int black; |
@BindDimen | 绑定Dimen,@BindDimen(R.dimen.borth_width) int mBorderWidth; |
@BindDrawable | 绑定Drawable,@BindDrawable(R.drawable.test_pic) Drawable mTestPic; |
@BindFloat | 绑定float |
@BindInt | 绑定int |
@BindString | 绑定一个String id为一个String变量,@BindString( R.string.app_name ) String meg; |