Android多元化不工作,需要帮助

时间:2021-11-14 02:01:51

I've been attempting to utilize the plurals resource with Android but have not had any luck.

我一直在尝试使用Android的plurals资源,但没有任何运气。

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.

…但是TextView中的文本仍然是%d度和%d米。

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.

有人知道发生了什么吗?我调试了代码,getquantitystring(…)调用返回一个值为%d度或%d米的字符串。虽然当数量恰好是1时,它会正确地计算1度或1米。

Thanks in advance for any help!

谢谢你的帮助!

Regards, celestialorb.

问候,celestialorb。

3 个解决方案

#1


33  

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.

至少在我目前的测试中,不需要资源中的xliff:g元素。

#2


4  

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:

Android“支持”使用R来使用复数。复数实际上是无证的。深入到源代码中可以发现,您应该能够拥有以下字符串的可能版本:

  • "zero"
  • “零”
  • "one"
  • “一”
  • "few" (for exactly 2)
  • “几”(2)
  • "other" (for 3 and above)
  • “其他”(3及以上)

However, I've found that only "one" and "other" actually work (despite the others being used in the android source!).

然而,我发现只有“one”和“other”可以使用(尽管其他的在android源代码中使用!)

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:

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

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

#3


3  

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.

同样的问题!我想这只是文档中的一个缺陷。“纯”getQuantitiyString(int, int)方法只获取一个文本资源,没有任何格式。如superfall所述:只需使用getQuantityString(int, int, Object…)方法,并将整数值传递两次。

I'd hoped this worked the same way you did, but it simply doesn't!!

我希望这和你做的一样,但它就是不行!

PS: maybe check an answer as the correct one? ;-)

也许检查一下答案是否正确?:-)

#1


33  

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.

至少在我目前的测试中,不需要资源中的xliff:g元素。

#2


4  

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:

Android“支持”使用R来使用复数。复数实际上是无证的。深入到源代码中可以发现,您应该能够拥有以下字符串的可能版本:

  • "zero"
  • “零”
  • "one"
  • “一”
  • "few" (for exactly 2)
  • “几”(2)
  • "other" (for 3 and above)
  • “其他”(3及以上)

However, I've found that only "one" and "other" actually work (despite the others being used in the android source!).

然而,我发现只有“one”和“other”可以使用(尽管其他的在android源代码中使用!)

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:

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

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

#3


3  

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.

同样的问题!我想这只是文档中的一个缺陷。“纯”getQuantitiyString(int, int)方法只获取一个文本资源,没有任何格式。如superfall所述:只需使用getQuantityString(int, int, Object…)方法,并将整数值传递两次。

I'd hoped this worked the same way you did, but it simply doesn't!!

我希望这和你做的一样,但它就是不行!

PS: maybe check an answer as the correct one? ;-)

也许检查一下答案是否正确?:-)