How to use Android quantity strings (plurals)?

前端 未结 1 666
执笔经年
执笔经年 2021-02-05 00:32

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

The erro

相关标签:
1条回答
  • 2021-02-05 00:59

    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
    
    0 讨论(0)
提交回复
热议问题