Android plurals treatment of “zero”

前端 未结 6 1473
悲哀的现实
悲哀的现实 2020-12-04 08:38

If have the following plural ressource in my strings.xml:

   
        No item
           


        
6条回答
  •  北荒
    北荒 (楼主)
    2020-12-04 08:59

    The Android resource method of internationalisation is quite limited. I have had much better success using the standard java.text.MessageFormat.

    Basically, all you have to do is use the standard string resource like this:

    
        {0,choice,0#No items|1#One item|1<{0} items}
    
    

    Then, from the code all you have to do is the following:

    String fmt = getResources().getText(R.string.item_shop).toString();
    textView.setText(MessageFormat.format(fmt, amount));
    

    You can read more about the format strings in the javadocs for MessageFormat

提交回复
热议问题