首页 > 代码库 > Android @好友解析&&改变颜色
Android @好友解析&&改变颜色
private static SpannableStringBuilder afterGetName(String str) {
List<Integer> starts = new ArrayList<Integer>();
List<Integer> ends = new ArrayList<Integer>();
SpannableStringBuilder result = new SpannableStringBuilder(str);
Pattern pattern1 = Pattern.compile("@[\\[].+?[:].+?[\\]]");
Matcher matcher1 = pattern1.matcher(result);
while (matcher1.find()){
int i = matcher1.start();
starts.add(i);
String currentStr = matcher1.group();
String[] name = currentStr.split(":");
String name1 = name[1];
StringBuilder stringBuilder = new StringBuilder(name1);
stringBuilder.delete(name1.length()-1,name1.length());
stringBuilder.insert(0, "@");
String str1 = matcher1.replaceFirst(stringBuilder.toString());
int j = i+stringBuilder.length();
ends.add(j);
result = new SpannableStringBuilder(str1);
matcher1 = pattern1.matcher(result);
}
for(int m = 0 ; m < starts.size(); m++){
ForegroundColorSpan span = new ForegroundColorSpan(Color.parseColor("#ff548dff"));
int j = starts.get(m);
int k = ends.get(m);
result.setSpan(span, j, k, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return result;
}