android 如何让文本中某个关键字高亮显示?

TextView tv = (TextView) findViewById(R.id.hello);
SpannableString s = new SpannableString("abcdef");
//关键字
Pattern p = Pattern.compile("abc");

Matcher m = p.matcher(s);

while (m.find()) {
int start = m.start();
int end = m.end();
s.setSpan(new ForegroundColorSpan(Color.RED), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
tv.setText(s);

你可能感兴趣的:(android)