給雙引號中的內容加彩色:
private void highlightSearchHint(Spannable hintSpan){
String hint = hintSpan.toString();
Log.i(TAG, "highlightSearchHint " + hint);
int index = -1;
int start = 0;
boolean foundStartQuote = false;
do {
index = hint.indexOf("/"", index + 1);
if (foundStartQuote && index != -1) {
foundStartQuote = false;
Log.i(TAG, "highlight from " + (start+1) + " to " + index);
/*每次setSpan必須用一個全新的ForegroundColorSpan對象*/
ForegroundColorSpan hintColor = new ForegroundColorSpan(Color.parseColor("#01A8FF"));
hintSpan.setSpan(hintColor, start + 1, index,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} else if (!foundStartQuote && index != -1) {
foundStartQuote = true;
start = index;
}
} while (index != -1);
}
//建立一個 SpannableString對象
SpannableString sp = new
SpannableString(
"點擊Google連結:斜體."
);
//在2-3兩個字元上設定超連結
sp.setSpan(new
URLSpan(
"http://www.google.com"
),
2
,
4
,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//在7-8兩個字元上設定斜體
sp.setSpan(new
StyleSpan(android.graphics.Typeface.BOLD_ITALIC),
7
,
9
, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);