2011-05-02 67 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); 
相關問題