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

j2e*_*nue 11 android android-annotations

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

@IntDef(
  flag = true
  value = {NAVIGATION_MODE_STANDARD, NAVIGATION_MODE_LIST, NAVIGATION_MODE_TABS})
Run Code Online (Sandbox Code Playgroud)

这个例子直接来自文档.这究竟意味着什么?这是否意味着它们最初都设置为真?如果我在下面做"或":

NAVIGATION_MODE_STANDARD | NAVIGATION_MODE_LIST
Run Code Online (Sandbox Code Playgroud)

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

小智 14

设置@IntDef的标志,然后你可以使用位操作来组合这些int标志(如|,&...).

Fox例子:

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) {
    ...
}
Run Code Online (Sandbox Code Playgroud)

将setIntDefFalg与'|'一起使用

setIntDefFlag(DisplayOptions.DISPLAY_OP1|DisplayOptions.DISPLAY_OP2);
Run Code Online (Sandbox Code Playgroud)