android开发去掉TextView使用html.from里面的a链接的下划线的方法

class ClickURLSpan(url: String) : URLSpan(url) {
companion object {
fun stripUnderline(textView: TextView) {
textView.movementMethod = LinkMovementMethod.getInstance()
if (textView.text is Spannable) {
val spannable = textView.text as Spannable
val allSpans = spannable.getSpans(0, spannable.length, URLSpan::class.java)//查找<a></a>对应的URLSpan
for (span in allSpans) {
val start = spannable.getSpanStart(span)
val end = spannable.getSpanEnd(span)
spannable.removeSpan(span)
spannable.setSpan(ClickURLSpan(span.url), start, end, 0)//替换为自定义的ClickURLSpan
}
textView.autoLinkMask = 0
textView.text = spannable
}
}
}
override fun updateDrawState(ds: TextPaint) {
super.updateDrawState(ds)
ds.linkColor = ContextCompat.getColor(BaseLib.getContext(), R.color.text_primary)//设置a链接颜色
ds.isUnderlineText = false  //去掉a链接下划线
}
override fun onClick(widget: View) {
LogUtils.d("ClickURLSpan onClick:$url")
}
}
使用方法:ClickURLSpan.stripUnderline(textview)

 

上一篇:Android语音转文本::实时更新文本


下一篇:渲染问题找不到以下类-android.support.v7.widget.AppCompatTextView