Android @Intdef 用于标记如何使用它

标签 android android-annotations

我不清楚如何使用 @Intdef 将它设为这样的标志:

@IntDef(
  flag = true
  value = {NAVIGATION_MODE_STANDARD, NAVIGATION_MODE_LIST, NAVIGATION_MODE_TABS})

这个例子直接来自 docs .这到底是什么意思?这是否意味着所有这些最初都设置为 true ?如果我对以下内容做“或”:

NAVIGATION_MODE_STANDARD | NAVIGATION_MODE_LIST

这是什么意思...我有点困惑这里发生了什么。

最佳答案

使用 IntDef#flag() 属性设置为 true,可以组合多个常量。

用户可以将允许的常量与标志组合(例如 |、&、^)。

例如:

public static final int DISPLAY_OP_1 = 1;
public static final int DISPLAY_OP_2 = 1<<1;
public static final int DISPLAY_OP_3 = 1<<2;

@IntDef (
    flag=true,
    value={
            DISPLAY_OP_1,
            DISPLAY_OP_2,
            DISPLAY_OP_3
    }
)

@Retention(RetentionPolicy.SOURCE)
public @interface DisplayOptions{}

public void setIntDefFlag(@DisplayOptions int ops) {
    ...
}

并使用带有“|”的setIntDefFalg()

setIntDefFlag(DisplayOptions.DISPLAY_OP1|DisplayOptions.DISPLAY_OP2);

关于Android @Intdef 用于标记如何使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34583796/

相关文章:

android - 如何在 NavigationView 中自定义项目背景和项目文本颜色?

AndroidAnnotations - ViewById 不能用于私有(private)元素

java - Intent 过滤器不适用于注释

android - Spring Rest Template 使用导致 EOFException

android - 自定义 ListView 中的复选框和 selectAll 按钮错误

Android - 存储下次启动时保留和使用的游戏状态信息

android - java.lang.NoClassDefFoundError,在我自己的类(class)之一

android - 为相同的小部件类型定义不同的主题样式

Android注解和单元测试

androidannotations2.7.1 - AndroidManifest.xml 文件包含原始组件