鉴于strings.xml发布了belwo,如何获取给定值的字符串的标识符? 换句话说,假设我们在下面发布了View,我们可以按如下方式获取设置为Button的值:
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>
答案 0 :(得分:0)
int resId = getResources().getIdentifier(youString, "string", packageName);
答案 1 :(得分:0)
你应该使用 context.getResources()。getIdentifier();
private String getStringResourceByName(String aString) {
String packageName = getPackageName();
int resId = getResources().getIdentifier(aString, "string", packageName);
return getString(resId);
}