"如果 textView1 中的值等于字串名稱,我希望字串顯示 textView2" // 字串示例
<string name="a">Apple</string>
<string name="b">Banana</string>
<string name="c">Car</string>
//示例 if textView1 = a textView2 Will Show Apple
uj5u.com熱心網友回復:
在這里stringName,您的textView1. 您可以fetch String value通過 以編程方式從資源中獲取stringName。
Step1:首先獲取這兩個值,并將其存盤在變數中;
String mTvTextStrName = textView1.getText().toString().trim();
String strValue = getStringResourceByName(mTvTextStrName);
使用此方法獲取字串值。
private String getStringResourceByName(String aString) {
String packageName = getPackageName();
int resId = getResources().getIdentifier(aString, "string", packageName);
return getString(resId);
}
您還可以通過 來檢查此值Log.e。
Step2: 現在設定你strValue的textView2.
// Do your code.. Show Apple
textView2.setText(strValue);
uj5u.com熱心網友回復:
嘗試使用 getResources() 的 getIdentifier() 方法,如下所示。
TextView textView1= findViewById(R.id.textView1);
TextView textView2= findViewById(R.id.textView2);
String textViewText=getResources().getString(getResources().getIdentifier(textView1.getText().toString(), "string", getPackageName()));
textView2.setText(textViewText);
參考自https://stackoverflow.com/a/17086690/9502601
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/490473.html
上一篇:哪個組件指定活動的入口點?
