我不喜欢一直写findViewById怎么办--用ButterKnife呀

    我们平时应用界面中多多少少都要使用到控件,在代码中要使用这些控件,就需要对布局中的view们进行findViewById,这下好了,一个布局文件里面有十几个甚至几十个view,然后我们就重复地findViewById...篇幅又长又没啥意义,怎么办,我就是不想使用findViewById--推荐使用ButterKnife!!!

具体的原理和说明已经有大神做了很充分的解释了,我这边就按照自己的习惯记录一下使用过程。

首先需要在Androidstudio中安装相关插件


点击了Install之后,继续restart  androidstudio。

这时候Androidstudio已经安装上了butterknife插件,然后我们继续在外层的gradle中添加依赖

  dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'
        classpath 'com.jakewharton:butterknife-gradle-plugin:8.5.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }

除此之外,还需要在内层的gradle中添加

apply plugin: 'com.android.application'
//使用这个插件
apply plugin: 'com.jakewharton.butterknife'
然后在依赖库中也要添加依赖

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    //添加下面两句
    compile 'com.jakewharton:butterknife:8.5.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

接下来,在使用到布局的地方,比如我在MainActivity中使用了 R.layout.activity_main 在这里,我双击选择该布局,然后按下快捷键Alt + Insert ,在弹出的窗口中选中Generate Butterknife Injections


很快地,自动为我找到了布局中的控件,我顺便也给按钮绑定点击事件



效果还不错

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.button)
    Button btn1;
    @BindView(R.id.checkBox)
    CheckBox cb1;
    @BindView(R.id.radioButton)
    RadioButton rb1;
    @BindView(R.id.spinner)
    Spinner sp1;
    @BindView(R.id.progressBar)
    ProgressBar pb1;
    @BindView(R.id.textView)
    TextView tv1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

    }

    @OnClick(R.id.button)
    public void onViewClicked() {
    }
}

除了在Activity中使用之外,还可以在其他的一些地方使用比如:

1、使用一些res资源文件


2、在fragment中绑定控件

public class MyFragment extends Fragment {

    @BindView(R.id.textView5)
    TextView tv1;
    @BindView(R.id.textView6)
    TextView tv2;
    Unbinder unbinder;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
还有一些其他地方可以用到,因为暂时没有用到,这边先不记录,后续补充


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值