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