gpt4 book ai didi

Android复数处理 "zero"

转载 作者:IT老高 更新时间:2023-10-28 13:12:07 25 4
gpt4 key购买 nike

如果我的 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>

我正在向用户显示结果:

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

它适用于 1 及以上,但如果数量为 0,那么我会看到“0 个项目”。文档似乎表明,“零”值是否仅支持阿拉伯语?还是我错过了什么?

最佳答案

Android 资源国际化的方法相当有限。使用标准 java.text.MessageFormat.

我取得了更好的成功

基本上,您所要做的就是像这样使用标准字符串资源:

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

那么,从代码中你所要做的就是:

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

您可以在 javadocs for MessageFormat 中阅读有关格式字符串的更多信息。

关于Android复数处理 "zero",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5651902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com