android从入门到放弃2--ButterKnife

来源:互联网 发布:淘宝购买的模板在哪里 编辑:程序博客网 时间:2024/05/04 07:40


Project gradler 设置:

<span style="font-size:18px;">classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'</span>


Module gradler 设置:
添加的库
<span style="font-size:18px;">apply plugin: 'android-apt'compile 'com.jakewharton:butterknife:8.1.0'apt 'com.jakewharton:butterknife-compiler:8.1.0'</span>


<span style="font-size:18px;">public class MainActivity extends AppCompatActivity {    @BindView(R.id.text_hello)    TextView mTvNick;    @OnClick(R.id.btn_hello) void submit() {        mTvNick.setText("按钮点击了!");    }    private Unbinder unbinder;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        unbinder = ButterKnife.bind(this);        mTvNick.setText("hei hei");    }    @Override    protected void onDestroy() {        super.onDestroy();        unbinder.unbind();    }}</span>


ButterKnife是采用注解的方式,在编译的时候生成java代码,实际上也是生成 findviewbyid 的代码。不是用反射的机制,因此性能很高。
除了能够对实现获取控件元素之外,还能够实现 点击事件的注解。大大的提高了开发效率。


说的比较简陋,直接上源码。




github地址:https://github.com/wuzhuojun/firstcode/tree/master/fcode2










github地址:https://github.com/wuzhuojun/firstcode/tree/master/fcode1
0 0
原创粉丝点击