如何通过我的textView1从字符串资源中textView2 getText进行等效字符串名称

发布于 2025-02-07 14:15:48 字数 256 浏览 2 评论 0原文

“如果TextView1中的值是等于字符串名称,我希望字符串显示TextView2” //字符串示例

<string name="a">Apple</string>
<string name="b">Banana</string>
<string name="c">Car</string>

//示例 如果textview1 = a textview2将显示苹果

"If the value in textView1 is equal string name I want string to show textView2"
// String Example

<string name="a">Apple</string>
<string name="b">Banana</string>
<string name="c">Car</string>

//Example
if textView1 = a
textView2 Will Show Apple

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撕心裂肺的伤痛 2025-02-14 14:15:48

在这里,您将StringName作为textView1的文本。
您可以通过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:现在在textView2中设置strvalue

// Do your code.. Show Apple
   textView2.setText(strValue);

Here you have stringName as a text in your textView1.
You can fetch String value programatically from resourses by stringName.

Step1 : first fetch this both values, and store it in a variable;

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.

Step2 : Now set your strValue in your textView2.

// Do your code.. Show Apple
   textView2.setText(strValue);
一枫情书 2025-02-14 14:15:48

尝试使用getIdentifier()getResources()的方法如下。

    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);

Try using getIdentifier() method of getResources() like below.

    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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文