android 如何让文本中某个关键字高亮显示?
TextView tv = (TextView) findViewById(R.id.hello);
SpannableString s = new SpannableString(getResources().getString(R.string.linkify));
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);
- SpannableStringBuilder style = new SpannableStringBuilder("test Height light");
- //参数一:高亮颜色[ForegroundColorSpan前景色]
- //from:高亮开始
- //to:高亮结束
- style.setSpan(new ForegroundColorSpan(color), from, to, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- (TextView)view.setText(style);
/*******************************************************/