Android注解式绑定控件
16lz
2021-01-24
一般的绑定
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;
更多相关文章
- 从源代码编译Android(CyanogenMod For Hero GSM)【转】
- android重新启动应用程序和重新启动系统
- Android(安卓)adb shell启动应用程序的方法
- SDK1.5下 android判断是否存在网络
- android UEventObserver的用法
- 谈谈 View 绘制流程
- android很的意思的事情,关于Input…
- Android(安卓)面试整理
- Ubuntu 编译Android若干错误及解决方法(转)