Java 枚举和 android 注释 intDef
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32032503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enums and android annotation intDef
提问by AliSh
I have an enum:
我有一个枚举:
public enum AppEnums {
SERVICE_ERROR,
CONNECTION_ERROR;
}
and I want to use it in an intDef of Android Annotation:
我想在 Android Annotation 的 intDef 中使用它:
@IntDef({AppEnums.CONNECTION_ERROR, AppEnums.SERVICE_ERROR})
public @interface ServiceErrors {
}
error shows:
错误显示:
incompatible types found, required: 'long'
发现不兼容的类型,需要:'long'
What I can do with this incompatibility?
对于这种不兼容性,我该怎么办?
I don't want to handle values of AppEnum parameters manually, Enum create values automatically ordinarily. AppEnums.CONNECTION_ERROR.ordinal()
return int value of enum parameter but don't work here.
我不想手动处理 AppEnum 参数的值,Enum 通常会自动创建值。AppEnums.CONNECTION_ERROR.ordinal()
返回枚举参数的 int 值,但在这里不起作用。
采纳答案by Oleksii K.
The main idea of IntDef
annotation is to use set of int
constants like an enum
, but withoutenum
. In this case you have to declare all constants manually.
IntDef
注释的主要思想是使用int
像 an 一样的常量集enum
,但不使用enum
. 在这种情况下,您必须手动声明所有常量。
@IntDef({Status.IDLE, Status.PROCESSING, Status.DONE, Status.CANCELLED})
@Retention(RetentionPolicy.SOURCE)
@interface Status {
int IDLE = 0;
int PROCESSING = 1;
int DONE = 2;
int CANCELLED = 3;
}
You can see detailed example here.
您可以在此处查看详细示例。
回答by Bhargav
Well, you can't quite do it that way. AppEnums.SERVICE_ERROR
will never return int
; it will return AppEnums.SERVICE_ERROR
. That's the point of enumerated types.
好吧,你不能完全那样做。AppEnums.SERVICE_ERROR
永远不会回来int
;它会回来AppEnums.SERVICE_ERROR
。这就是枚举类型的意义所在。
What I can suggest is this:
我可以建议的是:
public static class AppEnums {
public static final int CONNECTION_ERROR = 0;
public static final int SERVICE_ERROR = 1;
}
@IntDef({AppEnums.CONNECTION_ERROR,AppEnums.SERVICE_ERROR})
public @interface ServiceErrors {
}
Copied from Yazazzello's comment below:
复制自 Yazazzello 的评论如下:
IntDef - new Enums for Android development. Enums often require more than twice as much memory as static constants. You should strictly avoid using enums on Android. so
IntDef
where designed to replace Enums, you cannot useEnum
inIntDef
declarations
IntDef - 用于 Android 开发的新枚举。枚举通常需要比静态常量多两倍的内存。您应该严格避免在 Android 上使用枚举。因此
IntDef
,在旨在替换枚举的地方,您不能Enum
在IntDef
声明中使用
回答by Alok Singh
@Retention(RetentionPolicy.SOURCE)
@IntDef({NOT_STARTED, PENDING, COMPLETED})
public @interface DownloadState {
int NOT_STARTED = 1;
int PENDING = 2;
int COMPLETED = 3;
}
回答by Alireza aslami
the annotated element of integer type, represents a logical type and that its value should be one of the explicitly named constants.
整数类型的带注释的元素,表示一个逻辑类型,它的值应该是显式命名的常量之一。
NOTE: If the IntDef#flag() attribute is set to true, multiple constants can be combined.
注意:如果 IntDef#flag() 属性设置为 true,则可以组合多个常量。
@IntDef(flag = false,value = {AppEnums.CONNECTION_ERROR, AppEnums.SERVICE_ERROR})
@Retention(RetentionPolicy.SOURCE)
public @interface AppEnums {
int CONNECTION_ERROR = 0;
int SERVICE_ERROR = 1;
}
NOTE: If the IntDef#flag() attribute is set to true, multiple constants can be combined.
注意:如果 IntDef#flag() 属性设置为 true,则可以组合多个常量。
also you can Using @LongDeffor long values and @StringDeffor StringValues
您也可以将@LongDef用于长值,将@StringDef用于 StringValues