一、ButterKnife 是什么?
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.
https://github.com/JakeWharton/butterknife
二、使用ButterKnife
2.1 导入ButterKnife至项目中
1.在Project的build.gradle中导入butterknife; 如下所示:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
//加入下面这段代码
classpath 'com.jakewharton:butterknife-gradle-plugin:8.5.1'
}
}
allprojects {
repositories {
jcenter()
}
}
2.在Module的build.gradle中添加butterknife的插件,如下:
apply plugin: 'com.android.application'
//加入下面这段代码
apply plugin: 'com.jakewharton.butterknife'
3.在Module的build.gradle中添加依赖,然后同步项目。如下所示:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:26+'
//加入下面这两行代码
compile 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
}
2.2 使用ButterKnife初始化控件
1. 在activity生命周期的onCreate()方法中初始化butterknife框架;注意初始化需要放在setContentView之后。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
2. 绑定控件
public class MainActivity extends AppCompatActivity {
//绑定控件
@BindView(R.id.tv_test1)
TextView tvTest;
@BindView(R.id.btn_test1)
Button btnTest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
}
注意: 控件不能被声明为private
3.测试控件是否被正确初始化
tvTest.setText("hello world!");
btnTest.setText("I'm OK");
4.为控件添加点击事件
@OnClick(R.id.btn_test1)
public void onClick(View view){
btnTest.setText("Clicked");
tvTest.setText("you are welcome !");
}
2.3 其他用途
绑定res里资源对象
public class MainActivity 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
// ...
}
绑定Fragment
public class SampleFragment extends Fragment {
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.sample_fragment, container, false);
ButterKnife.bind(this, view);
// TODO Use fields...
return view;
}
}
绑定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.item, parent, false);
//利用构造器将item的view传入viewHolder中
holder = new ViewHolder(view);
view.setTag(holder);
}
holder.name.setText("Soccet");
// etc...
return view;
}
static class ViewHolder {
@BindView(R.id.title) TextView name;
@BindView(R.id.book_title) TextView jobTitle;
public ViewHolder(View view) {
//在构造器中绑定view
ButterKnife.bind(this, view);
}
}
}
绑定控件ID并把控件添加至集合或者数组中
@BindViews({ R.id.first_name, R.id.middle_name, R.id.last_name })
//nameViews的集中中添加了三个edittext对象
List<EditText> nameViews;
绑定listview的item点击事件
@OnItemSelected(R.id.list_view)
void onItemSelected(int position) {
// TODO ...
}
findviewById的简化
View view = LayoutInflater.from(context).inflate(R.layout.person, null);
TextView firstName = ButterKnife.findById(view, R.id.first_name);
TextView lastName = ButterKnife.findById(view, R.id.last_name);
ImageView photo = ButterKnife.findById(view, R.id.photo);
使用apply一次性改变集合中所有对象的值
//DISABLE是一个接口的实现类
ButterKnife.apply(nameViews, DISABLE);
ButterKnife.apply(nameViews, ENABLED, false);
Action and Setter interfaces allow specifying simple behavior.
//初始化DISABLE
static final ButterKnife.Action<View> DISABLE = new ButterKnife.Action<View>() {
@Override public void apply(View view, int index) {
//将view设为不可输入
view.setEnabled(false);
}
};
//以下是传入布尔值
static final ButterKnife.Setter<View, Boolean> ENABLED = new ButterKnife.Setter<View, Boolean>() {
@Override public void set(View view, Boolean value, int index) {
view.setEnabled(value);
}
};
//如果是view的自带属性,可以不用实现接口,直接使用
ButterKnife.apply(nameViews, View.ALPHA, 0.0f);
在Android Studio 中还可以使用 “Android ButterKnife Zelezny”注解插件进行一键话初始化操作,具体使用步骤就不详述了。
总结
以上对ButterKnife如何使用有了清晰的认识,接下来就要探析其内部实现及原理了。