其他分享
首页 > 其他分享> > android开发去掉TextView使用html.from里面的a链接的下划线的方法

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)

 

标签:spannable,span,下划线,val,text,ClickURLSpan,html,android,textView
来源: https://www.cnblogs.com/yongfengnice/p/11880349.html