Android @IntDef 局部变量示例

标签 android annotations

https://developer.android.com/reference/android/support/annotation/IntDef.html

我正在寻找一个如何定义局部变量的示例。

我看到了如何创建一个函数返回值、一个成员变量、一个函数参数,但没有使用 Android 注释元素 @IntDef 分配局部变量的示例。

谢谢

更新

这是一个不起作用的例子。这与放置@Retention 的位置有关吗?我不明白编译器如何知道将保留策略应用于什么。这是全局设置吗?

int foobar() {
        @IntDef({
                ItemType.TYPE1,
                ItemType.TYPE2
        })
        @Retention(RetentionPolicy.SOURCE)
        @interface ItemType {
            int TYPE1 = 0;
            int TYPE2 = 1;
        }


        @ItemType int type = TYPE1;
...
}

另一个对我不起作用的例子:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    final @View.MeasureSpec.MeasureSpecMode int heightMode = MeasureSpec.getMode(heightMeasureSpec);

...

最佳答案

@IntDef({
  ItemType.TYPE1,
  ItemType.TYPE2
})
public @interface ItemType {
  int TYPE1 = 0;
  int TYPE2 = 1;
}

// use it in global variable like
@ItemType
private int type;

// use it in local variable like
public void add(@ItemType int type){

}

关于Android @IntDef 局部变量示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43335109/

相关文章:

java - Google 自定义搜索引擎 android 不接受 %20 或空格

java - Annotation Processor - 如何获取它正在处理的类

android - 选项卡滑动无法正常工作

安卓模拟器讨厌我

android - 多少共享首选项太多了?

javax.annotation.security 不是基于角色的访问控制?

ios - 如何随机显示注释

android - 如何在 Android 中通过通话传输调制语音?

javascript - 在Javascript中,是否可以在运行时访问字段的注释数据?

java - 在 Java 中向接口(interface)添加不变量