Android setText / R.string / values
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11536326/
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
Android setText / R.string / values
提问by S Arumik
I am having trouble with setting text in a text view with format and multiple values.
我在使用格式和多个值的文本视图中设置文本时遇到问题。
holder.car.setText(R.string.mycar + lm.getCarName() + R.string.year + lm.getYear());
this is giving me " 2143545 Camero 2143213 1977 "
这给了我“2143545 Camero 2143213 1977”
I have tried few other "solutions" from the web
我从网上尝试了一些其他“解决方案”
holder.car.setText(getString(R.string.mycar) + lm.getCarName() + getString(R.string.year) + lm.getYear()); << not work, getString undefine>>
I even tried String.valueOf(R.string.mycar); getResources().getText(R.String.mycar)
, still it didn't work.
我什至尝试过String.valueOf(R.string.mycar); getResources().getText(R.String.mycar)
,仍然没有奏效。
It would be great if someone can help me, thanks
如果有人可以帮助我,那就太好了,谢谢
回答by user1417127
Try this
尝试这个
holder.car.setText(getResources().getString(R.string.mycar));
holder.car.setText(getResources().getString(R.string.mycar));
回答by Benito Bertoli
I think you're trying to use parameters in your string.
我认为您正在尝试在字符串中使用参数。
Try this:
尝试这个:
<string name="mycar">Car: %1$s Year: %2$s</string>
String mycar = getString(R.string.mycar);
mycar = String.format(mycar, lm.getCarName(), lm.getYear());
You should get:
你应该得到:
Car: Camaro Year: 1977
汽车:Camaro 年份:1977
回答by G?khan Sar?kaya
If you want to set your textview just a string from your string.xml file,
如果您只想将 textview 设置为 string.xml 文件中的一个字符串,
mytextview.setText(R.String.mycar);
If you want to set your textview with combination of some strings or integers, (better than first way)
如果你想用一些字符串或整数的组合来设置你的文本视图,(比第一种方式更好)
int number=5;
mytextview.setText(getResources().getString(R.String.mycar) + " " +number + " " + getResources().getString(R.String.mysecondcar));
回答by Zelter Ady
R.string.mycar and R.string.year are only IDs for resources. For this reason you get the numbers (IDs are numeric).
R.string.mycar 和 R.string.year 只是资源的 ID。因此,您会得到数字(ID 是数字)。
To get string from resources you need to use this construction:
要从资源中获取字符串,您需要使用此构造:
String myCar = getResources().getString(R.string.mycar);
and now the myCar variable holds the string you put in strings.xml file under the mycar name.
现在 myCar 变量保存了您在 mycar 名称下放入 strings.xml 文件中的字符串。
the method getResources() belongs to Context. If you run your code outside an Activity, use the context instance to get the string, like this:
方法 getResources() 属于 Context。如果在 Activity 之外运行代码,请使用上下文实例获取字符串,如下所示:
String myCar = context.getResources().getString(R.string.mycar);
回答by Praveenkumar
Try this. If you're fetching string in class without extending Activity
Get using your Context
尝试这个。如果您在类中获取字符串而不Activity
使用您的扩展GetContext
holder.car.setText(context.getResources().getString(R.string.mycar));
If you're extending Activity
如果你要延长 Activity
holder.car.setText(yourActivity.this.getResources().getString(R.string.mycar));
Hope this helps you..
希望这对你有帮助..
回答by I?igo
You have to retrieve your resources first, and the call the medthod getString(int), not getText, has you have put.
您必须首先检索您的资源,然后调用方法 getString(int),而不是 getText,您已经放置了。
So, it should be:
所以,应该是:
getResources().getString(R.String.mycar);
回答by Rocel
The R class contains kind of pointers to your ressources, so you can not directly use them, use getResources().getString(), as others say.
R 类包含指向资源的指针,因此您不能直接使用它们,请使用 getResources().getString(),正如其他人所说。