Java Annotation简化findViewById
来源:互联网 发布:薛之谦 像风一样 知乎 编辑:程序博客网 时间:2024/06/18 13:24
直接贴代码
/** * @author huangbo */public class ViewFindUtil { @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface ViewId { int value(); } /** * * @param object the object whose field should be modified * @param root */ public static void findViews(Object object, View root) { /** * Returns an array containing {@code Field} objects for all fields declared * in the class represented by this {@code Class}. If there are no fields or * if this {@code Class} represents an array class, a primitive type or void * then an empty array is returned. * * @see #getFields() */ Field fields[] = object.getClass().getDeclaredFields(); for (Field field : fields) { /** * is annotation exist */ boolean isExist = field.isAnnotationPresent(ViewId.class); if (!isExist) continue; ViewId viewId = field.getAnnotation(ViewId.class); try { /** * reset field data */ field.setAccessible(true); field.set(object, root.findViewById(viewId.value())); } catch (IllegalAccessException e) { e.printStackTrace(); } } }}
用法
class ViewHolder{ @ViewId(R.id.etNum)/*textView Id*/ public TextView textView; public ViewHolder(View rootView) { ViewFindUtil.findViews(this,rootView); } }View view=View.inflate(context, R.layout.layout_name,null);ViewHolder vh= new ViewHolder(view);vh.textView.setText("success");
阅读全文
0 0
- Java Annotation简化findViewById
- 通过JAVA注解简化android的findViewById
- Android 反射简化findViewById
- Android 反射简化findViewById
- Android 反射简化findViewById
- Android 简化findViewById写法
- Android 简化 findViewById
- 使用反射简化findviewbyid
- android findviewbyId 简化写法
- Java Annotation实例:使用Annontaion简化开发
- Java Annotation实例:使用Annontaion简化开发
- Java Annotation实例:使用Annontaion简化开发
- Android通过泛型来简化findViewById
- 泛型编程简化findViewById
- findviewbyid跟setOnClickListtener的简化
- ButterKnife的使用、简化FindViewById
- android利用annotation代替findViewById()
- 使用 ButterKnife annotation 代替 findViewById()
- 有名管道和无名管道的区别
- 【视觉-三维视觉技术-理论篇】三维视觉技术----双目立体视觉,结构光等-战略总结0
- CodeForces
- 常用正则表达式
- c编程(一)基础知识
- Java Annotation简化findViewById
- 内存映射文件原理探索
- RxJava入门
- Linux Shell中 eval语法调研
- KETTLE使用的注意事项
- 发送端用Jrtplib库进行发送,接收端通过VLC接收并播放——有参考代码
- JAVA使用JCO实现调用SAP接口方法
- Java中String、StringBuffer、StringBuilder的区别
- qml自定义SwitchButton