2011-05-02 58 views

回答

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


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

例子是,你可以使用。 将标签添加到string.xml文件后,然后重新启动.java文件中的相同内容,您可以按照此操作。 。

AndriodApp

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

+32

仅供参考,Resources.getString(INT渣油,对象... formatArgs)类似的String.format(),另一种方法,这需要资源ID和Object参数。让你跳过最后一步。 – Josh 2011-05-02 17:03:42

+8

标志的文档:http://developer.android.com/reference/java/util/Formatter.html – 2013-09-09 08:27:56

-2

采取

33

只需将它作为formatArgs对象传递给getString()函数即可。

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

XML:

<string name="next_x_results">Next %1$d results</string> 
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);