IFormattable 介面
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
Formattable介面必須由任何需要使用轉換's'規範符java.util.Formatter進行自訂格式化的類別實作。
[Android.Runtime.Register("java/util/Formattable", "", "Java.Util.IFormattableInvoker")]
public interface IFormattable : Android.Runtime.IJavaObject, IDisposable, Java.Interop.IJavaPeerable
[<Android.Runtime.Register("java/util/Formattable", "", "Java.Util.IFormattableInvoker")>]
type IFormattable = interface
interface IJavaObject
interface IDisposable
interface IJavaPeerable
- 屬性
- 實作
備註
Formattable介面必須由任何需要使用轉換's'規範符java.util.Formatter進行自訂格式化的類別實作。 此介面允許對任意物件格式化進行基本控制。
例如,以下類別會根據旗標與長度限制,列印出不同的股票名稱表示:
{@code
import java.nio.CharBuffer;
import java.util.Formatter;
import java.util.Formattable;
import java.util.Locale;
import static java.util.FormattableFlags.*;
...
public class StockName implements Formattable {
private String symbol, companyName, frenchCompanyName;
public StockName(String symbol, String companyName,
String frenchCompanyName) {
...
}
...
public void formatTo(Formatter fmt, int f, int width, int precision) {
StringBuilder sb = new StringBuilder();
// decide form of name
String name = companyName;
if (fmt.locale().equals(Locale.FRANCE))
name = frenchCompanyName;
boolean alternate = (f & ALTERNATE) == ALTERNATE;
boolean usesymbol = alternate || (precision != -1 && precision < 10);
String out = (usesymbol ? symbol : name);
// apply precision
if (precision == -1 || out.length() < precision) {
// write it all
sb.append(out);
} else {
sb.append(out.substring(0, precision - 1)).append('*');
}
// apply width and justification
int len = sb.length();
if (len < width)
for (int i = 0; i < width - len; i++)
if ((f & LEFT_JUSTIFY) == LEFT_JUSTIFY)
sb.append(' ');
else
sb.insert(0, ' ');
fmt.format(sb.toString());
}
public String toString() {
return String.format("%s - %s", symbol, companyName);
}
}
}
當與 java.util.Formatter結合使用時,上述類別會產生以下各種格式字串的輸出。
{@code
Formatter fmt = new Formatter();
StockName sn = new StockName("HUGE", "Huge Fruit, Inc.",
"Fruit Titanesque, Inc.");
fmt.format("%s", sn); // -> "Huge Fruit, Inc."
fmt.format("%s", sn.toString()); // -> "HUGE - Huge Fruit, Inc."
fmt.format("%#s", sn); // -> "HUGE"
fmt.format("%-10.8s", sn); // -> "HUGE "
fmt.format("%.12s", sn); // -> "Huge Fruit,*"
fmt.format(Locale.FRANCE, "%25s", sn); // -> " Fruit Titanesque, Inc."
}
formattables 不一定適合多執行緒存取。 執行緒安全是可選的,並可由擴充並實作此介面的類別強制執行。
除非另有說明,將參數傳遞 null 給此介面中的任何方法都會導致拋出 a NullPointerException 。
已在1.5中新增。
的 java.util.FormattableJava 檔。
本頁部分內容為基於 Open Source Project 所創建與分享的作品,並依授權條款所描述的使用進行修改。
屬性
| 名稱 | Description |
|---|---|
| Handle |
取得底層 Android 物件的 JNI 值。 (繼承來源 IJavaObject) |
| JniIdentityHashCode |
回傳包裹實例的 |
| JniManagedPeerState |
管理貴族的狀況。 (繼承來源 IJavaPeerable) |
| JniObjectReferenceControlBlock |
|
| JniPeerMembers |
成員存取與召喚支援。 (繼承來源 IJavaPeerable) |
| PeerReference |
回傳JniObjectReference包裹後的 Java 物件實例。 (繼承來源 IJavaPeerable) |
方法
| 名稱 | Description |
|---|---|
| Disposed() |
當實例被處理後才被召喚。 (繼承來源 IJavaPeerable) |
| DisposeUnlessReferenced() |
如果沒有未解決的參考資料,則 |
| Finalized() |
當實例完成後才會被通知。 (繼承來源 IJavaPeerable) |
| FormatTo(Formatter, FormatFlags, Int32, Int32) |
使用 提供的 |
| SetJniIdentityHashCode(Int32) |
將回傳的值設為 |
| SetJniManagedPeerState(JniManagedPeerStates) |
|
| SetPeerReference(JniObjectReference) |
將回傳的值設為 |
| UnregisterFromRuntime() |
取消註冊此實例,讓執行時不會在未來 Java.Interop.JniRuntime+JniValueManager.PeekValue 的呼叫中回傳該實例。 (繼承來源 IJavaPeerable) |
擴充方法
| 名稱 | Description |
|---|---|
| GetJniTypeName(IJavaPeerable) |
取得實例 |
| JavaAs<TResult>(IJavaPeerable) |
試著強制 |
| JavaCast<TResult>(IJavaObject) |
執行 Android 執行時檢查型別轉換。 |
| JavaCast<TResult>(IJavaObject) |
|
| TryJavaCast<TResult>(IJavaPeerable, TResult) |
試著強制 |