我开始遇到 android 复数形式,并且我对这个功能的可用性感到满意。我声明plurals.xml并尝试从我的代码中读取它们,但我得到了不正确的结果。
复数:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<plurals name="numbers">
<item quantity="zero">No comments</item>
<item quantity="one">%1$d comment.</item>
<item quantity="two">%1$d comments.</item>
<item quantity="few">%1$d comments.</item>
<item quantity="many">%1$d comments.</item>
<item quantity="other">%1$d comments.</item>
</plurals>
<plurals name="numberOfSongsAvailable">
<item quantity="zero">No song found.</item>
<item quantity="one">One song found.</item>
<item quantity="two">Two song found.</item>
<item quantity="other">Other songs found.</item>
</plurals>
</resources>
Run Code Online (Sandbox Code Playgroud)
活动 :
public class AndroidPlurals extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.plurals);
((ListView) findViewById(R.id.listView)).setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getPluralsList(10)));
Toast.makeText(this, getClass().getSimpleName(), Toast.LENGTH_LONG).show();
}
private String[] getPluralsList(int k) {
String[] array = new String[k];
for (int i = 0; i < array.length; i++) {
String quantityString = (String) this.getResources().getQuantityText(R.plurals.numberOfSongsAvailable, i);
array[i] = quantityString;
android.util.Log.i("Plurals", "i = " + i + ", quantityString = " + quantityString);
}
return array;
}
}
Run Code Online (Sandbox Code Playgroud)
输出 :
I/Plurals (17689): i = 0, quantityString = Other songs found.
I/Plurals (17689): i = 1, quantityString = One song found.
I/Plurals (17689): i = 2, quantityString = Other songs found.
I/Plurals (17689): i = 3, quantityString = Other songs found.
I/Plurals (17689): i = 4, quantityString = Other songs found.
I/Plurals (17689): i = 5, quantityString = Other songs found.
I/Plurals (17689): i = 6, quantityString = Other songs found.
I/Plurals (17689): i = 7, quantityString = Other songs found.
I/Plurals (17689): i = 8, quantityString = Other songs found.
I/Plurals (17689): i = 9, quantityString = Other songs found.
Run Code Online (Sandbox Code Playgroud)
为什么我没有0 quantity像No song found.文本一样的正确结果???
简短的回答是,在 Android 上,复数是为了区分语法。
引用文档(我的亮点):
选择使用哪个字符串完全基于语法需要。在英语中,即使数量为 0,表示 0 的字符串也将被忽略,因为 0 在语法上与 2 或除 1 之外的任何其他数字(“零书”、“一本书”、“两本书”和等等)。相反,在韩语中,只会使用另一个字符串。
和:
不要被以下事实误导,比如说,
two听起来它只能适用于数量 2:一种语言可能要求 2、12、102(等等)都被视为彼此相同但与其他数量不同. 依靠您的翻译来了解他们的语言实际坚持的区别。
此处有更多详细信息和替代方案:Android 复数处理“零”
| 归档时间: |
|
| 查看次数: |
2932 次 |
| 最近记录: |