[英]static final CharSequence to R.string.xxx how to do it properly?
[英]How to access R.string.xxx resources from a method by passing string 'xxx' as parameter to that method?
我的res/values/strings.xml
有很多字符串元素
所以,我想要一個方法getString(String abc)
從strings.xml
檢索strings.xml
:
public String getString(String abc){ // abc = address1
String result;
result = context.getResources().getString(R.strings.+abc);
}
如何基於參數中的String訪問此方法中的字符串元素?
public String getString(String abc){ // Ex. abc = "address1"
int resID = getResources().getIdentifier(abc, "string", getPackageName());
return context.getResources().getString(resID);
}
String abc="StringId";
int resID = getResources().getIdentifier(abc, "string", getPackageName());
嘗試這個:
Resources res = this.getResources();
int resID = res.getIdentifier(imagename, "drawable", this.getPackageName());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.