Now there is such a need, after inputting the text to search for the relevant account, the effect is as shown in the figure (the searched text is displayed in blue), what should I do?
//TextView,要显示文字的控件,text搜索到的账号的名字,searchWord,搜索的文字
fun setText(text: String, view: TextView) {
if (TextUtils.isEmpty(searchWord) || TextUtils.isEmpty(text)) {
view.text = text
return
}
var ssb = SpannableStringBuilder(text)
val pattern = Pattern.compile(searchWord)
val matcher = pattern.matcher(text)
while (matcher.find()) {
var span = ForegroundColorSpan(ColorUtils.getColor(R.color.color_2E7BFD))
ssb.setSpan(span, matcher.start(), matcher.end(), Spannable.SPAN_MARK_MARK)
}
view.text = ssb
Log.d("名字", "名字1:${ssb}")
}