一般的绑定
headPicture = (ImageView)findViewById(R.id.UserHeadPicture);
注解式绑定
1) 导butterknife.BindView包:http://jingyan.baidu.com/article/48b37f8d37ca921a64648833.html
2) 编写处理注解的方法(直接粘贴就可以,没有什么特别的地方):
public static void initBindView(Object currentClass){
Field[] fields = currentClass.getClass().getDeclaredFields();
if (fields != null && fields.length > 0){
for (Field field : fields){
if (field.isAnnotationPresent(BindView.class)){
BindView bindView = field.getAnnotation(BindView.class);
int viewId = bindView.value();
try{
field.setAccessible(true);
field.set(currentClass, ((Activity)currentClass).findViewById(viewId));
} catch (Exception e){
e.printStackTrace();
}
}
}
}
}
3) 调用上面的方法(要在setContentView()方法调用之后)
ButterKnife.bind(this);
initBindView(this);
4) 之后就可以用这种方式来绑定控件了
@BindView(value = R.id.firstname_edit)
private EditText firstName;
@BindView(value = R.id.lastname_edit)
private EditText lastName;
@BindView(value = R.id.id_edit)
private EditText id;
@BindView(value = R.id.save_btn)
private Button save;
@BindView(value = R.id.read_btn)
private Button read;