android通过注解实现findViewById 和 setOnClickListener

不多说,上代码

使用
@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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值