按名称获取资源 [英] Get Resource by Name
本文介绍了按名称获取资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
海兰!
我在/values/strings.xml:
I have in my /values/strings.xml:
<resources>
<string name="error418">Bad Request</string>
<string name="error404">Not Found</string>
......
</resources>
在/values-de/strings.xml相同
the same in /values-de/strings.xml
现在我想通过字符串得到错误信息error418。
Now i want to get the error messages by the string "error418".
我只找到context.getString(INT ...)。
I only found context.getString(int ...).
如何通过code,以获得错误信息?
How to get the error message by code?
推荐答案
如果你想获得资源ID动态,根据错误code例如,你需要使用<一个href=\"http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier%28java.lang.String,%20java.lang.String,%20java.lang.String%29\"相对=nofollow>则getIdentifier()
If you want to get resource id dynamically, according to error code for example, you need to use getIdentifier()
例如
String err_code = ...;
int strId = context.getResources().getIdentifier("error" + err_code, "string", "packagename");
if (strId != 0)
Toast.maketext(context, context.getString(strId), Toast.LENGTH_LONG).show();
else
Toast.maketext(context, "Unknown error", Toast.LENGTH_LONG).show();
这篇关于按名称获取资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文