枚举和android注释intDef [英] Enums and android annotation intDef

查看:163
本文介绍了枚举和android注释intDef的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个枚举:

public enum AppEnums {
    SERVICE_ERROR,
    CONNECTION_ERROR;
}

,我想在Android注释的indDef中使用它:

and I want to use it in an intDef of Android Annotation:

@IntDef({AppEnums.CONNECTION_ERROR, AppEnums.SERVICE_ERROR})
public @interface ServiceErrors {
}

错误显示:


找到不兼容的类型,必需:'long'

incompatible types found, required: 'long'

该不兼容该怎么办?

我不想手动处理AppEnum参数的值,Enum通常会自动创建值。 AppEnums.CONNECTION_ERROR.ordinal()返回枚举参数的int值,但在这里不起作用。

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.

推荐答案

IntDef 批注的主要思想是使用一组 int 常量,例如枚举,但没有 枚举。在这种情况下,您必须手动声明所有常量。

The main idea of IntDef annotation is to use set of int constants like an enum, but without enum. In this case you have to declare all constants manually.

@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.

这篇关于枚举和android注释intDef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆