【问题标题】:format statement in a string resource file字符串资源文件中的格式语句
【发布时间】:2012-09-27 18:02:48
【问题描述】:

我在通常的 strings.xml 资源文件中定义了字符串,如下所示:

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

是否可以定义如下格式字符串

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

在strings.xml 资源文件中?

我尝试转义特殊字符,但它不起作用。

【问题讨论】:

  • 您可以尝试以下方式: mTextView.setText(String.format("Score: "+"%1$s", runs));其中 int 运行 = 100;

标签: android android-resources


【解决方案1】:

您不需要在 XML 中使用 formatted="false"。您只需要使用完全限定的字符串格式标记 - %[POSITION]$[TYPE](其中 [POSITION] 是属性位置,[TYPE] 是变量类型),而不是短版本,例如 %s%d

引用Android Docs: String Formatting and Styling:

<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);

【讨论】:

  • 我得到 java.util.IllegalFormatConversionException: %d can't format java.lang.Double arguments when I use $d think $d is an integer
  • 这里是所有不同转换器的列表,您必须为数字类型选择合适的一个,您可能需要 %f(浮点数):docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html
  • res.getString(R.string.welcome_messages, username, mailCount) 将完成这项工作。资源/上下文的 getString 方法包括格式化功能。
【解决方案2】:

您应该将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));

【讨论】:

  • 根据格式化字符串的文档没有必要。 developer.android.com/guide/topics/resources/…
  • 这是一种修复方法,但可能会让一些人感到困惑,因为formatted="false" 可能暗示字符串没有被格式化。发布另一个解决方案stackoverflow.com/a/20887690/228429
  • 请注意,您可以这样简化String.format(getString(R.string.all), 3.12, 2)getString(R.string.all, 3.12, 2)
  • 这个页面的所有答案都有一个大问题:如果你需要在参数旁边添加一个百分号,比如25%,Android 会崩溃。
  • 您可以通过包含两个 %% 来转义百分号。以下标准格式语法。示例: String.format("百分比 %d%% 或作为浮点数 %.2f%%", 12, 12.34f);将生成一个字符串“Percent 12% or as float 12.34%”
【解决方案3】:

在文件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 方法。)

【讨论】:

    【解决方案4】:

    引用Android Docs:

    如果您需要使用String.format(String, Object...) 格式化您的字符串,那么您可以通过将您的格式参数放在 字符串资源。例如,使用以下资源:

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

    在本例中,格式字符串有两个参数:%1$s 是一个字符串 %2$d 是十进制数。您可以使用参数格式化字符串 像这样从您的应用程序中:

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

    【讨论】:

      【解决方案5】:

      对我来说,它在 Kotlin 中就是这样工作的:

      我的字符串.xml

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

      我的班级.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:价格: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

      【讨论】:

        猜你喜欢
        • 2011-05-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-15
        • 1970-01-01
        • 2011-09-25
        相关资源
        最近更新 更多