如何将变量放入字符串资源中?

时间:2022-02-08 07:12:22

Is it possible to put variables inside string-resources? And if yes - how do i use them.

是否可能将变量放入字符串资源中?如果是,我该如何使用它们。

What i need is the following:

我所需要的是:

<string name="next_x_results">Next %X results</string>

<字符串名称= " next_x_results> % X结果旁边< /字符串>

and put an int in place of the %X.

然后用int代替%X。

4 个解决方案

#1


75  

<string name="meatShootingMessage">You shot %1$d pounds of meat!</string>  


int numPoundsMeat = 123;
String strMeatFormat = getResources().getString(R.string.meatShootingMessage, numPoundsMeat);

Example taken from here

例子来自这里

#2


34  

Just pass it throught getString() function as formatArgs Object.

只需将它作为formatArgs对象通过getString()函数传递。

int nextResultsSize = getNextResultsSize();
String strNextResultsSize = 
     getResources().getString(R.string.next_x_results, nextResultsSize);

XML:

XML:

<string name="next_x_results">Next %1$d results</string>

#3


3  

<string name="meatShootingMessage">You shot %1$d pounds of meat! Put Second Variable String here %$s and third variable integer here %3$d</string>
int intVariable1 = 123;
int stringVariable2 = "your String";
int intVaribale3 = 456;
String strMeatFormat = getResources().getString(R.string.meatShootingMessage, intVariable1, stringVariable2 , intVaribale3);

#4


-2  

yes,you can use. after adding tag to string.xml file and then to retrrive the the same in your .java file you can follow this.

是的,你可以使用。将标签添加到字符串之后。xml文件,然后在.java文件中retrrive相同的内容,您可以遵循以下步骤。

AndriodApp

AndriodApp

String str = getResources().getString(R.string.name);

字符串str = getresource().getString(R.string.name);

#1


75  

<string name="meatShootingMessage">You shot %1$d pounds of meat!</string>  


int numPoundsMeat = 123;
String strMeatFormat = getResources().getString(R.string.meatShootingMessage, numPoundsMeat);

Example taken from here

例子来自这里

#2


34  

Just pass it throught getString() function as formatArgs Object.

只需将它作为formatArgs对象通过getString()函数传递。

int nextResultsSize = getNextResultsSize();
String strNextResultsSize = 
     getResources().getString(R.string.next_x_results, nextResultsSize);

XML:

XML:

<string name="next_x_results">Next %1$d results</string>

#3


3  

<string name="meatShootingMessage">You shot %1$d pounds of meat! Put Second Variable String here %$s and third variable integer here %3$d</string>
int intVariable1 = 123;
int stringVariable2 = "your String";
int intVaribale3 = 456;
String strMeatFormat = getResources().getString(R.string.meatShootingMessage, intVariable1, stringVariable2 , intVaribale3);

#4


-2  

yes,you can use. after adding tag to string.xml file and then to retrrive the the same in your .java file you can follow this.

是的,你可以使用。将标签添加到字符串之后。xml文件,然后在.java文件中retrrive相同的内容,您可以遵循以下步骤。

AndriodApp

AndriodApp

String str = getResources().getString(R.string.name);

字符串str = getresource().getString(R.string.name);