Android的复数的治疗和QUOT;零" [英] Android plurals treatment of "zero"

查看:152
本文介绍了Android的复数的治疗和QUOT;零"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在我的strings.xml以下复数的ressource:

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

   <plurals name="item_shop">
        <item quantity="zero">No item</item>
        <item quantity="one">One item</item>
        <item quantity="other">%d items</item>
   </plurals>   

我使用的显示结果给用户:

I'm showing the result to the user using:

textView.setText(getQuantityString(R.plurals.item_shop, quantity, quantity));

它的工作很好地与1以上,但如果数量为0,那么我看空的。 零的价值只支持阿拉伯语语言文档似乎预示? 还是我失去了一些东西?

It's working well with 1 and above, but if quantity is 0 then I see "0 items". Is "zero" value supported only in Arabic language as the documentation seems to indicate? Or am I missing something?

推荐答案

国际化的Andr​​oid资源的方法是相当有限的。我曾使用标准的要好得多成功 java.text.MessageFormat中的

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:

<resources>
    <string name="item_shop">{0,choice,0#No items|1#One item|1&lt;{0} items}</string>
</resources>

然后,从code所有你需要做的是以下内容:

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

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

您可以阅读更多有关格式字符串的 javadoc,了解的MessageFormat

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

这篇关于Android的复数的治疗和QUOT;零&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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