Android - 關鍵字比對字串並變色

//可以比對關鍵字並變色
public static SpannableString matcherSearchText(int color, String text, String keyword) {
   SpannableString ss = new SpannableString(text);
   Pattern pattern = Pattern.compile(keyword);
   Matcher matcher = pattern.matcher(ss);
   while (matcher.find()) {
      int start = matcher.start();
      int end = matcher.end();
      ss.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
   }
   return ss;
}

來源

iamshawn