Android 如何动态设置字符串资源

Android 如何动态设置字符串资源,android,android-resources,Android,Android Resources,我有一个edittext和按钮,当第二个活动从主活动接收文本时,它会将edittext文本传递给另一个活动onClick 我的问题是,我在字符串资源文件夹中有20多个字符串名称,我必须打开与主活动的edittext值匹配的特定字符串。 简而言之,我必须更改与字符串资源字符串匹配的textview文本 <resources> <string name="app_name">sylb</string> <string name

我有一个edittext和按钮,当第二个活动从主活动接收文本时,它会将edittext文本传递给另一个活动onClick

我的问题是,我在字符串资源文件夹中有20多个字符串名称,我必须打开与主活动的edittext值匹配的特定字符串。 简而言之,我必须更改与字符串资源字符串匹配的textview文本

<resources>
        <string name="app_name">sylb</string>
        <string name="title_activity_display">display</string>
        <string name="large_text">some_large_text</string>
        <string name="ds15mca21">second mca</string>
        <string name="ds15mca11">first mca</string>
        <string name="ds15mba21">second <b>mba</b>></string>
        <string name="ds15aut21">second <i>automobile</i></string>
        <string name="action_settings">Settings</string>
</resources>
使用下面的代码

getResources().getString(resId)//resId is R.string.STRING_NAME
你可以试试这个

textView.setText(getResources().getIdentifier("R.string." + "ds" + code,"string",getPackageName()));
这将完成你的工作! 只需传递您修复的正确字符串,而不是app_name。不要使用R.String,只需传递资源名称

示例:如果我想得到

 <string name="app_name">ItsCharuHereHiFolks</string>


这真的很容易。在课堂上使用这个

private String getStringResourceByName(String aString) {
      String packageName = getPackageName();
      int resId = getResources().getIdentifier(aString, "string", packageName);
      return getString(resId);
    }
简单地使用这个

textView.setText(getStringResourceByName("ds" + code));
或者您可以简单地使用上述类并提取任何字符串

范例

如果您需要以下资源中的问题

问题

用这个

String question=getStringResourceByName("questions_en");
此外,在getStringResourceByName中使用整数而不是字符串 方法获取集成资源

做这项工作


.

虽然@Charu的答案很好,并且足够高效,可以执行id属于不同资源类型的一般任务,但如果您知道字符串资源名称,并且您的任务仅与字符串相关,则仍然可以有更好的解决方案

第一步

在您的呼叫活动中,使用以下代码

Intent intent = new Intent(this, newActivity.class);
intent.putExtra("StringResourceId", R.string.dsAbcXyz); //this step will directly pass the int value of your string resource without any iterations
startActivity(intent);
Intent prevIntent = getIntent();
int resId = prevIntent.getIntExtra("StringResourceId");
textView.setText(resId); //you can directly pass the int id to textview to display the relevant text
步骤2

然后在新活动中,使用以下代码

Intent intent = new Intent(this, newActivity.class);
intent.putExtra("StringResourceId", R.string.dsAbcXyz); //this step will directly pass the int value of your string resource without any iterations
startActivity(intent);
Intent prevIntent = getIntent();
int resId = prevIntent.getIntExtra("StringResourceId");
textView.setText(resId); //you can directly pass the int id to textview to display the relevant text
当您非常确定要作为参数传递的ResourceXe名称时,这种方法可以工作

但如果您是通过随机源(如服务器)获取res名称的 或者数据库,那么我建议您遵循@Charu的解决方案

用这个

getResources().getIdentifier("ds" + code, "String", getPackageName());

textView.settexter.string.+ds+代码;这一行将普通文本显示为r.string.ds如果您想从字符串文件中设置字符串值,您应该使用我的答案!那么,你到底想做什么?请多解释!而不是将代码作为额外的字符串参数传递。将R.string.dsXYZ的值作为额外的int资源传递。然后在其他活动中获取该int并使用textView.setTextresInt@我不理解你!请解释更多,您建议他传递字符串资源的名称,然后从中计算字符串id,然后再次获取实际字符串的值,然后将其分配给textView?认真地要么传递属于该字符串资源的实际字符串,要么直接传递作为该字符串资源对应的额外参数的resId。@Mohammed Atif他的要求是使用字符串资源的名称获取字符串值。阅读问题,这就是他试图使用R.string做的事。+D他是个初学者,他可能不知道做简单任务的更好方法。这并不意味着你按原样回答他的问题,而不是帮助他找到更好的解决方案。@Mohammed Atif may getIdentifier是一个通用工具,因此速度很慢。与自己的R.java相比,运行n次迭代的性能更好,您需要一些学习,不要将方法与它们拥有的行数进行比较!这里有一些关于堆栈溢出的内容供您阅读,您通常会添加几句解释,而不仅仅是一行代码。。。
getResources().getIdentifier("ds" + code, "String", getPackageName());