How to use Android quantity strings (plurals)?

自闭症网瘾萝莉.ら 提交于 2019-12-03 09:40:24
LWChris

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

  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

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.

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

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>

then the appropiate calls to getQuantityString are:

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!