Butter Knife (黄油刀)

jakeWharton大神的又一巨作。


github地址:https://github.com/JakeWharton/butterknife

官网地址:http://jakewharton.github.io/butterknife/

字段与方法和安卓视图的绑定

简介:通过注解绑定视图的方法,大大的简化代码的量,。

1.使用@BindView 代替使用findViewById

之前是这样

activity.user=(android.widget.TextView)activity.findViewById(R.id.user);

现在

@BindView(R.id.user)EditText username; 

2.绑定预定义的资源文件:

@BindBool,@BindColor,@BindDimen,@BindDrawable,@BindInt,@BindString

eg:

@BindString(R.string.title)String title;

@BindDrawable(R.drawable.graphic)Drawable graphic;

@BindColor(R.color.red)intred;// int or ColorStateList field

@BindDimen(R.dimen.spacer)Floatspacer;// int (for pixel size) or float (for exact value) field

3.消除点击的匿名内部类 onclicklistener 

@OnClick(R.id.submit)voidsubmit() {//TODO call server...}

4.列表视图的监听

@OnItemSelected(R.id.list_view)voidonItemSelected(intposition){// TODO ...}

@OnItemSelected(value=R.id.maybe_missing,callback=NOTHING_SELECTED)voidonNothingSelected(){// TODO ...}

BONUS

你可能感兴趣的:(Butter Knife (黄油刀))