"如果 textView1 中的值等于字符串名称,我希望字符串显示 textView2" // 字符串示例
<string name="a">Apple</string>
<string name="b">Banana</string>
<string name="c">Car</string>
//示例 if textView1 = a textView2 Will Show Apple
在这里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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句