1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
| package com.xiasuhuei321.forspanstring;
import android.text.SpannableString; import android.text.SpannableStringBuilder; import android.text.Spanned; import android.text.TextPaint; import android.text.TextUtils; import android.text.style.ClickableSpan; import android.text.style.ForegroundColorSpan; import android.view.View;
import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern;
public class RichTextUtil {
public static CharSequence getColorString(String originText, String keyword, int color) { return getColorString(originText, keyword, color, null); }
public static CharSequence getColorString(String originText, String keyword, int color, final View.OnClickListener listener) { SpannableString s = new SpannableString(originText); Pattern p = Pattern.compile(keyword); Matcher m = p.matcher(s);
while (m.find()) { int start = m.start(); int end = m.end(); s.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
if (listener != null) { s.setSpan(new ClickableSpan() { @Override public void onClick(View widget) { listener.onClick(widget); }
@Override public void updateDrawState(TextPaint ds) { ds.setColor(color); ds.setUnderlineText(false); } }, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } }
return s; }
public static CharSequence getColorString(String originText, List<String> keywords, Map<String, Integer> colorMap) { return getColorString(originText, keywords, colorMap, null); }
public static CharSequence getColorString(String originText, List<String> keywords, final Map<String, Integer> colorMap, Map<String, View.OnClickListener> listenerMap) { SpannableString s = new SpannableString(originText);
for (int i = 0; i < keywords.size(); i++) { final String keyword = keywords.get(i); Pattern p = Pattern.compile(keyword); Matcher m = p.matcher(s);
while (m.find()) { int start = m.start(); int end = m.end();
s.setSpan(new ForegroundColorSpan(colorMap.get(keyword)), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
if (listenerMap != null) { final View.OnClickListener listener = listenerMap.get(keyword); if (listener != null) { s.setSpan(new ClickableSpan() { @Override public void onClick(View widget) { listener.onClick(widget); }
@Override public void updateDrawState(TextPaint ds) { ds.setColor(colorMap.get(keyword)); ds.setUnderlineText(false); } }, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } } }
return s; }
private SpannableStringBuilder builder = null;
public RichTextUtil append(String str) { return append(str, -1, null); }
public RichTextUtil append(String str, int color) { return append(str, color, null); }
public RichTextUtil append(String str, int color, View.OnClickListener listener) { if (TextUtils.isEmpty(builder)) { builder = new SpannableStringBuilder(); }
if (TextUtils.isEmpty(str)) { BlackgagaLogger.debug("传入的str为 null!请检查!"); return null; }
if (color == -1) { builder.append(str); return this; }
SpannableString span = new SpannableString(str); span.setSpan(new ForegroundColorSpan(color), 0, str.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
if (listener != null) { span.setSpan(new ClickableSpan() { @Override public void onClick(View widget) { listener.onClick(widget); }
@Override public void updateDrawState(TextPaint ds) { ds.setColor(color); ds.setUnderlineText(false); } }, 0, str.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); }
builder.append(span); return this; }
public SpannableStringBuilder finish() { return builder; }
}
|