【发布时间】:2011-11-13 21:56:55
【问题描述】:
我正在尝试使用此函数在 Android 中使用 spannableString 和正则表达式为字符串的一部分着色:
public static String StringReplace(String source) {
String find = "ABC";
SpannableString replace = new SpannableString(find);
replace.setSpan(new ForegroundColorSpan(Color.RED), 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
String output = source.replace(find, replace);
return output;
}
因为函数 replace() 返回一个字符串,所以我无法获得彩色字符串。我的问题是:使用正则表达式为部分文本着色的最佳方法是什么?
【问题讨论】: