ButterKnife的基本使用_____摆脱findviewbyid
2017-10-04 22:17
417 查看
作用
ButterKnife是一个专注于Android系统的View注入框架,可以减少大量的findViewById以及setOnClickListener代码,可视化一键生成。
导包
在module的gradle中 //bufferknife框架 compile 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
基本使用
public class MainActivity extends AppCompatActivity { Unbinder bind; //绑定单个视图 @BindView(R.id.tv) TextView tv; //绑定多个视图 @BindViews({R.id.tv, btn}) List<View> views; //方法名随意,带不带形参都可以 @OnClick(btn) public void cl(View view){ Toast.makeText(getApplicationContext(),"哈哈"+view.getId(),Toast.LENGTH_SHORT).show(); } @OnLongClick(R.id.btn) public boolean lo(){ Toast.makeText(getApplicationContext(),"长按",Toast.LENGTH_SHORT).show(); return true; } //绑定字符串 @BindString(R.string.app_name) String appname; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //绑定butterknife bind = ButterKnife.bind(this); //在Fragment或其他视图中使用,其中Fragment在onCreat中bind,在onDestry中unBind //ButterKnife.bind(this,view); tv.setText(appname); } @Override protected void onDestroy() { //在onDestoy中解除绑定 bind.unbind(); super.onDestroy(); } }
推荐配合zelezny 使用,一键生成代码
在setting>>plugin>>Browse respositories中搜做zelezny install后重启即可
点击选中R.layout.xx,右键generate,选择generate butterknife即可一键绑定视图
相关文章推荐
- ButterKnife基本使用(代替烦人的findViewById)
- butterknife使用,相当于findviewById
- LayoutInflater 的基本使用; LayoutInflater ,setContentView,findViewById三者的对比。
- ButterKnife懒人库的使用 摆脱findviewbyid苦力
- 你还在苦逼地findViewById吗?使用ButterKnife从此轻松定义控件
- 注解Butterknife使用 代替findViewById
- ButterKnife的使用、简化FindViewById
- 你还在苦逼地findViewById吗?使用ButterKnife从此轻松定义控件
- android 使用泛型再也不写findViewById方法了
- 注释参数opengl的surfaceview使用findViewById返回null问题解决
- 关于android Activity中注解的使用,省去无用的findviewbyid....setonclick.....
- Android开发框架xUtils简介(附有不使用findViewById的方法)
- opengl的surfaceview使用findViewById返回null问题解决
- 还在使用findViewById()?教你如何注解式绑定控件
- 使用InjectView和findViewById说拜拜
- view右对齐 findviewbyid dialogbuilder SharedPreference 的使用
- Android版本:使用findViewById()用字符串/在一个循环
- android ButterKnife 解决重复findViewById
- Android解决使用findViewById时需要对返回值进行类型转换问题的辅助类
- Android解决使用findViewById时需要对返回值进行类型转换问题的辅助类