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)//查找对应的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)
原文:https://www.cnblogs.com/yongfengnice/p/11880349.html