Android 注解解析及使用

目录

一、注解解析。

1.什么是注解?

2.为什么要使用注解?

3.android中常见的注解有哪些?

4.元注解。

二、注解使用。

1.如何实现一个注解?

2.android中注解示例。



一、注解解析。

1.什么是注解?

注解是一系列元数据,它提供数据用来解释程序代码,但是注解并非是所解释代码的一部分。注解对于代码的运行效果没有直接影响。

通俗易懂点讲,注解就是一种标签,对类、方法、属性等赋予的一种代码级解释,对代码的运行效果没有丝毫影响。

那么问题来了,既然对代码没有丝毫影响,为什么要用它呢?我们带着问题来进行下面的阅读。

2.为什么要使用注解?

  • 通过注解中的参数值,进行条件判断来处理对应的逻辑。
  • 通过注解可以处理一些有规律性的代码处理,简化代码,比如我们一直重复写的findViewById问题。
  • 通过注解我们可以自动生成一些代码,比如我们用的greendao,这样自动生成,既可以避免错误,又让代码简洁。

下面我们会讲到一些注解的实际使用场景,大家先了解注解的一些概念及方法。

3.android中常见的注解有哪些?

下面我们来讲一下android中常见的注解,带大家先有个基本认识。

@override:当子类重写父类的方法时,用于标识继承自父类的方法。

@deprecate:用于标记一些过时的方法,相信大家都不陌生,比如我们调用一些过时的api,中间会有一条删除线。

@SupressWarnings: 忽略警告。all表示忽略全部警告。

4.元注解。

元注解是用于注解注解的注解。

听起来可能有点拗口,元注解就是在我们定义的注解上添加的注解即为元注解,元注解一共有四个,大家别慌,我们一个一个来看。

1.@Retention

该注解用于标识我们的定义的注解的过期时间。一共有三个值:

RetentionPolicy.SOURCE:源码阶段,就是说我们在代码编写阶段有效。

RetentionPolicy.CLASS:编译阶段,代码编译前有效。

RetentionPolicy.RUNTIME:运行时阶段,代码会一直有效,生命周期很长。

2.@Target

该注解用于标识我们定义的注解的使用场景,比如方法名上,类上等。下面是它可以使用场景的值:                  ElementType.ANNOTATION_TYPE 可以给一个注解进行注解
ElementType.CONSTRUCTOR 可以给构造方法进行注解
ElementType.FIELD 可以给属性进行注解
ElementType.LOCAL_VARIABLE 可以给局部变量进行注解
ElementType.METHOD 可以给方法进行注解
ElementType.PACKAGE 可以给一个包进行注解
ElementType.PARAMETER 可以给一个方法内的参数进行注解
ElementType.TYPE 可以给一个类型进行注解,比如类、接口、枚举

3.@Documented

文档相关,将注解中的元素包含到 Javadoc 中去。

4.@inherit

如果一个超类被 @Inherited 注解过的注解进行注解的话,那么如果它的子类没有被任何注解应用的话,那么这个子类就继承了超类的注解。

二、注解使用。

1.如何实现一个注解?

我们想自定义一个注解的话,跟接口类似,用关键字@interface。

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface MyAnnotation {
    int getAge() default 0; //方法可以设置默认值
}

上面我们一定一个注解为MyAnnotation,定义这个注解我们使用场景为给属性添加并且一直有效。

那么我们接下来使用这个注解,我们定义个user类。

public class User {
    @MyAnnotation(getNum = 20)
    private int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

我们给字段属性age设置了注解,并且值为20;那么我们接下来实现代码获取到这个20.

public static void main(String[] args) throws NoSuchFieldException {
        //获取user类对象
        Class<User> userClass = User.class;
        //获取user类的age属性值
        Field ageField = userClass.getDeclaredField("age");
        //判断该属性值是否包含MyAnnotation注解
        boolean isExit = ageField.isAnnotationPresent(MyAnnotation.class);
        if (isExit){
            //如果包含,获取到注解
            MyAnnotation annotation = ageField.getAnnotation(MyAnnotation.class);
            //获取到注解值
            int num = annotation.getNum();
            System.out.println("num:" + num);
        }
    }

这里需要将几个重要方法:

isAnnotationPresent(Annotation.class):该方法判断是否包含此注解,可以用在类,属性,方法等。

getAnnotation(Annotation.class):该方法获取此注解对象。

那么我们通过上面的例子,基本上是可以使用注解了。下面我们来讲几个android中常用的例子。

2.android中注解示例。

我们android中注解,比如Butterknife,Evenbus,GreenDao等。

我们来自己实现一下Butterknife。

首先定义我们的注解,一共俩个,@BindView、@BindLayout.

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Documented
public @interface BindView {
    int value();
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface BindLayout {
    @LayoutRes
    int value();
}

我们定义了俩个注解,通过名字大家也可以看出,一个为绑定view,一个绑定布局xml。

下面我们来实现activity添加注解。

@BindLayout(R.layout.activity_main)
public class MainActivity extends BaseActivity {


    @BindView(value = R.id.tv)
    TextView textView;


    @Override
    void create(){
        textView.setTextColor(Color.parseColor("#ff0000"));
    }
}

也很简单,我们给textview控件添加id注解,并且oncreate初始化时进行文本变色,给activity添加了布局文件xml申明的注解。

public abstract class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bindLayout();
        bind();
        create();
    }

    abstract void create();

    protected void bind(){
        Class aClass = this.getClass();
        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field field : declaredFields){
            boolean isExit = field.isAnnotationPresent(BindView.class);
            if (isExit){
                BindView annotation = field.getAnnotation(BindView.class);
                int resId = annotation.value();
                try {
                    Method method = aClass.getMethod("findViewById", int.class);
                    method.setAccessible(true);
                    Object invoke = method.invoke(this, resId);
                    field.setAccessible(true);
                    field.set(this,invoke);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    protected void bindLayout(){
        Class aClass = this.getClass();
        BindLayout bindLayout = (BindLayout) aClass.getAnnotation(BindLayout.class);
        if (bindLayout != null){
            int layoutId = bindLayout.value();
            try {
                Method method = aClass.getMethod("setContentView", int.class);
                method.setAccessible(true);
                method.invoke(this, layoutId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

我们的BaseActivity中做了绑定view跟layout的初始化操作,通过反射来获取到setContentView方法跟findViewById方法进行设置我们的注解值的。代码很全很清晰,如果大家有不清楚的,可以参考我的下一篇,反射篇。

 

如果帮到大家,请大家帮忙点个赞,大家的关注和支持是我坚持下去的动力。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值