ButterKnife8.4.0的使用方法
1 在项目下的build.grale中添加
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
// ButterKnife 依赖注解 需要添加
classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0'
}
2 在model中的build.gradle中添加
dependencies {
// ButterKnife 依赖注解 需要添加
compile 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
}
3 在BaseActivity中的使用
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(bindLayout());
ButterKnife.bind(this);
}
4 继承BaseActivity的使用
// 初始化控件
public class HomeActivity extends BaseActivity {
// 注意:控件的修饰类型不能是:private 或 static
//绑定控件
@BindView(R.id.home_rl1)
RelativeLayout r1;
//绑定资源
@BindString(R.string.title)
String title;
@BindColor(R.color.red)
int red;
@BindDrawable(R.mipmap.aa)
Drawable aa;
@BindDimen(R.dimen.bb)
float bb;
}
//控件的点击事件
@OnClick({R.id.home_rl1,R.id.home_rl2,R.id.home_rl3})
public void onClick(View view){
switch (view.getId()) {
case R.id.home_rl1:
//相应的逻辑
break;
case R.id.home_rl2:
//相应的逻辑
break;
case R.id.home_rl3:
//相应的逻辑
break;
}
}
5 在非Activity中使用
public class MFragment extends Fragment {
@BindView(R.id.tv1)
TextView tv1;
@BindView(R.id.tv2)
TextView tv2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.m_fragment, container, false);
ButterKnife.bind(this, view);
return view;
}
}