Share via


IFormattable 接口

定义

接口Formattable必须由需要使用 的转换说明符java.util.Formatter执行自定义格式's'的任何类实现。

[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必须由需要使用 的转换说明符java.util.Formatter执行自定义格式's'的任何类实现。 此接口允许对任意对象进行格式设置的基本控制。

例如,以下类根据标志和长度约束打印出股票名称的不同表示形式:

{@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."
            }

格式化表对于多线程访问不一定是安全的。 线程安全性是可选的,可由扩展和实现此接口的类强制执行。

除非另行指定,否则将参数传递给 null 此接口中的任何方法都将导致 NullPointerException 引发 。

在 1.5 中添加。

java.util.FormattableJava 文档。

此页面的部分内容是基于 创建和共享的工作进行的修改,并根据 署名许可中所述的术语使用。

属性

Handle

获取基础 Android 对象的 JNI 值。

(继承自 IJavaObject)
JniIdentityHashCode

返回包装实例的 的值 java.lang.System.identityHashCode()

(继承自 IJavaPeerable)
JniManagedPeerState

托管对等方的状态。

(继承自 IJavaPeerable)
JniPeerMembers

成员访问和调用支持。

(继承自 IJavaPeerable)
PeerReference

返回 JniObjectReference 包装的 Java 对象实例的 。

(继承自 IJavaPeerable)

方法

Disposed()

释放实例时调用。

(继承自 IJavaPeerable)
DisposeUnlessReferenced()

如果没有对此实例的未完成引用,则调用 Dispose();否则,不执行任何操作。

(继承自 IJavaPeerable)
Finalized()

在实例完成时调用。

(继承自 IJavaPeerable)
FormatTo(Formatter, FormatFlags, Int32, Int32)

使用提供的 Formatter formatter设置 对象的格式。

SetJniIdentityHashCode(Int32)

设置 返回 JniIdentityHashCode的值。

(继承自 IJavaPeerable)
SetJniManagedPeerState(JniManagedPeerStates)

接口Formattable必须由需要使用 的转换说明符java.util.Formatter执行自定义格式's'的任何类实现。

(继承自 IJavaPeerable)
SetPeerReference(JniObjectReference)

设置 返回 PeerReference的值。

(继承自 IJavaPeerable)
UnregisterFromRuntime()

取消注册此实例,以便运行时不会从将来 Java.Interop.JniRuntime+JniValueManager.PeekValue 的调用中返回它。

(继承自 IJavaPeerable)

扩展方法

JavaCast<TResult>(IJavaObject)

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

JavaCast<TResult>(IJavaObject)

接口Formattable必须由需要使用 的转换说明符java.util.Formatter执行自定义格式's'的任何类实现。

GetJniTypeName(IJavaPeerable)

接口Formattable必须由需要使用 的转换说明符java.util.Formatter执行自定义格式's'的任何类实现。

适用于