charsequence在android中的格式化方法

小樊
83
2024-08-08 12:29:40

在Android中,可以使用以下方法來(lái)格式化CharSequence:

  1. 使用String.format()方法:
CharSequence str = "Hello";
String formattedStr = String.format("Formatted string: %s", str);
  1. 使用StringBuilder類:
CharSequence str = "World";
StringBuilder builder = new StringBuilder();
builder.append("Formatted string: ");
builder.append(str);
String formattedStr = builder.toString();
  1. 使用SpannableStringBuilder類來(lái)添加格式化樣式:
CharSequence str = "Android";
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append("Formatted string: ");
SpannableString spannableStr = new SpannableString(str);
spannableStr.setSpan(new ForegroundColorSpan(Color.RED), 0, str.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.append(spannableStr);
CharSequence formattedStr = builder;

0