Android 复数无法正常工作,需要帮​​助

发布于 2024-09-19 19:14:32 字数 1243 浏览 10 评论 0原文

我一直在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独自←快乐 2024-09-26 19:14:33

这里同样的问题!我想这只是文档中的一个缺陷。 “纯”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 the getQuantityString(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? ;-)

关于从前 2024-09-26 19:14:32

看来您需要指定两次计数,第一次用于确定要使用的字符串,第二次是替换到字符串中的计数。例如

Resources res = this.getResources();
int tv = this.mObject.temp_c.intValue();
tTemp.setText(res.getQuantityString(R.plurals.degrees, tv, tv));

,至少在我迄今为止的测试中,不需要资源中的 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.

Resources res = this.getResources();
int tv = this.mObject.temp_c.intValue();
tTemp.setText(res.getQuantityString(R.plurals.degrees, tv, tv));

And at least in my testing so far, the xliff:g elements in the resource aren't needed.

一页 2024-09-26 19:14:32

Android 通过使用 R.plurals “支持”使用复数,而 R.plurals 实际上没有文档记录。深入研究源代码表明您应该能够拥有以下可能的字符串版本:

  • “零”
  • “一”
  • “少数”(正好是 2)
  • “其他”(对于 3 及以上)

但是,我发现只有“一个”和“其他”实际上有效(尽管其他的在 Android 源代码中使用!)。

要使用复数,您需要以与普通字符串资源类似的方式声明复数字符串:

<resources>
  <plurals name="match">
    <!-- Case of one match -->
    <item quantity="one">1 match</item>
    <!-- Case of several matches -->
    <item quantity="other">%d matches</item>
  </plurals>
</resources>

然后要在代码中实际使用它们,请使用类似于上面 superfell 建议的代码:

String text = getResources().getQuantityString(R.plurals.match, myIntValue, myIntValue);
myTextView.setText(text);

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:

  • "zero"
  • "one"
  • "few" (for exactly 2)
  • "other" (for 3 and above)

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:

<resources>
  <plurals name="match">
    <!-- Case of one match -->
    <item quantity="one">1 match</item>
    <!-- Case of several matches -->
    <item quantity="other">%d matches</item>
  </plurals>
</resources>

Then to actually use them in code, use code similar to what superfell suggested above:

String text = getResources().getQuantityString(R.plurals.match, myIntValue, myIntValue);
myTextView.setText(text);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文