自定义 Android 注解

120 阅读1分钟
//@Retention表示这个注解保留的范围,SOURCE=注解将被编译器编译的时候丢弃,不在代码运行时存在,这个注解只是希望IDE警告限定值的范围并不需要保留到VM或者运行时
    @Retention(RetentionPolicy.SOURCE)
//@Target 这个注解需要使用的地方 PARAMETER=注解将被使用到方法的参数中
    @Target({ElementType.PARAMETER})
//显式声明被定义的整数值,除了@IntDef还有@LongDef @StringDef等等
    @IntDef(value = {MyAnnotation.CONST_V1, MyAnnotation.CONST_V2})
    public @interface MyAnnotation {
        int CONST_V1 = 0x00;
        int CONST_V2 = 0x01;
    }