我必须在android中插入一个xml字符串,两个复数,以满足用户的年龄。我得处理sutch的案子
正如你只看到的英语,我需要6个不同的情况。另一种语言就更多了。另外,我也不想把字符串拆分成两个复数,因为在同一种语言中,世界的顺序是不同的。
到目前为止,我只用复数表示一个数量。在文档中,我没有看到任何解决sutch问题的提示?
实际解决办法:
在xml中:
<string name="age_1">You are %1$s old</string>
<string name="age_2">You are %1$s and %2$s old</string>
<plurals name="age_months">
<item quantity="one">%d month</item>
<item quantity="other">%d months</item>
</plurals>
<plurals name="age_years">
<item quantity="one">%d year</item>
<item quantity="other">%d years</item>
</plurals>在代码中:
if(years == 0) {
String m = res.getQuantityString(R.plurals.age_months, months, months);
ageTextView.setText(String.format(res.getString(R.age_1), m);
} else if (months == 0){
String y = res.getQuantityString(R.plurals.age_years, years, years);
ageTextView.setText(String.format(res.getString(R.age_1), y);
} else {
String m = res.getQuantityString(R.plurals.age_months, months, months);
String y = res.getQuantityString(R.plurals.age_years, years, years);
ageTextView.setText(String.format(res.getString(R.age_2), y, m);
}但是我正在寻找在xml中使用一个复数内容的东西:
<plurals name="age_months">
<item quantityA="one" quantityB="one">You are %1$d year %2$d month</item>
<item quantityA="other"quantityB="one">You are %1$d years %2d month</item>
...
</plurals>发布于 2017-02-20 13:50:16
因此,似乎没有比我所用的更好的解决办法:
在xml中:
<string name="age_1">You are %1$s old</string>
<string name="age_2">You are %1$s and %2$s old</string>
<plurals name="age_months">
<item quantity="one">%d month</item>
<item quantity="other">%d months</item>
</plurals>
<plurals name="age_years">
<item quantity="one">%d year</item>
<item quantity="other">%d years</item>
</plurals>以及代码中的一些逻辑:
if(years == 0) {
String m = res.getQuantityString(R.plurals.age_months, months, months);
ageTextView.setText(String.format(res.getString(R.age_1), m);
} else if (months == 0){
String y = res.getQuantityString(R.plurals.age_years, years, years);
ageTextView.setText(String.format(res.getString(R.age_1), y);
} else {
String m = res.getQuantityString(R.plurals.age_months, months, months);
String y = res.getQuantityString(R.plurals.age_years, years, years);
ageTextView.setText(String.format(res.getString(R.age_2), y, m);
}发布于 2017-02-20 09:29:28
看看这个。
数量字符串(Plurals):
https://developer.android.com/guide/topics/resources/string-resource.html#Plurals
<?xml version="1.0" encoding="utf-8"?>
<resources>
<plurals
name="plural_name">
<item
quantity=["zero" | "one" | "two" | "few" | "many" | "other"]
>text_string</item>
</plurals>
</resources>https://stackoverflow.com/questions/42340687
复制相似问题