ChoiceFormat 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
A ChoiceFormat 允许将格式附加到一系列数字。
[Android.Runtime.Register("java/text/ChoiceFormat", DoNotGenerateAcw=true)]
public class ChoiceFormat : Java.Text.NumberFormat
[<Android.Runtime.Register("java/text/ChoiceFormat", DoNotGenerateAcw=true)>]
type ChoiceFormat = class
inherit NumberFormat
- 继承
- 特性
注解
A ChoiceFormat 允许将格式附加到一系列数字。 它通常用于 MessageFormat 处理复数。 该选项使用双精度值升序列表进行指定,其中每个项指定下一项的半打开间隔: <blockquote>
X matches j if and only if limit[j] ≤ X < limit[j+1]
</blockquote> 如果不存在匹配项,则使用第一个或最后一个索引,具体取决于数字 (X) 是否太低或太高。 如果限制数组不按升序排列,格式设置的结果将不正确。 ChoiceFormat 也接受 \u221E 等效于无穷大(INF)。
注意:ChoiceFormat不同于使用构造函数创建ChoiceFormat对象的其他Format类(而不是getInstance样式工厂方法)。 不需要工厂方法,因为 ChoiceFormat 不需要给定区域设置的任何复杂设置。 事实上, ChoiceFormat 不实现任何区域设置特定的行为。
创建时 ChoiceFormat,必须指定格式数组和限制数组。 这些数组的长度必须相同。 例如,<ul><li>limits = brformats = {1,2,3,4,5,6,7}<{“Sun”,“Mon”,“Wed”,“Thur”,“Fri”,“Sat”}<li>limits = {0, 1, ChoiceFormat.nextDouble(1)}<>br>formats = {“no files”, “one file”, “many files”}<br> (nextDouble可用于获取下一个更高的双精度,使半开放间隔)。 </ul>
下面是显示格式和分析的简单示例: <blockquote>
{@code
double[] limits = {1,2,3,4,5,6,7};
String[] dayOfWeekNames = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"};
ChoiceFormat form = new ChoiceFormat(limits, dayOfWeekNames);
ParsePosition status = new ParsePosition(0);
for (double i = 0.0; i <= 8.0; ++i) {
status.setIndex(0);
System.out.println(i + " -> " + form.format(i) + " -> "
+ form.parse(form.format(i),status));
}
}
</blockquote> 下面是一个更复杂的示例,其模式格式为: <blockquote>
{@code
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));
}
}
</blockquote>
指定 ChoiceFormat 对象的模式非常简单。 例如: <blockquote>
{@code
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));
}
</blockquote> 和输出结果如下所示: <blockquote>
{@code
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.
}
</blockquote>
<h2>“synchronization”>Synchronization</h2>
选项格式未同步。 建议为每个线程创建单独的格式实例。 如果多个线程同时访问格式,则必须在外部同步。
在 1.1 中添加。
本页的某些部分是根据 创建和共享的工作进行的修改,并根据 许可证中所述的条款使用。
构造函数
| 名称 | 说明 |
|---|---|
| ChoiceFormat(Double[], String[]) |
使用限制和相应的格式构造。 |
| ChoiceFormat(IntPtr, JniHandleOwnership) |
创建 JNI 对象的托管表示形式时使用的构造函数;由运行时调用。 |
| ChoiceFormat(String) |
基于模式具有限制和相应格式的构造。 |
字段
| 名称 | 说明 |
|---|---|
| FractionField |
用于构造 FieldPosition 对象的字段常量。 (继承自 NumberFormat) |
| IntegerField |
用于构造 FieldPosition 对象的字段常量。 (继承自 NumberFormat) |
属性
| 名称 | 说明 |
|---|---|
| Class |
返回此 |
| Currency |
获取格式设置货币值时此数字格式使用的货币。 - 或 - 设置格式设置货币值格式时此数字格式使用的货币。 (继承自 NumberFormat) |
| GroupingUsed |
如果采用此格式使用分组,则返回 true。 - 或 - 设置是否将使用此格式的分组。 (继承自 NumberFormat) |
| Handle |
基础 Android 实例的句柄。 (继承自 Object) |
| JniIdentityHashCode |
获取互操作运行时分配给此Java对等的标识哈希代码。 (继承自 Object) |
| JniManagedPeerState |
获取运行时上次分配的状态标志。 (继承自 JavaObject) |
| JniPeerMembers |
获取用于访问此Java绑定成员的 JNI 元数据。 |
| MaximumFractionDigits |
返回数字的分数部分允许的最大位数。 - 或 - 设置数字的分数部分允许的最大位数。 (继承自 NumberFormat) |
| MaximumIntegerDigits |
返回数字整数部分中允许的最大位数。 - 或 - 设置数字整数部分中允许的最大位数。 (继承自 NumberFormat) |
| MinimumFractionDigits |
返回数字的分数部分允许的最小位数。 - 或 - 设置数字的分数部分允许的最小位数。 (继承自 NumberFormat) |
| MinimumIntegerDigits |
返回数字整数部分中允许的最小位数。 - 或 - 设置数字整数部分中允许的最小位数。 (继承自 NumberFormat) |
| ParseIntegerOnly |
如果此格式将仅将数字分析为整数,则返回 true。 - 或 - 设置是否只应将数字分析为整数。 (继承自 NumberFormat) |
| PeerReference |
获取此Java对等方的 JNI 对象引用。 (继承自 Object) |
| RoundingMode |
|
| ThresholdClass |
此 API 支持 Mono for Android 基础结构,不打算直接从代码使用。 |
| ThresholdType |
此 API 支持 Mono for Android 基础结构,不打算直接从代码使用。 |
方法
显式接口实现
| 名称 | 说明 |
|---|---|
| IJavaPeerable.Disposed() |
将此对等方标记为已释放,并将 |
| IJavaPeerable.Finalized() |
将此对等方标记为已释放,并将 |
| IJavaPeerable.JniObjectReferenceControlBlock |
获取此对等方使用的本机控制块指针。 (继承自 JavaObject) |
| IJavaPeerable.SetJniIdentityHashCode(Int32) |
存储运行时提供的标识哈希代码。 (继承自 JavaObject) |
| IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) |
替换存储的托管对等状态标志。 (继承自 JavaObject) |
| IJavaPeerable.SetPeerReference(JniObjectReference) |
更新对等引用,并在引用无效时释放已释放的对等方控制块。 (继承自 JavaObject) |
| IJavaPeerable.UnregisterFromRuntime() |
从运行时的Java对象查找中删除此实例。 (继承自 _Format) |
扩展方法
| 名称 | 说明 |
|---|---|
| GetJniTypeName(IJavaPeerable) |
获取实例 |
| JavaAs<TResult>(IJavaPeerable) |
尝试强制 |
| JavaCast<TResult>(IJavaObject) |
执行 Android 运行时检查的类型转换。 |
| JavaCast<TResult>(IJavaObject) |
重复使用兼容的托管对等,或获取请求类型的包装器。 |
| TryJavaCast<TResult>(IJavaPeerable, TResult) |
尝试强制 |