简体   繁体   English

如何去掉超链接中的下划线

[英]How to remove underline in hyperlink

I have a TextView on my 'settings' activity:我的“设置”活动中有一个 TextView:

    <TextView
        android:id="@+id/review"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#FFFFFF"
        android:drawablePadding="15dp"
        android:gravity="center_vertical"
        android:text="@string/review"
        android:textColor="#595959"
        android:textColorLink="#595959"
        android:textSize="12sp"
        android:textStyle="bold"
        app:drawableLeftCompat="@drawable/ic_review"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.055"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view"
        app:layout_constraintVertical_bias="0.876" />

I have defined my URL on the string.我已经在字符串上定义了我的 URL 。

<string name="review"><a href="https://play.google.com/store">Submit a review</a></string>

On my settings.kt page I have the following code onCreate:在我的 settings.kt 页面上,我有以下代码 onCreate:

val mTextView = findViewById<TextView>(R.id.review)

mTextView.movementMethod = LinkMovementMethod.getInstance()

From what I have found, I need to add a 'spannable' in relation to the above (settings.kt) but I'm not sure how to apply it, as everyone adds hyperlinks differently.根据我的发现,我需要添加与上述(settings.kt)相关的“spannable”,但我不确定如何应用它,因为每个人添加的超链接都不同。

As you mentioned you have to add spanable first add this function(this is a kotlin extention function for textview):正如您提到的,您必须先添加 spanable 添加此功能(这是一个 kotlin 扩展 function for textview):

fun TextView.removeLinksUnderline() {
val spannable = SpannableString(text)
for (u in spannable.getSpans(0, spannable.length, URLSpan::class.java)) {
    spannable.setSpan(object : URLSpan(u.url) {
        override fun updateDrawState(ds: TextPaint) {
            super.updateDrawState(ds)
            ds.isUnderlineText = false
        }
    }, spannable.getSpanStart(u), spannable.getSpanEnd(u), 0)
}
text = spannable

} }

then to use this function and remove underline type:然后使用这个 function 并删除下划线类型:

replace your textview with correct one(name of your textview)用正确的替换你的 textview(你的文本视图的名称)

txtView.removeLinksUnderline()   

more info about kotlin extention functions: this有关 kotlin 扩展功能的更多信息: this

more about removing hyperlinks: this有关删除超链接的更多信息: this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM