是否可以将变量放在字符串资源中?如果是的话 - 我如何使用它们。如何把变量内部的字符串资源?
我需要的是以下几点:
<string name="next_x_results">Next %X results</string>
,并把一个int代替%X的。从here
是否可以将变量放在字符串资源中?如果是的话 - 我如何使用它们。如何把变量内部的字符串资源?
我需要的是以下几点:
<string name="next_x_results">Next %X results</string>
,并把一个int代替%X的。从here
<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);
采取
只需将它作为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>
<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);
仅供参考,Resources.getString(INT渣油,对象... formatArgs)类似的String.format(),另一种方法,这需要资源ID和Object参数。让你跳过最后一步。 – Josh 2011-05-02 17:03:42
标志的文档:http://developer.android.com/reference/java/util/Formatter.html – 2013-09-09 08:27:56