Android 复数无法正常工作,需要帮助
我一直在尝试在 Android 上使用复数资源,但没有任何运气。
这是我的复数资源文件:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<plurals name="meters">
<item quantity="one">1 meter</item>
<item quantity="other">
<xliff:g id="count">%d</xliff:g>
meters
</item>
</plurals>
<plurals name="degrees">
<item quantity="one">1 degree</item>
<item quantity="other">
<xliff:g id="count">%d</xliff:g>
degrees
</item>
</plurals>
</resources>
...然后这是我尝试从资源中提取数量字符串时使用的代码:
Resources res = this.getResources();
tTemp.setText(res.getQuantityString(R.plurals.degrees, this.mObject.temp_c.intValue()));
...但 TextView 中的文本仍然是 %d度
和%d 米
。
有谁知道发生了什么事?我已经调试了代码,并且 res.getQuantityString(...) 调用返回一个值为 %d 度
或 %d 米
的字符串。尽管当数量恰好为 1 时,它确实会正确评估为 1 度
或 1 米
。
预先感谢您的任何帮助!
问候,天球。
I've been attempting to utilize the plurals resource with Android but have not had any luck.
Here is my resource file for my plurals:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<plurals name="meters">
<item quantity="one">1 meter</item>
<item quantity="other">
<xliff:g id="count">%d</xliff:g>
meters
</item>
</plurals>
<plurals name="degrees">
<item quantity="one">1 degree</item>
<item quantity="other">
<xliff:g id="count">%d</xliff:g>
degrees
</item>
</plurals>
</resources>
...and then here is the code I am using when I attempt to extract the quantity string from my resources:
Resources res = this.getResources();
tTemp.setText(res.getQuantityString(R.plurals.degrees, this.mObject.temp_c.intValue()));
...but the text in the TextView remains to be %d degrees
and %d meters
.
Does anyone know what is happening? I've debugged the code and the res.getQuantityString(...) call is returning a String whose value is %d degrees
or %d meters
. Though when the quantity happens to be 1 it does correctly evalute to 1 degree
or 1 meter
.
Thanks in advance for any help!
Regards, celestialorb.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里同样的问题!我想这只是文档中的一个缺陷。 “纯”
getQuantitiyString(int, int)
方法仅获取文本资源,没有任何格式设置。正如 superfell 所说:只需使用 getQuantityString(int, int, Object...) 方法并两次传递整数值。我希望这能像你一样工作,但事实并非如此!
PS:也许检查一下答案是否正确? ;-)
Same problem here! I guess it is just a flaw in the documentation. The "pure"
getQuantitiyString(int, int)
method does just get a text resource, without any formatting. As superfell stated: just use thegetQuantityString(int, int, Object...)
method and hand over your integer value twice.I'd hoped this worked the same way you did, but it simply doesn't!!
PS: maybe check an answer as the correct one? ;-)
看来您需要指定两次计数,第一次用于确定要使用的字符串,第二次是替换到字符串中的计数。例如
,至少在我迄今为止的测试中,不需要资源中的
xliff:g
元素。It appears that you need to specify the count twice, the first is used to determine the string to use, and the second is the one that is replaced into the string. e.g.
And at least in my testing so far, the
xliff:g
elements in the resource aren't needed.Android 通过使用 R.plurals “支持”使用复数,而 R.plurals 实际上没有文档记录。深入研究源代码表明您应该能够拥有以下可能的字符串版本:
但是,我发现只有“一个”和“其他”实际上有效(尽管其他的在 Android 源代码中使用!)。
要使用复数,您需要以与普通字符串资源类似的方式声明复数字符串:
然后要在代码中实际使用它们,请使用类似于上面 superfell 建议的代码:
Android "supports" the use of plurals by use of R.plurals which is practically undocumented. Diving into the source code reveals that you should be able to have the following possible versions of a string:
However, I've found that only "one" and "other" actually work (despite the others being used in the android source!).
To use plurals you want to declare you pluralizable strings in a similar way to normal string resources:
Then to actually use them in code, use code similar to what superfell suggested above: