在字符串资源文件中格式化语句

181

我在通常的strings.xml资源文件中定义了这样的字符串:

<string name="hello_world"> HELLO</string>

是否有可能定义像下面这样的格式字符串

 result_str = String.format("Amount: %.2f  for %d days ",  var1, var2);

在strings.xml资源文件中吗?

我尝试转义特殊字符,但没有成功。


1
你可以尝试以下方式:mTextView.setText(String.format("得分: "+"%1$s", runs)); 其中 int runs = 100; - Ganesh
5个回答

320
你不需要在XML中使用formatted="false",你只需要使用完全限定的字符串格式标记-%[POSITION]$[TYPE](其中[POSITION]是属性位置,[TYPE]是变量类型),而不是短版本,例如%s%d
引用自Android文档:字符串格式化和样式:
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
在这个例子中,格式化字符串有两个参数:%1$s 是一个字符串,%2$d 是一个十进制整数。你可以像这样使用应用程序中的参数来格式化字符串:
Resources res = getResources();
String text = res.getString(R.string.welcome_messages, username, mailCount);

1
当我使用$ d时,我会收到java.util.IllegalFormatConversionException错误:“%d无法格式化java.lang.Double参数”,我认为$ d是一个整数。 - user1634451
7
以下是所有不同类型的转换器列表,您需要根据数字类型选择合适的转换器,对于浮点数可能需要使用 %f。参考链接:http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html - LocalPCGuy
2
res.getString(R.string.welcome_messages, username, mailCount) 就可以完成任务。资源/上下文的 getString 方法包括格式化功能。 - CrepuscularV
1
最佳且直接的解决方案。 - Dennis Gonzales

109

你应该在字符串资源中添加formatted="false"


这是一个例子

在你的strings.xml文件中:

<string name="all" formatted="false">Amount: %.2f%n  for %d days</string>

在你的代码中:

yourTextView.setText(String.format(getString(R.string.all), 3.12, 2));

6
根据“格式化字符串”的文档,这并非必需。参见:http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling - Squonk
这是一种修复的方法,但对于某些人可能会感到困惑,因为formatted="false"可能暗示着字符串不会被格式化。在https://dev59.com/J2cs5IYBdhLWcg3wtmWf#20887690上发布了另一个解决方案。 - LocalPCGuy
20
请注意,您可以这样简化String.format(getString(R.string.all), 3.12, 2)getString(R.string.all, 3.12, 2) - patryk.beza
这个页面上所有答案都存在一个主要问题:如果你需要在参数旁边添加一个百分号,比如 25%,Android 就会崩溃。 - Henrique de Sousa
4
您可以通过输入两个百分号来转义百分号。以下是标准格式语法示例:String.format("Percent %d%% or as float %.2f%%", 12, 12.34f); 将生成一个字符串"Percent 12% or as float 12.34%"。 - LanDenLabs
显示剩余3条评论

16

在文件strings.xml中定义字符串资源如下:

<string name="string_to_format">Amount: %1$f  for %2$d days%3$s</string>

在您的代码中(假设它继承自Context),只需执行以下操作:

 String formattedString = getString(R.string.string_to_format, floatVar, decimalVar, stringVar);

(相比于LocalPCGuyGiovanny Farto M. 的答案,这里不需要使用 String.format 方法。)


9

引用自Android文档

If you need to format your strings using String.format(String, Object...), then you can do so by putting your format arguments in the string resource. For example, with the following resource:

<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>

In this example, the format string has two arguments: %1$s is a string and %2$d is a decimal number. You can format the string with arguments from your application like this:

Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);

4

对我而言,在 Kotlin 中,它的运行方式如下:

我的 string.xml 文件

 <string name="price" formatted="false">Price:U$ %.2f%n</string>

我的class.kt

 var formatPrice: CharSequence? = null
 var unitPrice = 9990
 formatPrice = String.format(context.getString(R.string.price), unitPrice/100.0)
 Log.d("Double_CharSequence", "$formatPrice")

D/Double_CharSequence: 价格:U$ 99,90

为了获得更好的结果,我们可以这样做

 <string name="price_to_string">Price:U$ %1$s</string>

 var formatPrice: CharSequence? = null
 var unitPrice = 199990
 val numberFormat = (unitPrice/100.0).toString()
 formatPrice = String.format(context.getString(R.string.price_to_string), formatValue(numberFormat))

  fun formatValue(value: String) :String{
    val mDecimalFormat = DecimalFormat("###,###,##0.00")
    val s1 = value.toDouble()
    return mDecimalFormat.format(s1)
 }

 Log.d("Double_CharSequence", "$formatPrice")

D/Double_CharSequence: 价格:U$ 1.999,90


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接