是否有针对Android的枚举字符串资源查找模式?

jsm*_*ith 36 enums android android-resources

我有一个枚举,我需要将值显示为本地化字符串.我目前的做法是:

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(Resources r) {
        if (-1 != mResId) return (r.getString(mResId));
        return (this.toString());
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有更简单的方法来做到这一点?如果我能以某种方式根据枚举值名称(即'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>
Run Code Online (Sandbox Code Playgroud)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

编辑:为了将来参考,这是最适合我的解决方案:

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());
    }
}
Run Code Online (Sandbox Code Playgroud)

Ted*_*opp 23

您当然可以使用其名称查找资源Resources.getIdentifier().例如,使用您发布的字符串资源作为示例,您可以通过以下活动执行此操作:

Resources res = getResources();
MyEnum e = MyEnum.VALUE1;
String localized = res.getString(res.getIdentifier(e.name(), "string", getPackageName()));
Run Code Online (Sandbox Code Playgroud)

从视图中,您必须将最后一个参数更改为 getContext().getPackageName()


dsh*_*rew 7

我认为你所尝试的是好的,除非你不需要每次想要翻译枚举时都将资源参数传递给枚举.


使用该链接子类化Application Class,然后按照这种方法.

改善方案

import android.app.Application;

public enum MyEnum {

    VALUE1(R.string.VALUE1),
    VALUE2(R.string.VALUE2),
    .
    .
    VALUE10(R.string.VALUE10);

    private int resourceId;

    private MyEnum(int id)  {
        resourceId = id;
    }


    @Override
    public String toString() {

        return MyApplication.getApplicationContext().getString(resourceId);

    }

}
Run Code Online (Sandbox Code Playgroud)

然后调用MyEnum.VALUEx将始终为您提供已翻译的枚举值,但请注意,这可能不是您想要的总是,例如,您可能有这样的原始查询:

select * from Customer where userStatus = MyEnum.VALUEx.toString();
Run Code Online (Sandbox Code Playgroud)

这可能会破坏您的应用程序,如果您将枚举值存储为数据库中的VALUE1,VALUE2 ...,请记住MyEnum.VALUEx.name()在您不想使用您的翻译值时使用此值MyEnum.

select * from Customer where userStatus = MyEnum.VALUEx.name();
Run Code Online (Sandbox Code Playgroud)


Oas*_*eng 5

使用静态 Application 总是不好的做法,因为它不仅破坏了 Instant Run,而且这违背了编程的解耦原则,从而使得模块化难以实现。更不用说Android实际上在单个进程中支持多个应用程序。

出于这个原因,我建议为要在运行时创建的枚举定义一个内部类,无论何时可能更改语言环境。

enum Example {
    A(R.string.label_a),
    B(R.string.label_b);

    Example(@StringRes int label) { mLabel = label; }
    private @StringRes int mLabel;

    class Entry {
        private Context mContext;
        Entry(final Context context) { mContext = context; }
        @Override public String toString() { return mContext.getString(mLabel); }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,构建 Example.Entry 实例或 Example.Entry 数组来表示原始枚举的本地化版本。

Example.A.new Entry(context);

Arrays.stream(Example.values()).map(item -> item.new Entry(context)).toArray(Example.Entry[]::new)
Run Code Online (Sandbox Code Playgroud)