如何将 TextView 的文本设置为字符串资源?(安卓的Java)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28504673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I set text of TextView to a string resource? (Java for android)
提问by levente
I would like to change the text of a TextView
component to another text, that I have already created in strings.xml
. When the app starts, the text shown is stored in strings.xml
, under the name "help0". I would like to set this to the string under the name "help00" programmatically, by placing another "0" at the and of the name. So far I have written this code :
我想将一个TextView
组件的文本更改为另一个文本,我已经在strings.xml
. 当应用程序启动时,显示的文本存储在strings.xml
名称“help0”下。我想通过在名称的 and 处放置另一个“0”,以编程方式将其设置为名称为“help00”的字符串。到目前为止,我已经编写了这段代码:
String help = "0";
help = help + "0";
final TextView help = (TextView) findViewById(R.id.help);
help.setText("@string/help" + help);
However, when I run the app the text changes to "@string/help00", instead of the text I have stored under help00 in strings.xml
.
但是,当我运行该应用程序时,文本更改为“@string/help00”,而不是我在 help00 下存储的文本strings.xml
。
How could I fix this problem?
我该如何解决这个问题?
回答by Marcus
Try this
尝试这个
String help = "0";
final TextView tvHelp = (TextView) findViewById(R.id.help);
String yourString = getResources().getString(R.string.help);
tvHelp.setText(yourString +help)
回答by user370305
Because You have concatted String resource id with normal String help
So its worked as a String
. You have to get first resource string from android resource and then need to concat with local String variable, like,
因为您已经将 String 资源 id 与普通 String 连接起来,help
所以它作为String
. 您必须从 android 资源中获取第一个资源字符串,然后需要使用本地 String 变量进行连接,例如,
help.setText(getResources().getString(R.string.help)+help);
help.setText(getResources().getString(R.string.help)+help);
As I doubt, if you are looking for dynamically String id, You want String resource id with already exist id and '0' append to it then get int resource id from String using..
我怀疑,如果您正在寻找动态字符串 id,您想要字符串资源 id 已经存在的 id 和 '0' 附加到它然后使用 ..
int resourceId = this.getResources().getIdentifier("@string/help" + help, "string", this.getPackageName());
and then
进而
help.setText(resourceId);
help.setText(resourceId);
回答by Waddah Mustafa
I believe that strings.xml resource file content cannot be edited during runtime. now i am assuming u have the following on your strings.xml file
我相信在运行时无法编辑 strings.xml 资源文件内容。现在我假设你的 strings.xml 文件中有以下内容
<resources>
<string name="app_name">Application Name</string>
<string name="help0">Text1</string>
<string name="hellp00">Text2</string>
</resources>
if u want to change the text on your TextView (id=@+id/textview) from the value stored as "help0",to value stored in "help00" use the following code:
如果您想将 TextView (id=@+id/textview) 上的文本从存储为“help0”的值更改为存储在“help00”中的值,请使用以下代码:
String text= getString(R.string.help00);
TextView myTextView = findViewById(R.id.textview);
myTextView.setText(text);