问题描述
我有一个问题再次。一个Android应用程序的资源:
我的申请杂项。模式(编辑/策划/执行),这是我想说明使用枚举类型。我会,不过,像填充枚举值与字符串从资源string.xml文件茎,即代替
枚举模式{
编辑(编辑),
计划(计划),
EXEC(EXEC);
字符串名称;
模式(字符串名称){this.name =名称; }
@覆盖
公共字符串的toString(){返回this.name; }
};
我想写点东西,如:
枚举模式{
编辑(getResources()。的getText(R.string.mode_edit)的ToString()),
计划(getResources()。的getText(R.string.mode_plan)的ToString()))
EXEC(getResources()的getText(R.string.mode_exec)的ToString()));
字符串名称;
模式(字符串名称){this.name =名称; }
@覆盖
公共字符串的toString(){返回this.name; }
}
这将如允许使用资源文件来修改模式的名字,从而允许后名称变更没有code的变化,国际化等。
的问题是,该标准访问资源是通过一个活动的getResources() - 方法,这是仅在构造可用的(和在实例方法)。枚举声明,然而,是一类的静态初始化code部分。有没有什么办法来访问应用程序的资源在一个静态的方式?
迈克尔
枚举模式{
编辑(R.string.mode_edit)
PLAN(R.string.mode_plan)
EXEC(R.string.mode_exec);
字符串ID;
模式(字符串ID){this.id = ID; }
公共字符串的getName(资源R){返回r.getText(ID); }
@覆盖
公共字符串的toString(){返回this.name; }
}
另外,您可以做到以下几点:
公共类类名{
公共静态资源资源;
}
在你的 Application.onCreate()
或 Activity.onCreate()
:
ClassName.res = getResources();
和您的枚举模式
:
@覆盖
公共字符串的toString(){
资源资源= ClassName.res;
如果(RES == NULL){返回super.toString(); }
其他{返回res.getText(ID); }
}
I have a problem re. an Android application's resources:
My application has misc. modes (edit/plan/exec), which I would like to describe using an enumeration type. I would, however, like to populate the enumeration-values with strings that stem from the resource string.xml file, i.e. instead of
enum Mode {
EDIT ("edit"),
PLAN ("plan"),
EXEC ("exec");
String name;
Mode(String name) { this.name = name; }
@Override
public String toString() { return this.name; }
};
I would like to write something like:
enum Mode {
EDIT (getResources().getText(R.string.mode_edit).toString()),
PLAN (getResources().getText(R.string.mode_plan).toString())),
EXEC (getResources().getText(R.string.mode_exec).toString()));
String name;
Mode(String name) { this.name = name; }
@Override
public String toString() { return this.name; }
}
which would e.g. allow to modify the modes' names using the resource file and thus allow for later name modifications without code changes, internationalization, etc.
The problem is, that the standard access to the resources is via an Activity's getResources()-method which is only available in the constructor (and during instance methods). The enumeration declaration, however, is part of a class' static initialization code. Is there any way to access an app's resources in a static way?
Michael
enum Mode {
EDIT (R.string.mode_edit),
PLAN (R.string.mode_plan),
EXEC (R.string.mode_exec);
String id;
Mode(String id) { this.id = id; }
public String getName(Resources r){ return r.getText(id); }
@Override
public String toString() { return this.name; }
}
Alternatively you can do following:
public class ClassName {
public static Resources res;
}
In your Application.onCreate()
or Activity.onCreate()
:
ClassName.res = getResources();
and in your enum Mode
:
@Override
public String toString() {
Resources res = ClassName.res;
if (res==null){ return super.toString(); }
else { return res.getText(id); }
}
这篇关于一个Android应用程序的资源静态访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!