替代枚举的注解

  • Android中新引入的替代枚举的注解有IntDef和StringDef,这里以IntDef做例子说明一下.
public class Colors {
    @IntDef({RED, GREEN, YELLOW})
    //声明必要的int常量,使用@IntDef修饰LightColors,参数设置为待枚举的集合
    @Retention(RetentionPolicy.SOURCE)
    //使用@Retention(RetentionPolicy.SOURCE)指定注解仅存在与源码中,不加入到class文件中
    public @interface LightColors{}
    //声明一个注解为LightColors
    public static final int RED = 0;
    public static final int GREEN = 1;
    public static final int YELLOW = 2;
}
//用法
private void setColor(@Colors.LightColors int color) {
        Log.d("MainActivity", "setColor color=" + color);
}
//调用的该方法的时候
setColor(Colors.GREEN);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容