学会android自定义注解,只需3分钟

**

学会android自定义注解,只需3分钟

**
要学习自定义注解,首先我们要知道注解是什么

An annotation is a form of metadata, that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated. Annotations have no direct effect on the operation of the code they annotate.

注解是一种元数据, 可以添加到java代码中. 类、方法、变量、参数、包都可以被注解,注解对注解的代码没有直接影响.

了解了这个之后我们来开始学习怎么去实现注解
在实现之前,我们要先了解四种元注解

@Retention 保留的范围,默认值为CLASS. 可选值有三种
SOURCE, 只在源码中可用
CLASS, 在源码和字节码中可用
RUNTIME, 在源码,字节码,运行时均可用

@Target 可以用来修饰哪些程序元素,如
CONSTRUCTOR:用于描述构造器
FIELD:用于描述域
  LOCAL_VARIABLE:用于描述局部变量
  METHOD:用于描述方法
  PACKAGE:用于描述包
  PARAMETER:用于描述参数
  TYPE:用于描述类、接口(包括注解类型) 或enum声明
@Inherited 是否可以被继承,默认为false
@Documented 是否会保存到 Javadoc 文档中

熟悉了这个之后我们可以开始写了
我们现在来做一个小练习,自定义android View,Layout的注解
首先定义两个接口
ViewInject.class


@Target({ElementType.FIELD,ElementType.METHOD,ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface ViewInject {
    int value() default -1;
}

LayoutInject.class

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface LayoutInject {
    int value() default -1;
}

然后在来定义一个android的父类BaseActivity.class

public abstract class BaseActivity extends Activity{
    private int mLayoutId = -1;//父类布局id

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);

    injectView();
    layoutView();
    }

    private void injectView(){
        if(mLayoutId <= 0){//父类布局是否为空,为空则返回不执行下面任务
            return;
        }
        Class<?> clazz = this.getClass();//获取当前的Class对象
        Field[] fields = clazz.getDeclaredFields();//获取对象当中的所有属性
        for (Field field:fields){//遍历class中的所有属性
            if (field.getAnnotations() != null){//判断当前属性是否被注解
                if(field.isAnnotationPresent(ViewInject.class)){//判断当前的注解类是否为ViewInject.class
                    field.setAccessible(true);//设置当前属性可以被修改
                    ViewInject inject = field.getAnnotation(ViewInject.class);//获取当前注解的类
                    try {
                        field.set(this,this.findViewById(inject.value()));//将注解的值设置到当前的属性中
                    } catch (IllegalAccessException e) {
                        throw new IllegalAccessError("not found View id!");
                    }
                }
            }
        }
    }

    private void layoutView(){
        Class<?> clazz = this.getClass();//获取当前的class
        if(clazz.getAnnotations() != null){//判断当前class是否有被注解
            if(clazz.isAnnotationPresent(LayoutInject.class)){//判断当前的注解类是否为LayoutInject.class
                LayoutInject inject = clazz.getAnnotation(LayoutInject.class);//获取当前注解的类
                mLayoutId = inject.value();//获取当前注解的值
                setContentView(mLayoutId);//设置到当前的Activity中
            }
        }
    }
}

这样就写好了,不过也要有一点的java反射机制的基础才行
最后来看一下代码调用

@LayoutInject(R.id.activity_main)
public class MainActivity extends BaseActivity {

    @ViewInject(R.id.tv_content)
    private TextView content;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        content.setText("hello android");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值