android通过注解实现findViewById 和 setOnClickListener
来源:互联网 发布:美国eia数据 编辑:程序博客网 时间:2024/04/29 05:06
不多说,上代码
使用
@MyView(value = R.id.text)
TextView t;
@MyClick(value = {R.id.button})
public void myClick(View v){
t.setText(“”+i++);
}
在onCreate()中
MyZhujie.register(this);
ondestroy()
MyZhujie.unRegister(this);
unRegister 需要在super.onDestroy()前
注解1
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyView {
int value() default 0;
}
注解2
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyClick {
int[] value() default 0;
}
初始化工具
class MyZhujie{
static ArrayMap<View,Method> clickIDS = new ArrayMap();static View.OnClickListener mOnclickListener = new View.OnClickListener() { @Override public void onClick(View v) { Method method = clickIDS.get(v); if(method != null){ try{ method.invoke(v.getContext(),v); }catch (Exception e){ e.printStackTrace(); } } }};public static void register(Activity activity) { autoInjectAllField(activity);}public static void unRegister(Activity ctx){ try { zhujie(ctx,false); } catch (Exception e) { e.printStackTrace(); }}private static void autoInjectAllField(Activity ctx) { try { zhujie(ctx, true); } catch (Exception e) { e.printStackTrace(); }}private static void zhujie(Activity ctx , boolean isSet) throws IllegalAccessException { Class clazz = ctx.getClass(); if(isSet){ Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { if (field.isAnnotationPresent(MyView.class)) { MyView myView = field.getAnnotation(MyView.class); int id = myView.value(); if (id > 0) { field.setAccessible(true); field.set(ctx, ctx.findViewById(id)); } } } } Method[] methods = clazz.getDeclaredMethods(); for(Method mMethod : methods) { if (mMethod.isAnnotationPresent(MyClick.class)) { MyClick click = mMethod.getAnnotation(MyClick.class); int[] ids = click.value(); for (int id : ids) { View v = ctx.findViewById(id); if (v != null) { if (isSet) { v.setOnClickListener(mOnclickListener); clickIDS.put(v, mMethod); } else { v.setOnClickListener(null); clickIDS.remove(v); } } } } }}
}
一套简单的注解就完成了
源码
http://download.csdn.net/detail/wwwbjj1988/9604927
0 0
- android通过注解实现findViewById 和 setOnClickListener
- Android中通过注解代替findViewById方法
- Android中通过注解代替findViewById方法
- Android中通过注解代替findViewById方法
- Android中通过注解代替findViewById方法
- 通过JAVA注解简化android的findViewById
- Android中注解详解,通过注解代替findViewById方法
- android findviewbyid的简单注解实现
- android---------Android中通过注解代替findViewById方法
- 关于通过注解反射实现findViewById(int x);的原理
- 安卓通过注解+反射简单实现代替findViewById方法
- Android:注解初始化findViewById
- Android: 使用注解findViewById
- Android 注解findViewById、onClick
- 用反射和注解实现对findViewById的简化调用
- Android中注解和反射的结合findViewById
- 初试注解 自定义实现FindViewById
- Android中通过注解代替findViewById方法 Android 中的 IOC 框架 【ViewInject】
- Yii框架发送邮件
- 这是一个使用mvp模式实现模拟用户登录的简单Demo。
- EditText末尾添加删除按钮
- Java学习笔记之IO流中的File类
- POJ 1185炮兵阵地(状压DP+优化)
- android通过注解实现findViewById 和 setOnClickListener
- POJ 2709 水题
- Spring-Task
- Java垃圾回收(三)垃圾收集器---深入理解Java虚拟机
- HDU 5773 The All-purpose Zero(最长上升子序列)
- 基于CXF Java 搭建Web Service (Restful Web Service与基于SOAP的Web Service混合方案)
- 摄动(Perturbation)
- 能够提高开发效率的Eclipse实用操作
- J2EE-JavaWeb中请求的绝对路径与相对路径