如何使用Android数量字符串(复数)? [英] How to use Android quantity strings (plurals)?

查看:160
本文介绍了如何使用Android数量字符串(复数)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 getQuantityString 方法以根据android开发人员指南

I am trying to use getQuantityString method in Resources to retrieve Quantity Strings (Plurals) based on android developer guidelines Quantity String (Plurals)

我得到的错误是

Error:(604) Multiple substitutions specified in non-positional format; did you mean to add the formatted="false" attribute?
Error:(604) Found tag </item> where </plurals> is expected

当我如下设置复数时

<plurals name="productCount">
    <item quantity="one" formatted="true">%1$d of %2$d product</item>
    <item quantity="other" formatted="true">%1$d of %2$d products</item>
</plurals>

并尝试如下阅读

productIndexCountText.setText(getResources().getQuantityString(R.plurals.productCount, position, size));

一种解决方法是将字符串拆分为仅对字符串的最后一部分使用复数形式,然后将这两部分串联起来.但是我试图避免这样做.

One workaround is to break the string up to use plural only for the last part of the string and concatenate the two parts. But I am trying to avoid doing that if possible.

推荐答案

您无需为这些项目中的任何一项设置格式化"属性.使用数量字符串时,只有三种可能性:

You don't need to set the "formatted" attribute for any of those items. When using quantity strings, there are only three possibilities:

  1. 资源字符串为纯文本,不包含任何参数
  2. 资源字符串仅包含一个参数(很可能是数量);使用%d或所需的任何格式
  3. 资源字符串包含多个参数;必须按位置明确访问所有参数,例如%1$d
  1. the resource string is plain text and does not contain any parameters
  2. the resource string contains only one parameter (most likely the quantity); use %d or whatever format you need
  3. the resource string contains multiple parameters; all parameters have to be explicitly accessed by their position, for example %1$d

对于getQuantityString方法,有两种重载:一种仅具有资源ID和数量,另一种具有附加的Object... formatArgs参数.

As for the getQuantityString method, there are two overloads: one with only the ressource id and the quantity, and one with an additional Object... formatArgs parameter.

对于情况1,可以使用getQuantityString(@PluralsRes int id, int quantity)方法.

For case 1., you can use the getQuantityString(@PluralsRes int id, int quantity) method.

对于其他所有情况,i. e.如果有任何参数,则需要getQuantityString(@PluralsRes int id, int quantity, Object... formatArgs)重载.注意:所有参数必须存在于param数组中.这意味着,如果资源字符串显示数量,则数量变量将两次传递给函数.

For all other cases, i. e. if you have any parameters, you need the getQuantityString(@PluralsRes int id, int quantity, Object... formatArgs) overload. Note: all parameters have to be present in the param array. That means, if the resource string displays the quantity, the quantity variable will be passed twice to the function.

因此,如果这些是您的资源

So if these are your resources

<resources>
    <plurals name="test0">
        <item quantity="one">Test ok</item>
        <item quantity="other">Tests ok</item>
    </plurals>
    <plurals name="test1">
        <item quantity="one">%d test ok</item>
        <item quantity="other">%d tests ok</item>
    </plurals>
    <plurals name="test2">
        <item quantity="one">%2$s: %1$d test ok</item>
        <item quantity="other">%2$s: %1$d tests ok</item>
    </plurals>
    <plurals name="test3">
        <item quantity="one">%3$s: %1$d test out of %2$d ok</item>
        <item quantity="other">%3$s: %1$d tests out of %2$d ok</item>
    </plurals>
</resources>

然后对getQuantityString的适当调用是:

int success = 1;
int total = 10;
String group = "Group name";

getResources().getQuantityString(R.plurals.test0, success)
// Test ok
getResources().getQuantityString(R.plurals.test1, success, success)
// 1 test ok
getResources().getQuantityString(R.plurals.test2, success, success, group)
// Group name: 1 test ok
getResources().getQuantityString(R.plurals.test3, success, success, total, group)
// Group name: 1 test out of 10 ok

success = 5;
getResources().getQuantityString(R.plurals.test0, success)
// Tests ok
getResources().getQuantityString(R.plurals.test1, success, success)
// 5 tests ok
getResources().getQuantityString(R.plurals.test2, success, success, group)
// Group name: 5 tests ok
getResources().getQuantityString(R.plurals.test3, success, success, total, group)
// Group name: 5 tests out of 10 ok

这篇关于如何使用Android数量字符串(复数)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆