/** * @param target 需要高亮的文字 */ public void highlight(String target){ String temp = getText().toString(); SpannableStringBuilder spannable = new SpannableStringBuilder(temp); CharacterStyle span = null ; Pattern p = Pattern.compile(target); Matcher m = p.matcher(temp); while (m.find()) { span = new ForegroundColorSpan(Color.RED);//span = new ImageSpan(drawable,ImageSpan.XX);//设置图片 spannable.setSpan(span, m.start(), m.end(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } setText(spannable); }
设置TextView的关键字高亮
最新推荐文章于 2021-05-25 16:15:56 发布