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/