2017-07-02 73 views
0

給定strings.xml發佈的值,如何獲取給定值的字符串標識符? 換句話說,讓我們假設有查看貼在下面,我們可以得到設定按鈕值如下:如何獲得給定值的字符串的標識符

mBtnDecisionAccepted = (Button) findViewById(R.id.actMain_btn_yes); 
mBtnDecisionAccepted.getText().toString() 

但是有可以返回這是

字符串的標識符的任何方法
decision_accepted 

佈局

<Button 
       android:id="@+id/actMain_btn_yes" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/decision_accepted" /> 

的strings.xml

<resources> 
    <string name="app_name">Test-TraverseThroughAView-1</string> 
    <string name="decision_accepted">eng: yes</string> 
    <string name="decision_rejected">eng: no</string> 
    <string name="decision_postponed">eng: change</string> 
</resources> 
+2

的可能的複製[安卓:我怎樣使用它的名字資源字符串?](https://stackoverflow.com/questions/7493287/android-how-do-i-get-string-從資源使用它的名字) –

+0

@LetsamrIt你有沒有得到這個工作......如果解決方案是由下面的答案提供的,你可以請接受它,讓其他人知道它已被回答。 –

回答

0
int resId = getResources().getIdentifier(youString, "string", packageName); 
0

你應該使用context.getResources()則getIdentifier()。

private String getStringResourceByName(String aString) { 
    String packageName = getPackageName(); 
    int resId = getResources().getIdentifier(aString, "string", packageName); 
    return getString(resId); 
    } 
相關問題