注解Butterknife使用 代替findViewById

 

注解Butterknife使用

代替findviewbyid

开发步骤

一、引入依赖包配置

1.引入依赖库:

app的Dependencies中:

操作如下:File-Project Structure -app-Dependencies-“+”Library dependency-“com.jakewharton:butterknife:8.4.0”

 

2.app的build.gradle中,操作如下

 

 

输入如下依赖包名:

//ButterKnife

compile 'com.jakewharton:butterknife:8.4.0'

annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'

3.RebuildProject一下项目


二、开发中运用

1.Activity中

在类中,onCreate中输入:  ButterKnife.bind(this);

然后在声明变量的时候使用: @BindView(R.id.XX)

     RadioButton mRbtnHome;

 


 

2.Fragment中

在类中,onCreateView中输入:  ButterKnife.bind(this, view);这里注意要是用this而不是getActivity()呢。

然后在声明变量的时候使用: @BindView(R.id.XX)

     RadioButton mRbtnHome;