MessageFormat 类

定义

MessageFormat 提供一种以非特定语言方式生成串联消息的方法。

[Android.Runtime.Register("java/text/MessageFormat", DoNotGenerateAcw=true)]
public class MessageFormat : Java.Text._Format
[<Android.Runtime.Register("java/text/MessageFormat", DoNotGenerateAcw=true)>]
type MessageFormat = class
    inherit _Format
继承
MessageFormat
属性

注解

MessageFormat 提供一种以非特定语言方式生成串联消息的方法。 使用此方法可构造为最终用户显示的消息。

MessageFormat 获取一组对象,设置这些对象的格式,然后将格式化字符串插入到相应位置的模式中。

<strong Note:</strong>MessageFormat> 与其他Format类不同,即使用其构造函数之一(而不是getInstance样式工厂方法)创建MessageFormat对象。 工厂方法不是必需的,因为 MessageFormat 本身不实现区域设置特定的行为。 任何区域设置特定行为都由你提供的模式以及用于插入参数的子格式定义。

<h2>“patterns”>Patterns and Their Interpretation</h2>

MessageFormat 使用以下格式的模式: <blockquote>

<i>MessageFormatPattern:</i>
<i>String</i>
<i>MessageFormatPattern</i> <i>FormatElement</i> <i>String</i>

<i>FormatElement:</i>
                    { <i>ArgumentIndex</i> }
                    { <i>ArgumentIndex</i> , <i>FormatType</i> }
                    { <i>ArgumentIndex</i> , <i>FormatType</i> , <i>FormatStyle</i> }

<i>FormatType: one of </i>
                    number date time choice

<i>FormatStyle:</i>
                    short
                    medium
                    long
                    full
                    integer
                    currency
                    percent
<i>SubformatPattern</i>

</blockquote>

字符串中,可以使用一对单引号来引用除单引号以外的任意字符。 例如,模式字符串 "'{0}'" 表示字符串 "{0}",而不是 FormatElement。 单引号 '' 本身必须由 字符串中的双引号表示。 例如,模式字符串 "'{''}'" 被解释为一个序列 '{ (引号开头和左大括号)、 '' (单引号)和 }' (右大括号和引号的结尾)、 <em>not</em>'{''}' (左大括号和右大括号):表示字符串 "{'}", <em>not</em>"{}"

SubformatPattern 由其相应的子格式解释,并应用子格式依赖模式规则。 例如,模式字符串"{1,number,<u>$'#',##</u>}"(带下划线的 SubformatPattern)将生成带井号引号的数字格式,结果如下: "$#31,45" 有关详细信息,请参阅每个 Format 子类文档。

任何不匹配的引号在给定模式的末尾被视为已关闭。 例如,模式字符串 "'{0“}被视为模式 "'{0'”}。

非引号模式中的任何大括号都必须保持平衡。 例如,"ab {0} de"并且"ab '}' de"是有效的模式,但"''{''""ab {0'}' de""ab } de"不是。

<dl><dt><b>Warning:</b><dd>消息格式模式中使用引号的规则似乎有些令人困惑。 具体而言,本地化人员是否需要加倍单引号并不总是显而易见的。 请确保通知本地化人员有关规则的信息,并告诉它们(例如,通过使用资源捆绑源文件中的注释)将处理 MessageFormat哪些字符串。 请注意,本地化人员可能需要在原始版本没有单引号的已翻译字符串中使用单引号。 </dl>

ArgumentIndex 值是使用数字'0''9'写入的非负整数,表示传递给format方法或方法返回parse的结果数组中的索引arguments

FormatTypeFormatStyle 值用于为 format 元素创建Format实例。 下表显示了值如何映射到 Format 实例。 表中未显示的组合是非法的。 SubformatPattern 必须是所用子类的有效模式字符串Format

<table class=“plain”><caption style=“display:none”>显示 FormatType 和 FormatStyle 值如何映射到 Format instances</caption><thead<>tr><th scope=“col” class=“TableHeadingColor”>FormatType <th scope=“col” class=“TableHeadingColor”>FormatStyle <th scope=“col” class=“TableHeadingColor”>Subformat Created </thead<>tbody<>tr><th scope=“row” style=“text-weight: normal”>(无)<th scope=“row” style=“text-weight: normal”>(none)<td>null<tr th scope=“row” style=“text-weight: normal” rowspan=5><numberth scope=“row” style=“text-weight: normal”>(none)<td tr th scope=“row” style=“text-weight: normal”><integertd tr<><>><th scope=“row” style=“text-weight: normal”currency><td(getLocale())NumberFormat#getIntegerInstance(Locale) NumberFormat.getIntegerInstance<>td>NumberFormat#getInstance(Locale) NumberFormat.getInstance(getLocale())<>NumberFormat#getCurrencyInstance(Locale) NumberFormat.getCurrencyInstance(getLocale())<tr><th scope=“row” style=“text-weight: normal”>percent<td>(getLocale())<NumberFormat#getPercentInstance(Locale) NumberFormat.getPercentInstancetr><th scope=“row” style=“text-weight: normal”>SubformatPattern<td tr th scope=“row” style=“text-weight: normal” rowspan=date<>“row” style=“text-weight: normal”>(none)<td><DecimalFormat#DecimalFormat(String,DecimalFormatSymbols) DecimalFormat(getLocale()))new(subformatPattern,DecimalFormatSymbols#getInstance(Locale) DecimalFormatSymbols.getInstance<DateFormat#DEFAULT>DateFormat#getDateInstance(int,Locale) DateFormat.getDateInstance, getLocale())(tr><><th scope=“row” style=“text-weight:normal“><shorttd>(, getLocale())<DateFormat#getDateInstance(int,Locale) DateFormat.getDateInstanceDateFormat#SHORTtr><th scope=”row“ style=”text-weight: normal“><mediumtd>DateFormat#getDateInstance(int,Locale) DateFormat.getDateInstanceDateFormat#DEFAULT(, getLocale())<tr><th scope=”row“ style=”text-weight: normal“><longtd tr><th scope=”row“ style=”text-weight: normal“>full<td>DateFormat#getDateInstance(int,Locale) DateFormat.getDateInstance(>DateFormat#LONG, getLocale()), getLocale())DateFormat#FULL<DateFormat#getDateInstance(int,Locale) DateFormat.getDateInstance(<tr th><scope=“row” style=“text-weight: normal”>SubformatPattern<td><newSimpleDateFormat#SimpleDateFormat(String,Locale) SimpleDateFormat(subformatPattern, getLocale())tr th scope=“row” style=“text-weight: normal” rowspan=6<time>th scope=“row” style=“text-weight: normal”>(none)<td tr><th scope=“row” style=“text-weight: normal”><shorttd tr<<>>>DateFormat#getTimeInstance(int,Locale) DateFormat.getTimeInstance(DateFormat#DEFAULT, getLocale())<>DateFormat#getTimeInstance(int,Locale) DateFormat.getTimeInstance(DateFormat#SHORT, getLocale())<th scope=“row” style=“text-weight: normal”><mediumtd>, getLocale())DateFormat#DEFAULT(DateFormat#getTimeInstance(int,Locale) DateFormat.getTimeInstance<tr th scope=“row” style=“text-weight: normal”><longtd>(, getLocale())<DateFormat#getTimeInstance(int,Locale) DateFormat.getTimeInstanceDateFormat#LONGtr><th scope=“row” style=“text-weight: normal”><fulltd>(DateFormat#getTimeInstance(int,Locale) DateFormat.getTimeInstanceDateFormat#FULL<, getLocale())tr><<>th scope=“row” style=“text-weight: normal”>SubformatPattern<td>newSimpleDateFormat#SimpleDateFormat(String,Locale) SimpleDateFormat(subformatPattern, getLocale())<tr><th scope=“row” style=“text-weight: normal”><choiceth scope=“row” style=“text-weight: normal”>SubformatPattern<td>(subformatPattern)ChoiceFormat#ChoiceFormat(String) ChoiceFormat<new/tbody></table>

<h3>使用情况信息</h3>

下面是一些用法示例。 在实际的国际化程序中,消息格式模式和其他静态字符串当然将从资源捆绑包获取。 其他参数将在运行时动态确定。

第一个示例使用静态方法 MessageFormat.format,该方法在内部创建 MessageFormat 一次性使用: <blockquote>

int planet = 7;
            String event = "a disturbance in the Force";

            String result = MessageFormat.format(
                "At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.",
                planet, new Date(), event);

</blockquote> 输出为: <blockquote>

At 12:30 PM on Jul 3, 2053, there was a disturbance in the Force on planet 7.

</blockquote>

以下示例创建 MessageFormat 可以重复使用的实例: <blockquote>

int fileCount = 1273;
            String diskName = "MyDisk";
            Object[] testArgs = {new Long(fileCount), diskName};

            MessageFormat form = new MessageFormat(
                "The disk \"{1}\" contains {0} file(s).");

            System.out.println(form.format(testArgs));

</blockquote> :具有不同值的 fileCount输出: <blockquote>

The disk "MyDisk" contains 0 file(s).
            The disk "MyDisk" contains 1 file(s).
            The disk "MyDisk" contains 1,273 file(s).

</blockquote>

对于更复杂的模式,可以使用 a ChoiceFormat 为单数和复数形式生成正确的形式: <块>

MessageFormat form = new MessageFormat("The disk \"{1}\" contains {0}.");
            double[] filelimits = {0,1,2};
            String[] filepart = {"no files","one file","{0,number} files"};
            ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
            form.setFormatByArgumentIndex(0, fileform);

            int fileCount = 1273;
            String diskName = "MyDisk";
            Object[] testArgs = {new Long(fileCount), diskName};

            System.out.println(form.format(testArgs));

</blockquote> :具有不同值的 fileCount输出: <blockquote>

The disk "MyDisk" contains no files.
            The disk "MyDisk" contains one file.
            The disk "MyDisk" contains 1,273 files.

</blockquote>

可以按上述示例或模式以编程方式创建 ChoiceFormat 。 有关详细信息,请参阅 ChoiceFormat。 <blockquote>

{@code
            form.applyPattern(
               "There {0,choice,0#are no files|1#is one file|1<are {0,number,integer} files}.");
            }

</blockquote>

<strong Note:</strong>> 如上所述,由 ChoiceFormat in MessageFormat 生成的字符串被视为特殊;“{”的出现用于指示子窗体,并导致递归。 如果同时创建和MessageFormatChoiceFormat编程方式(而不是使用字符串模式),请注意不要生成一种在自身上递归的格式,这将导致无限循环。

在字符串中多次分析单个参数时,最后一个匹配项将是分析的最终结果。 例如, <blockquote>

MessageFormat mf = new MessageFormat("{0,number,#.##}, {0,number,#.#}");
            Object[] objs = {new Double(3.1415)};
            String result = mf.format( objs );
            // result now equals "3.14, 3.1"
            objs = null;
            objs = mf.parse(result, new ParsePosition(0));
            // objs now equals {new Double(3.1)}

</blockquote>

同样,使用包含相同参数的多个匹配项的模式分析 MessageFormat 对象将返回最后一个匹配项。 例如, <blockquote>

MessageFormat mf = new MessageFormat("{0}, {0}, {0}");
            String forParsing = "x, y, z";
            Object[] objs = mf.parse(forParsing, new ParsePosition(0));
            // result now equals {new String("z")}

</blockquote>

<h3>“synchronization”>Synchronization</h3>

消息格式未同步。 建议为每个线程创建单独的格式实例。 如果多个线程同时访问格式,则必须在外部同步。

在 1.1 中添加。

Java文档java.text.MessageFormat

本页的某些部分是根据 创建和共享的工作进行的修改,并根据 许可证中所述的条款使用。

构造函数

名称 说明
MessageFormat(IntPtr, JniHandleOwnership)

创建 JNI 对象的托管表示形式时使用的构造函数;由运行时调用。

MessageFormat(String, Locale)

为指定的区域设置和模式构造 MessageFormat。

MessageFormat(String)

为默认 java.util.Locale.Category#FORMAT FORMAT 区域设置和指定模式构造 MessageFormat。

属性

名称 说明
Class

返回此 Object的运行时类。

(继承自 Object)
Handle

基础 Android 实例的句柄。

(继承自 Object)
JniIdentityHashCode

MessageFormat 提供一种以非特定语言方式生成串联消息的方法。

(继承自 Object)
JniPeerMembers

MessageFormat 提供一种以非特定语言方式生成串联消息的方法。

Locale

获取创建或比较子窗体时使用的区域设置。 - 或 - 设置创建或比较子格式时要使用的区域设置。

PeerReference

MessageFormat 提供一种以非特定语言方式生成串联消息的方法。

(继承自 Object)
ThresholdClass

此 API 支持 Mono for Android 基础结构,不打算直接从代码使用。

ThresholdType

此 API 支持 Mono for Android 基础结构,不打算直接从代码使用。

方法

名称 说明
ApplyPattern(String)

设置此消息格式使用的模式。

Clone()

创建并返回此对象的副本。

(继承自 _Format)
Dispose()

MessageFormat 提供一种以非特定语言方式生成串联消息的方法。

(继承自 Object)
Dispose(Boolean)

MessageFormat 提供一种以非特定语言方式生成串联消息的方法。

(继承自 Object)
Equals(Object)

指示其他对象是否“等于”此对象。

(继承自 Object)
Format(Object, StringBuffer, FieldPosition)

设置对象的数组的格式,并将格式元素替换为格式对象,并将其追加 MessageFormat到提供的 StringBuffer模式。

Format(Object)

设置对象的格式以生成字符串。

(继承自 _Format)
Format(Object[], StringBuffer, FieldPosition)

设置对象的数组的格式,并将格式元素替换为格式对象,并将其追加 MessageFormat到提供的 StringBuffer模式。

Format(String, Object[])

设置对象数组的格式,并将代码 MessageFormat</code> 的模式(格式元素替换为格式化的对象)追加<到所提供的<代码>StringBuffer</code>。>

FormatToCharacterIterator(Object)

设置生成 . 的对象 AttributedCharacterIterator的格式。

(继承自 _Format)
GetFormats()

获取用于以前设置的模式字符串中格式元素的格式。

GetFormatsByArgumentIndex()

获取用于传入 format 方法或从 parse 方法返回的值的格式。

GetHashCode()

返回对象的哈希代码值。

(继承自 Object)
JavaFinalize()

当垃圾回收确定不再引用该对象时,由对象上的垃圾回收器调用。

(继承自 Object)
Notify()

唤醒正在等待此对象的监视器的单个线程。

(继承自 Object)
NotifyAll()

唤醒正在等待此对象的监视器的所有线程。

(继承自 Object)
Parse(String, ParsePosition)

分析字符串。

Parse(String)

分析给定字符串开头的文本以生成对象数组。

ParseObject(String, ParsePosition)

分析字符串中的文本以生成对象数组。

ParseObject(String)

分析给定字符串开头的文本以生成对象。

(继承自 _Format)
SetFormat(Int32, _Format)

设置在以前设置的模式字符串中具有给定格式元素索引的格式元素所使用的格式。

SetFormatByArgumentIndex(Int32, _Format)

设置用于以前设置的模式字符串中使用给定参数索引的格式元素的格式。

SetFormats(_Format[])

设置要用于以前设置模式字符串中的格式元素的格式。

SetFormatsByArgumentIndex(_Format[])

设置要用于传入 format 方法或从 parse 方法返回的值的格式。

SetHandle(IntPtr, JniHandleOwnership)

设置 Handle 属性。

(继承自 Object)
ToArray<T>()

MessageFormat 提供一种以非特定语言方式生成串联消息的方法。

(继承自 Object)
ToPattern()

返回表示消息格式的当前状态的模式。

ToString()

返回对象的字符串表示形式。

(继承自 Object)
UnregisterFromRuntime()

MessageFormat 提供一种以非特定语言方式生成串联消息的方法。

(继承自 Object)
Wait()

使当前线程等待,直到唤醒它,通常是通过 em 通知/em< 或 >em<interrupted>/em<。><>

(继承自 Object)
Wait(Int64, Int32)

使当前线程等待直到唤醒,通常是通过 <em>通知</em> 或 <em interrupted>/em<>,或直到经过一定数量的实时。

(继承自 Object)
Wait(Int64)

使当前线程等待直到唤醒,通常是通过 <em>通知</em> 或 <em interrupted>/em<>,或直到经过一定数量的实时。

(继承自 Object)

显式接口实现

名称 说明
IJavaPeerable.Disposed()

MessageFormat 提供一种以非特定语言方式生成串联消息的方法。

(继承自 Object)
IJavaPeerable.DisposeUnlessReferenced()

MessageFormat 提供一种以非特定语言方式生成串联消息的方法。

(继承自 Object)
IJavaPeerable.Finalized()

MessageFormat 提供一种以非特定语言方式生成串联消息的方法。

(继承自 Object)
IJavaPeerable.JniManagedPeerState

MessageFormat 提供一种以非特定语言方式生成串联消息的方法。

(继承自 Object)
IJavaPeerable.SetJniIdentityHashCode(Int32)

MessageFormat 提供一种以非特定语言方式生成串联消息的方法。

(继承自 Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates)

MessageFormat 提供一种以非特定语言方式生成串联消息的方法。

(继承自 Object)
IJavaPeerable.SetPeerReference(JniObjectReference)

MessageFormat 提供一种以非特定语言方式生成串联消息的方法。

(继承自 Object)

扩展方法

名称 说明
GetJniTypeName(IJavaPeerable)

获取实例 self类型的 JNI 名称。

JavaAs<TResult>(IJavaPeerable)

尝试强制self键入TResult,检查强制是否在Java端有效。

JavaCast<TResult>(IJavaObject)

执行 Android 运行时检查的类型转换。

JavaCast<TResult>(IJavaObject)

MessageFormat 提供一种以非特定语言方式生成串联消息的方法。

TryJavaCast<TResult>(IJavaPeerable, TResult)

尝试强制self键入TResult,检查强制是否在Java端有效。

适用于