简单的 Butterknife

来源:互联网 发布:域名污染如何解决 编辑:程序博客网 时间:2024/05/22 04:37

 第一步:

先添加依赖

在app下的bulid.grable里面的dependencies里面加入

compile 'com.jakewharton:butterknife:8.6.0'annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0'
然后就可以不用findViewById()了

可以注解写控件还可以添加多个控件

public class MainActivity extends AppCompatActivity {
//控件    @BindViews({R.id.tv1,R.id.tv2,R.id.tv3})
//因为多个控件要写成List形式    List<TextView> nameView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);
//绑定控件        ButterKnife.bind(this);
//获取每一个控件           nameView.get(0).setText("第一个");           nameView.get(1).setText("第二个");           nameView.get(2).setText("第三个");            }}