[英]How to textview2 getText from String resource by my textView1 is equal string name
"If the value in textView1 is equal string name I want string to show textView2" // String Example "如果 textView1 中的值等于字符串名称,我希望字符串显示 textView2" // 字符串示例
<string name="a">Apple</string>
<string name="b">Banana</string>
<string name="c">Car</string>
//Example if textView1 = a textView2 Will Show Apple //示例 if textView1 = a textView2 Will Show Apple
Here you have stringName
as a text in your textView1
.在这里,您将stringName
作为textView1
中的文本。 You can fetch String value
programatically from resourses by stringName
.您可以通过stringName
以编程方式从资源中fetch String value
。
Step1
: first fetch this both values, and store it in a variable; Step1
:首先获取这两个值,并将其存储在一个变量中;
String mTvTextStrName = textView1.getText().toString().trim();
String strValue = getStringResourceByName(mTvTextStrName);
use this method to fetch String value.使用此方法获取字符串值。
private String getStringResourceByName(String aString) {
String packageName = getPackageName();
int resId = getResources().getIdentifier(aString, "string", packageName);
return getString(resId);
}
You can also check this values via Log.e
.您还可以通过Log.e
检查此值。
Step2
: Now set your strValue
in your textView2
.第 2 textView2
:现在在您的Step2
中设置您的strValue
。
// Do your code.. Show Apple
textView2.setText(strValue);
Try using getIdentifier() method of getResources() like below.尝试使用 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);
Referenced from https://stackoverflow.com/a/17086690/9502601引用自https://stackoverflow.com/a/17086690/9502601
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.