简体   繁体   English

Android setText / R.string / values

[英]Android setText / R.string / values

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) 我甚至试过String.valueOf(R.string.mycar); getResources().getText(R.String.mycar) 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 如果有人可以帮助我,那将是很好的,谢谢

试试这个

holder.car.setText(getResources().getString(R.string.mycar));

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年

R.string.mycar and R.string.year are only IDs for resources. R.string.mycar和R.string.year只是资源的ID。 For this reason you get the numbers (IDs are numeric). 因此,您可以获得数字(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. 方法getResources()属于Context。 If you run your code outside an Activity, use the context instance to get the string, like this: 如果在Activity外部运行代码,请使用上下文实例获取字符串,如下所示:

String myCar = context.getResources().getString(R.string.mycar);

You have to retrieve your resources first, and the call the medthod getString(int), not getText, has you have put. 你必须首先检索你的资源,并调用medthod getString(int),而不是getText,你已经放了。

So, it should be: 所以,它应该是:

getResources().getString(R.String.mycar);

Try this. 试试这个。 If you're fetching string in class without extending Activity Get using your Context 如果您在类中提取字符串而不使用Context扩展Activity Get

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.. 希望这可以帮助你..

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) 如果你想用一些字符串或整数的组合设置textview,(比第一种方式更好)

int number=5;

mytextview.setText(getResources().getString(R.String.mycar) + " " +number + " " + getResources().getString(R.String.mysecondcar));

R类包含指向你的资源的指针,所以你不能直接使用它们,使用getResources()。getString(),正如其他人所说。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM