//通过正则匹配出关键字,然后使用span使之变色高亮
private SpannableString getHighLightSs(String text, String keyword, int color) {
SpannableString span = new SpannableString(text);
Pattern pattern = Pattern.compile(keyword);
Matcher matcher = pattern.matcher(span);
while (matcher.find()) {
span.setSpan(new ForegroundColorSpan(color), matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return span;
}

TextView tv = findViewById(R.id.tv);
tv.setText(getHighLightSs(text,keyword,R.color.red));

相关文章:

  • 2021-04-29
  • 2021-06-22
  • 2021-12-19
  • 2022-02-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-06-12
  • 2022-12-23
  • 2021-12-23
  • 2021-07-27
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案