2012-08-02 114 views
1

我想動態地更改某些按鈕上的文本。 所有必要的文本都保存在strings.xml中,引用名稱保存在Json對象中。Android:動態按鈕名稱

任何人都知道我可以從我的setText函數的strings.xml中獲取數據嗎?

JSONObject e = Options.getJSONObject(i); 
//The Name for strings.xml 
String name = e.getString("name"); 

Resources res = getResources(); 
button1.setText(String.format(res.getString(R.string. ???????????))); 
+0

的HTTP重複得到String.xml的任何字符串/stackoverflow.com/questions/6157887/r-string-get-string-from-dynamic-k EY-名 – kgautron 2012-08-02 20:03:57

回答

2

使用這樣的

getResources().getIdentifier(name, "string", 「com.main.package」)); 

或更好:/:

getResources().getIdentifier(name, "string", getPackageName())); 
0

您可以通過

getString(R.string.string_name);