我想从 strings.xml 获取一个字符串。我知道该怎么做。但我的问题是别的:
我有一个每次都会更改的字符串变量,每次更改时,我想查看 strings.xml 并检查该字符串变量是否存在于 Strings.xml 中,然后获取文本。
例如:
String title="sample title" \\ which changes
String city= "sample city"
String s = getResources().getString(R.string.title);
第三行:title是一个String,并且Strings.xml中没有任何名为String的“title”
我怎样才能做到这一点?请帮我
据我所知,你可以使用public int getIdentifier (String name, String defType, String defPackage) http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier%28java.lang.String,%20java.lang.String,%20java.lang.String%29。但不鼓励使用它。
要使用它(我没有这样做,但我曾经读过该方法),您可能需要:
int identifier = getResources().getIdentifier ("title","string","your.package.name.here");
if (identifier!=0){
s=getResources().getString(identifier);
}
else{
s="";//or null or whatever
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)