http://stackoverflow.com/questions/5651902/android-plurals-treatment-of-zero

https://code.google.com/p/android/issues/detail?id=8287

plurals

Android中plurals表示单复数,但是对于0是不起作用的,比如

  <plurals name="leaderboard_community_hero_free">    <item quantity="zero">Free</item>    <item quantity="other">Free(%d)</item>  </plurals>  getQuantityString(R.plurals.leaderboard_community_hero_free, quantity, quantity)


对于quantity为0,结果是不正确的。解决方式是:

<string name="leaderboard_community_hero_free">{0,choice,0#Free|0<Free({0})}</string>MessageFormat.format(getSherlockActivity().getString(R.string.leaderboard_community_hero_free), number)


测试例子:

String fmt = "{0,choice,0#No items|1#One item|1<{0} items}";System.out.println(MessageFormat.format(fmt, 0));System.out.println(MessageFormat.format(fmt, 1));System.out.println(MessageFormat.format(fmt, 2));System.out.println(MessageFormat.format(fmt, 6));System.out.println("=============");fmt = "{0,choice,0#items|0<item({0})}";System.out.println(MessageFormat.format(fmt, 0));System.out.println(MessageFormat.format(fmt, 1));System.out.println(MessageFormat.format(fmt, 2));System.out.println(MessageFormat.format(fmt, 6));System.out.println("=============");ChoiceFormat form = new ChoiceFormat(      "-1#is negative| 0#is zero or fraction | 1#is one |1.0<is 1+ |2#is two |2<is more than 2.");System.out.println(form.format(0));System.out.println(form.format(1));System.out.println(form.format(2));System.out.println(form.format(3));System.out.println("=============");fmt = "0#No items|1< {0} items";form = new ChoiceFormat(fmt);System.out.println(form.format(0));System.out.println(form.format(1));System.out.println(form.format(2));

输出结果:

No itemsOne item2 items6 items=============itemsitem(1)item(2)item(6)=============is zero or fraction is one is two is more than 2.=============No itemsNo items {0} items


文档

http://docs.oracle.com/javase/6/docs/api/java/text/MessageFormat.html

http://docs.oracle.com/javase/6/docs/api/java/text/ChoiceFormat.html


更多的例子

Here is a more complex example, with a pattern format:

 double[] filelimits = {0,1,2}; String[] filepart = {"are no files","is one file","are {2} files"}; ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart); Format[] testFormats = {fileform, null, NumberFormat.getInstance()}; MessageFormat pattform = new MessageFormat("There {0} on {1}"); pattform.setFormats(testFormats); Object[] testArgs = {null, "ADisk", null}; for (int i = 0; i < 4; ++i) {     testArgs[0] = new Integer(i);     testArgs[2] = testArgs[0];     System.out.println(pattform.format(testArgs)); } 

Specifying a pattern for ChoiceFormat objects is fairly straightforward. For example:

 ChoiceFormat fmt = new ChoiceFormat(      "-1#is negative| 0#is zero or fraction | 1#is one |1.0<is 1+ |2#is two |2<is more than 2."); System.out.println("Formatter Pattern : " + fmt.toPattern()); System.out.println("Format with -INF : " + fmt.format(Double.NEGATIVE_INFINITY)); System.out.println("Format with -1.0 : " + fmt.format(-1.0)); System.out.println("Format with 0 : " + fmt.format(0)); System.out.println("Format with 0.9 : " + fmt.format(0.9)); System.out.println("Format with 1.0 : " + fmt.format(1)); System.out.println("Format with 1.5 : " + fmt.format(1.5)); System.out.println("Format with 2 : " + fmt.format(2)); System.out.println("Format with 2.1 : " + fmt.format(2.1)); System.out.println("Format with NaN : " + fmt.format(Double.NaN)); System.out.println("Format with +INF : " + fmt.format(Double.POSITIVE_INFINITY)); 
And the output result would be like the following:
        
Format with -INF : is negative Format with -1.0 : is negative Format with 0 : is zero or fraction Format with 0.9 : is zero or fraction Format with 1.0 : is one Format with 1.5 : is 1+ Format with 2 : is two Format with 2.1 : is more than 2. Format with NaN : is negative Format with +INF : is more than 2.










更多相关文章

  1. Android一个小球弹跳的例子,希望对大家有用
  2. Android学习之Android自带例子 ContactManager
  3. Android中Service的一个Demo例子
  4. php做接口+android 请求API接口并展示到ListView例子

随机推荐

  1. android 文字描边
  2. Android热更新六:Qzone热更新原理
  3. Android设置页面Activity全屏(隐藏导航栏
  4. android 布局中的属性总结
  5. android 相对布局里面的一些属性
  6. Android菜单详解——理解android中的Menu
  7. android EditText inputType 值说明
  8. Android(安卓)EditText 的 inputType属性
  9. Android热更新九:Robust热更新原理
  10. Android基础04-基本控件及表单三大控件