我在字符串中輸入以下數據:「Hello#this#is#sample#text。」從可擴展CharSequence中刪除字符
它爲#個字符之間的所有元素設置背景顏色。這是我到目前爲止:
public static CharSequence colorBackground(CharSequence text) {
Pattern pattern = Pattern.compile("#(.*?)#");
Spannable spannable = new SpannableString(text);
if(pattern != null)
{
Matcher matcher = pattern.matcher(text);
while(matcher.find())
{
int start = matcher.start();
int end = matcher.end();
CharacterStyle span = new BackgroundColorSpan(0xFF404040);
spannable.setSpan(span, start, end, 0);
}
}
return spannable;
}
設置背景顏色的作品,但佔位符字符#的樣式也是如此。如何在返回結果之前刪除它們,因爲CharSequence中不存在方法ReplaceAll?
我使用這個函數在ListView中設置TextView行的樣式。添加這個樣式函數後,模擬器感覺有點慢。也許我應該以其他方式來處理它,例如使用具有自定義繪圖功能的自定義TextView?
感謝您的時間,Spannables在啓動時有點混亂。至少它比HTML工作得更快。 – joshas 2012-01-10 19:20:54