【发布时间】:2020-11-11 23:10:12
【问题描述】:
我想突出显示方括号内的所有子字符串,例如:“[Toto] 正在同时 [做很多] 事情。”
我知道如何extract it。
我知道如何强调:
val str = SpannableString("Toto is doing a lot of stuff at the same time.")
str.setSpan(BackgroundColorSpan(Color.YELLOW), 0, 4, 0)
str.setSpan(BackgroundColorSpan(Color.YELLOW), 8, 22 , 0)
textView.text = str
但问题是我不知道如何同时实现两者。
我显然想在应用高亮效果后删除方括号,但是当我执行 toString() 时,replace() 高亮被删除。
另外,高亮是用索引做的,我不想提取子字符串,而是把它放在原始字符串中,我不知道我应该通过哪种优化方式来实现。
【问题讨论】:
-
“但是当我执行 toString() 时,replace() 会移除高亮”——所以,不要使用
toString()。尝试使用函数 onTextUtils,因为它们与CharSequence一起使用,并且通常保持跨度完好无损。
标签: java android regex kotlin highlight