MessageFormat 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
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 取一組物件,格式化後,將格式化字串插入模式中適當的位置。
<strong>Note:</strong>MessageFormat 與其他 Format 類別不同的是,你是用其中一個建構子來建立 MessageFormat 物件(不是用 getInstance style factory 方法)。 工廠方法並非必需,因為 MessageFormat 工廠本身並未實作特定區域的行為。 任何區域特定的行為都由你提供的模式以及用於插入參數的子格式所定義。
<h2>「模式」模式及其>詮釋</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>
</區塊引用>
在 字串中,一對單引號可以用來引用任意字元,唯獨不包括單引號。 例如,pattern 字串 "'{0}'" 代表字串 "{0}",而非 FormatElement。 單一引號本身必須由字''中雙重的單引號表示。 例如,圖案串 "'{''}'" 可解釋為 '{ (引號開始與左捲括 '' )、(單引號)、( }' 右捲括及引號結束)、 <em>not</em>'{' 和 '}' (引用左與右捲括):代表字串 "{'}"、 <em>not</em>"{}"。
SubformatPattern 會依其對應的子格式來解讀,並適用依子格式而定的模式規則。 例如,pattern string "{1,number,<u>$'#',##</u>}" (底線為 SubformatPattern )會產生一個帶有引號的數字格式,結果如下: "$#31,45"。 詳情請參閱各 Format 子類別文件。
任何未匹配的報價在給定模式結束時視為已封閉。 例如,模式字串 "'{0「} 會被視為模式 "'{0'」}。
任何捲髮在未標註的花紋內都必須保持平衡。 例如,和 "ab {0} de""ab '}' de" 是有效的模式,但 "ab {0'}' de"、和 "ab } de""''{''" 不是。
<dl><dt><b>警告:</b><dd>訊息格式中使用引號的規則,遺憾地顯示出有些令人困惑。 特別是,當地化人員並不總是能清楚判斷單引號是否需要加倍。 務必告知本地化人員規則,並告知他們(例如利用資源包原始碼檔案中的註解)哪些字串會被 MessageFormat處理。 請注意,本地化工具可能需要在翻譯字串中使用單引號,而原版沒有。
</dl>
ArgumentIndex 值是一個非負整數,使用'0'透過 '9'的數字寫入,代表arguments傳給format方法的陣列索引或方法回傳parse的結果陣列。
FormatType 與 FormatStyle 值用於建立 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”>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>number<th scope=“row” style=“text-weight: normal”>(none)<td>NumberFormat#getInstance(Locale) NumberFormat.getInstance(getLocale())<tr><th scope=“row” style=“text-weight: normal”>integer<td>NumberFormat#getIntegerInstance(Locale) NumberFormat.getIntegerInstance(getLocale())<tr><th scope =“row” style=“text-weight: normal”>currency<>NumberFormat#getCurrencyInstance(Locale) NumberFormat.getCurrencyInstance(getLocale())<翻譯><th scope=「row」樣式=「text-weight: normal」td>percent<>NumberFormat#getPercentInstance(Locale) NumberFormat.getPercentInstance(getLocale())< tr><th scope=「row」樣式=「text-weight: normal」>< td>newDecimalFormat#DecimalFormat(String,DecimalFormatSymbols) DecimalFormat(subformatPattern,DecimalFormatSymbols#getInstance(Locale) DecimalFormatSymbols.getInstance(getLocale()))<tr><th scope=「row」style=「text-weight: normal」rowspan=6th>date<scope=「row」style=「row」style=「text-weight: normal」(>none)<td>DateFormat#getDateInstance(int,Locale) DateFormat.getDateInstance(DateFormat#DEFAULT, getLocale())<tr><th scope=「row」樣式=「text-weight」: normal“>short<td>DateFormat#getDateInstance(int,Locale) DateFormat.getDateInstance(DateFormat#SHORT, getLocale())<tr><th scope=”row“ style=”text-weight: normal“>medium<td>DateFormat#getDateInstance(int,Locale) DateFormat.getDateInstance(DateFormat#DEFAULT, getLocale())<tr><th scope=”row“ style=”text-weight: normal“td>long<>DateFormat#getDateInstance(int,Locale) DateFormat.getDateInstance(trDateFormat#LONG, getLocale())th scope=”row“ style=”text-weight<><>full<>DateFormat#getDateInstance(int,Locale) DateFormat.getDateInstance(DateFormat#FULL, getLocale()): normal”<><scope=“row” style=“text-weight: normal”>SubformatPattern<td>newSimpleDateFormat#SimpleDateFormat(String,Locale) SimpleDateFormat(subformatPattern, getLocale())<tr><th scope=“row” style=“text-weight: normal” rowspan=6th>time<scope=“row” style=“text-weight: normal”>(none)<td>DateFormat#getTimeInstance(int,Locale) DateFormat.getTimeInstance(DateFormat#DEFAULT, getLocale())<tr><th scope=“row” style=“text-weight: normal”>short<td tr>DateFormat#getTimeInstance(int,Locale) DateFormat.getTimeInstance(DateFormat#SHORT, getLocale())<><th scope=「row」樣式=「text-weight: normal」td>medium<>DateFormat#getTimeInstance(int,Locale) DateFormat.getTimeInstance(DateFormat#DEFAULT, getLocale())< tr><scope = 「row」style=「text-weight: normal」td>long<>DateFormat#getTimeInstance(int,Locale) DateFormat.getTimeInstance(DateFormat#LONG, getLocale())< tr><th scope=「row」style=「text-weight: normal」>full<>DateFormat#getTimeInstance(int,Locale) DateFormat.getTimeInstance(DateFormat#FULL, getLocale())<tr th><scope=「row」style=「text-weight: normal」 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”>choice<th scope=“row” style=“text-weight: normal”>SubformatPattern <td>newChoiceFormat#ChoiceFormat(String) ChoiceFormat(subformatPattern)</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.
</區塊引用>
以下範例建立一個 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).
</區塊引用>
對於更複雜的模式,你可以使用 a ChoiceFormat 來產生單數和複數的正確形式: <blockquote>
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.
</區塊引用>
你可以像上面的例子一樣,用程式化的方式建立, ChoiceFormat 或是用模式來建立。 如需相關資訊,請參閱 ChoiceFormat 。
<引用>
{@code
form.applyPattern(
"There {0,choice,0#are no files|1#is one file|1<are {0,number,integer} files}.");
}
</區塊引用>
<強>註:</strong> 如上所述,由 a ChoiceFormat 在 中 MessageFormat 產生的字串被視為特殊字串;出現 '{' 用來表示子格式,並造成遞迴。 如果你同時用程式方式建立 a MessageFormat 和 ChoiceFormat (而不是用字串模式),那就要小心不要產生會自我遞迴的格式,否則會造成無限迴圈。
當同一個參數在字串中被解析多次時,最後一次的匹配將是解析的最終結果。 例如, <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)}
</區塊引用>
同樣地,使用包含多次相同參數出現的模式來解析 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")}
</區塊引用>
<h3>「同步」同步/><h3>
訊息格式並不同步。 建議為每個執行緒建立獨立格式實例。 若多個執行緒同時存取同一格式,必須在外部同步。
在 1.1 版本中加入。
Java 文件 java.text.MessageFormat。
本頁部分內容為基於 Open Source Project 所創建與分享的作品,並依授權條款所描述的使用進行修改。
建構函式
| 名稱 | Description |
|---|---|
| MessageFormat(IntPtr, JniHandleOwnership) |
建立 JNI 物件的 Managed 表示法時使用的建構函式;由運行時間呼叫。 |
| MessageFormat(String, Locale) |
為指定的地點與模式構建 MessageFormat。 |
| MessageFormat(String) |
為預設 |
屬性
| 名稱 | Description |
|---|---|
| Class |
傳回這個 |
| Handle |
基礎Android實例的句柄。 (繼承來源 Object) |
| JniIdentityHashCode |
取得由互通執行時指派給此 Java 對等端的身份雜湊碼。 (繼承來源 Object) |
| JniManagedPeerState |
|
| JniPeerMembers |
|
| Locale |
它會取得建立或比較子格式時所使用的地點。 -或- 設定用於建立或比較子格式的地點。 |
| PeerReference |
取得這個 Java 節點的 JNI 物件參考。 (繼承來源 Object) |
| ThresholdClass |
此 API 支援 Mono for Android 基礎架構,並非直接從你的程式碼中使用。 |
| ThresholdType |
此 API 支援 Mono for Android 基礎架構,並非直接從你的程式碼中使用。 |
方法
| 名稱 | Description |
|---|---|
| ApplyPattern(String) |
設定此訊息格式所採用的模式。 |
| Clone() |
建立並傳回這個 對象的複本。 (繼承來源 _Format) |
| Construct(JniObjectReference, JniObjectReferenceOptions) |
|
| Dispose() |
釋放該 Java 節點所持有的資源。 (繼承來源 Object) |
| Dispose(Boolean) |
釋放該 Java 節點所持有的資源。 (繼承來源 Object) |
| DisposeUnlessReferenced() |
|
| Equals(Object) |
|
| Equals(Object) |
指出其他物件是否「等於」這個物件。 (繼承來源 Object) |
| Format(Object, StringBuffer, FieldPosition) |
格式化物件陣列,並將 |
| Format(Object) |
格式化物件以產生字串。 (繼承來源 _Format) |
| Format(Object[], StringBuffer, FieldPosition) |
格式化物件陣列,並將 |
| Format(String, Object[]) |
格式化物件陣列,並將程式碼<MessageFormat>/code< 的模式(>格式元素替換為格式化物件)附加到提供的<程式碼>StringBuffer</code> 上。 |
| FormatToCharacterIterator(Object) |
格式化物件產生 |
| GetFormats() |
取得先前設定的模式字串中格式元素所使用的格式。 |
| GetFormatsByArgumentIndex() |
取得輸入 |
| 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[]) |
設定傳遞給 |
| SetHandle(IntPtr, JniHandleOwnership) |
設定 Handle 屬性。 (繼承來源 Object) |
| SetPeerReference(JniObjectReference, JniObjectReferenceOptions) |
|
| ToArray<T>() |
從這個 Java 陣列包裝器建立一個受管理陣列。 (繼承來源 Object) |
| ToPattern() |
回傳一個代表訊息格式當前狀態的模式。 |
| ToString() |
傳回物件的字串表示。 (繼承來源 Object) |
| UnregisterFromRuntime() |
將此 Java 節點從互通執行時中取消註冊。 (繼承來源 Object) |
| Wait() |
讓目前線程等候直到喚醒為止,通常是藉由em <notified/em>或<em>interrupted</em> 來喚醒它。<> (繼承來源 Object) |
| Wait(Int64, Int32) |
讓目前的線程等到喚醒為止,通常是因為 <em>notified</em> 或 <em>interrupted</em>,或直到經過一定數量的實時為止。 (繼承來源 Object) |
| Wait(Int64) |
讓目前的線程等到喚醒為止,通常是因為 <em>notified</em> 或 <em>interrupted</em>,或直到經過一定數量的實時為止。 (繼承來源 Object) |
明確介面實作
| 名稱 | Description |
|---|---|
| IJavaPeerable.Disposed() |
|
| IJavaPeerable.Finalized() |
|
| IJavaPeerable.JniObjectReferenceControlBlock |
|
| IJavaPeerable.SetJniIdentityHashCode(Int32) |
|
| IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) |
|
| IJavaPeerable.SetPeerReference(JniObjectReference) |
|
| IJavaPeerable.UnregisterFromRuntime() |
|
擴充方法
| 名稱 | Description |
|---|---|
| GetJniTypeName(IJavaPeerable) |
取得實例 |
| JavaAs<TResult>(IJavaPeerable) |
試著強制 |
| JavaCast<TResult>(IJavaObject) |
執行 Android 執行時間檢查的類型轉換。 |
| JavaCast<TResult>(IJavaObject) |
|
| TryJavaCast<TResult>(IJavaPeerable, TResult) |
試著強制 |