我有一个枚举,我需要显示的值作为本地化的字符串。我目前的做法是这样的:
公开枚举MyEnum {
VALUE1(R.string.VALUE1)
VALUE2(R.string.VALUE2)
。
。
VALUE10(R.string.VALUE10);
私人诠释mResId = -1;
私人MuEnum(INT渣油){
mResId =渣油;
}
公共字符串toLocalizedString(资源r){
如果(-1 = mResId!)回报(r.getString(mResId));
返程(this.toString());
}
}
有没有更简单的方式来做到这一点?我喜欢它,如果基于枚举值的名字(即VALUE1),我可以以某种方式查找资源。
< XML版本=1.0编码=UTF-8&GT?;
<资源>
<字符串名称=VALUE1/>我串LT; /串>
<字符串名称=VALUE2/>我的字符串2'; /串>
。
。
<字符串名称=VALUE10/>我串3'; /串>
< /资源>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
编辑:只是供将来参考,这是工作最适合我的解决方案:
公开枚举MyEnum {
VALUE1,
VALUE2,
。
。
VALUE10;
/ **
*返回用于重新present此枚举值的本地化标签。如果没有标签
*已定义,那么这个默认的结果{@link枚举#名称()}。
*
*< P>将用于标签的字符串资源的名称必须与枚举的名称相匹配
* 值。例如,对于枚举值'ENUM1'的资源将被定义为R.string.ENUM1'。
*
* @参数上下文的标签的字符串资源是在上下文中。
返回:一个本地化的标签,枚举值或名称()的结果
* /
公共字符串getLabel(上下文的背景下){
资源RES = context.getResources();
INT渣油= res.getIdentifier(this.name(),串,context.getPackageName());
如果(0!=渣油){
返程(res.getString(渣油));
}
返程(姓名());
}
}
解决方案
您当然可以查找的资源通过其名称使用 Resources.getIdentifier()
。例如,对于您发布作为一个例子的字符串资源,你可以从一个活动做到这一点:
资源资源= getResources();
MyEnum E = MyEnum.VALUE1;
字符串本地化= res.getString(res.getIdentifier(e.name(),串,getPackageName()));
这是一个视图,你必须改变的最后一个参数的getContext()。getPackageName()
I have an enumeration where I need to display the values as localized strings. My current approach has been this:
public enum MyEnum {
VALUE1(R.string.VALUE1),
VALUE2(R.string.VALUE2),
.
.
VALUE10(R.string.VALUE10);
private int mResId = -1;
private MuEnum(int resId) {
mResId = resId;
}
public String toLocalizedString(Resource r) {
if (-1 != mResId) return (r.getString(mResId));
return (this.toString());
}
}
Is there any easier way to to do this? I'd love it if I could somehow lookup the resource based on the enumeration value name (i.e 'VALUE1').
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="VALUE1"/>My string</string>
<string name="VALUE2"/>My string 2</string>
.
.
<string name="VALUE10"/>My string 3</string>
</resources>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
EDIT: Just for future reference, this is the solution that worked best for me:
public enum MyEnum {
VALUE1,
VALUE2,
.
.
VALUE10;
/**
* Returns a localized label used to represent this enumeration value. If no label
* has been defined, then this defaults to the result of {@link Enum#name()}.
*
* <p>The name of the string resource for the label must match the name of the enumeration
* value. For example, for enum value 'ENUM1' the resource would be defined as 'R.string.ENUM1'.
*
* @param context the context that the string resource of the label is in.
* @return a localized label for the enum value or the result of name()
*/
public String getLabel(Context context) {
Resources res = context.getResources();
int resId = res.getIdentifier(this.name(), "string", context.getPackageName());
if (0 != resId) {
return (res.getString(resId));
}
return (name());
}
}
解决方案
You can certainly look up a resource by its name using Resources.getIdentifier()
. For instance, with the string resources you posted as an example, you can do this from an activity:
Resources res = getResources();
MyEnum e = MyEnum.VALUE1;
String localized = res.getString(res.getIdentifier(e.name(), "string", getPackageName()));
From a View, you'd have to change the last argument to getContext().getPackageName()
上一篇:Android的ViewPager - 页面的左侧和右侧显示preVIEW页面、Android、ViewPager、preVIEW
下一篇:与MediaRecorder类问题设置音频源 - setAudioSource() - 不支持的参数不支持、音频、参数、问题