从textview中的可点击电子邮件中删除下划线

时间:2017-08-08 11:38:45

标签: android

我正在开发一个Android应用,我在textView的文本中添加了邮件,并且可以点击。我想从邮件中删除下划线。怎么做?

 <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="@dimen/mailnlink"
    android:textColor="@color/mltext"
    android:textColorLink="@color/link"
    android:textStyle="italic"
    android:gravity="center"
    android:autoLink="email"
    android:background="@color/mlb"
    android:text="@string/f2"/>

3 个答案:

答案 0 :(得分:0)

以下是

private void stripUnderlines(TextView textView) {
        Spannable s = new SpannableString(textView.getText());
        URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
        for (URLSpan span: spans) {
            int start = s.getSpanStart(span);
            int end = s.getSpanEnd(span);
            s.removeSpan(span);
            span = new URLSpanNoUnderline(span.getURL());
            s.setSpan(span, start, end, 0);
        }
        textView.setText(s);
    }

 private class URLSpanNoUnderline extends URLSpan {
        public URLSpanNoUnderline(String url) {
            super(url);
        }
        @Override public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setUnderlineText(false);
        }
    }

Remove underline from links in TextView - Android

答案 1 :(得分:0)

这里有一个 Kotlin 扩展函数作为删除所有 UrlSpan 下划线的解决方案:

private fun Spannable.removeAllUrlSpanUnderline() {
    for (urlSpan in getSpans(0, length, URLSpan::class.java)) {
        setSpan(object : UnderlineSpan() {
            override fun updateDrawState(tp: TextPaint) {
                tp.isUnderlineText = false
            }
        }, getSpanStart(urlSpan), getSpanEnd(urlSpan), 0)
    }
}

就我而言,我从一个带有 href 标签的字符串开始。 fromHtml 返回一个 Spanned,因此将其转换为 Spannable 以使其可变。请参阅下面的示例使用:

val sampleHtmlString = "<a href=\"www.google.com\">first Link</a> and <a href=\"www.google.com\">second link</a>"

val sampleSpannable = HtmlCompat.fromHtml(sampleHtmlString, HtmlCompat.FROM_HTML_MODE_LEGACY) as Spannable

val sampleSpannableWithoutUnderlines = sampleSpannable.removeAllUrlSpanUnderline()

现在您可以自由地将 sampleSpannableWithoutUnderlines 设置为您的 TextView

答案 2 :(得分:-1)

您只需将clickable添加到textView

即可
<TextView
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:textSize="@dimen/mailnlink"
   android:textColor="@color/mltext"
   android:textColorLink="@color/link"
   android:textStyle="italic"
   android:gravity="center"
   android:clickable="true"      
   android:background="@color/mlb"
   android:text="@string/f2"/>