java 如何将字符串转换为其资源 ID (Android Studio)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34957155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert String to its resource ID (Android Studio)
提问by gciluffo
I have an xml string in my values/strings.xml
file
我的values/strings.xml
文件中有一个 xml 字符串
<string name="pokemon_d">150</string>
And I have the String "150"
in my controller MainActivity.java
. In my MainActivity
, how can I convert that String
to the resource ID of the pokemon_d String in the xml file? Is this even possible?
"150"
我的控制器中有 String MainActivity.java
。在我的 .xml 文件中MainActivity
,如何将其转换String
为 pokemon_d 字符串的资源 ID?这甚至可能吗?
回答by user370305
You can not get identifier by value, but you can make your identifier name look like a value and get it by string name,
您不能通过值获取标识符,但您可以使标识符名称看起来像一个值并通过字符串名称获取它,
So what I suggest,
所以我的建议是
use your String resource name something like, resource_150
使用您的字符串资源名称,例如, resource_150
<string name="resource_150">150</string>
<string name="resource_150">150</string>
Now here resource_
is common for your string entries in string.xml
file, so
in your code,
现在文件中resource_
的字符串条目很常见string.xml
,因此在您的代码中,
String value = "150";
int resourceId = this.getResources().
getIdentifier("resource_"+value, "string", this.getPackageName());
Now resourceId
value is as equivalent to R.string.resource_150
现在resourceId
价值相当于R.string.resource_150
Just make sure here this
represent your application context. In your case MainActivity.this
will work.
只需确保此处this
代表您的应用程序上下文。在你的情况下MainActivity.this
会起作用。
回答by Dave S
EDIT
编辑
You can't search for a resource Id by the string value. You could make your own Map of the values and resourceIds and use that as a look up table, but I believe just choosing an intelligent naming convention like in the accepted answer is the best solution.
您不能通过字符串值搜索资源 ID。您可以制作自己的值和资源 ID 映射并将其用作查找表,但我相信只选择像已接受的答案中那样的智能命名约定是最好的解决方案。
回答by Gerard Frijters
I have found some tips here: Android, getting resource ID from string?
我在这里找到了一些提示: Android, 从字符串中获取资源 ID?
Below an example how to get strings and their values defined in strings.xml. The only thing you have to do is making a loop and test which string is holding your value. If you need to repeat this many times it might be better to build an array. //---------
下面是如何获取strings.xml 中定义的字符串及其值的示例。您唯一要做的就是创建一个循环并测试哪个字符串保存您的值。如果您需要多次重复此操作,最好构建一个数组。//---------
String strField = "";
int resourceId = -1;
String sClassName = getPackageName() + ".R$string";
try {
Class classToInvestigate = Class.forName(sClassName);
Field fields[] = classToInvestigate.getDeclaredFields();
strField = fields[0].getName();
resourceId = getResourceId(strField,"string",getPackageName());
String test = getResources().getString(resourceId);
Toast.makeText(this,
"Field: " + strField + " value: " + test ,
Toast.LENGTH_SHORT).show();
} catch (ClassNotFoundException e) {
Toast.makeText(this,
"Class not found" ,
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(this,
"Error: " + e.getMessage() ,
Toast.LENGTH_SHORT).show();
}
}
public int getResourceId(String pVariableName, String pResourcename, String pPackageName)
{
try {
return getResources().getIdentifier(pVariableName, pResourcename, pPackageName);
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}