动态获取字符串值 [英] Get String Value Dynamically

查看:86
本文介绍了动态获取字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过putExtra()和getExtra()将一个活动中的值传递给另一活动,并获得如下值:

I am passing a value from one activity to another through putExtra() and getExtra() and getting value like:

活动1:

intent = new Intent(this, blankScrollActivity.class);
intent.putExtra("alphabets", "a");

活动2:blankScrollActivity.class

Bundle extras = getIntent().getExtras();
String varName = extras.getString("alphabets");

TextView textView = (TextView) findViewByid(R.id.blankTextView);

字符串资源 MyString.xml :

<string name="a">My Example 1</string>
<string name="b">My Example 2</string>

我想动态获取string的值以分配textView.

I want to get the value of string dynamically to assign textView.

推荐答案

Bundle extras = getIntent().getExtras();
String varName = extras.getString("alphabets");

int resId = getResources().getIdentifier(varName , "string", getPackageName());
String data = getResources().getString(resId);

这样,您将基于从第一个活动发送的 String 值获得所需的 String资源.

This way, you will get the String resource that you want based on the String value sent from the first Activity.

这篇关于动态获取字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆