注意:10.0.0 版本改为8.4.0 降低版本,否则出错。
1.简介
Field and method binding for Android views which uses annotation processing to generate boilerplate code for you.
2.依赖
project 里的build.gradle
classpath 'com.jakewharton:butterknife-gradle-plugin:10.0.0'
module里的build.gradle文件里
//顶部
apply plugin: 'com.jakewharton.butterknife'
dependencies {
implementation 'com.jakewharton:butterknife:10.0.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.0.0'
}
3.使用
activity使用 注意view不能为private 或者static
@BindView(R.id.edit_text)
EditText mEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this); // 绑定
}
4.fragment使用
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.frag_main, container, false);
//这里有些不同
ButterKnife.bind(this, rootView);
return rootView;
}
}
5.点击事件使用
@OnClick(R.id.button)
public void onButtonClick(View view) {
Toast.makeText(this, "button被点击了", Toast.LENGTH_SHORT).show();
}
网友评论