Android: 在enumerate中获取本地资源class
Android: get local resources in enumerate class
我正在用 java 代码编写 android 应用程序。该应用程序可以支持英语和意大利语。在应用程序内部,有一个微调器从枚举 class 中获取其值,如下所示:
public enum ElementTypesEnum {
MEET("Meet"),
CEREAL("Cereal"),
FISH("Fish"),
OTHER("Other");
private String elementType;
ElementTypesEnum(String elementType) {
this.elementType = elementType;
}
public String getElementType() {
return elementType;
}
}
我想用本地字符串资源文件 (R.string.value_1) 中包含的值初始化枚举的值。在此 class 中,我没有资源文件的实例,因为我没有 Context 的实例。我怎样才能做到这一点?提前谢谢你,马可
使用资源 ID,然后在填充时使用微调器的上下文获取字符串。
public enum ElementTypesEnum {
MEAT(R.string.meat),
CEREAL(R.string.cereal),
FISH(R.string.fish),
OTHER(R.string.other);
@StringRes
private int elementType;
ElementTypesEnum(@StringRes int elementType) {
this.elementType = elementType;
}
public int getElementType() {
return elementType;
}
}
我正在用 java 代码编写 android 应用程序。该应用程序可以支持英语和意大利语。在应用程序内部,有一个微调器从枚举 class 中获取其值,如下所示:
public enum ElementTypesEnum {
MEET("Meet"),
CEREAL("Cereal"),
FISH("Fish"),
OTHER("Other");
private String elementType;
ElementTypesEnum(String elementType) {
this.elementType = elementType;
}
public String getElementType() {
return elementType;
}
}
我想用本地字符串资源文件 (R.string.value_1) 中包含的值初始化枚举的值。在此 class 中,我没有资源文件的实例,因为我没有 Context 的实例。我怎样才能做到这一点?提前谢谢你,马可
使用资源 ID,然后在填充时使用微调器的上下文获取字符串。
public enum ElementTypesEnum {
MEAT(R.string.meat),
CEREAL(R.string.cereal),
FISH(R.string.fish),
OTHER(R.string.other);
@StringRes
private int elementType;
ElementTypesEnum(@StringRes int elementType) {
this.elementType = elementType;
}
public int getElementType() {
return elementType;
}
}