从TextView中的链接删除下划线 – Android

我使用两个textview来显示数据库的链接,我设法改变链接的颜色,但我想删除下划线

 email.setText(c.getString(5)); website.setText(c.getString(6)); Linkify.addLinks(email, Linkify.ALL); Linkify.addLinks(website, Linkify.ALL); 

我可以从XML或代码做到这一点吗?

您可以在代码中使用不带下划线的版本来查找和replaceURLSpan实例。 在调用Linkify.addLinks() ,调用下面粘贴在每个TextView上的stripUnderlines()函数:

  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); } 

这需要URLSpan的定制版本,它不启用TextPaint的“下划线”属性:

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

给定一个textView和内容:

 TextView textView = (TextView) findViewById(R.id.your_text_view_id); String content = "your <a href='http://some.url'>html</a> content"; 

这里是一个简洁的方法来删除超链接的下划线:

 Spannable s = (Spannable) Html.fromHtml(content); for (URLSpan u: s.getSpans(0, s.length(), URLSpan.class)) { s.setSpan(new UnderlineSpan() { public void updateDrawState(TextPaint tp) { tp.setUnderlineText(false); } }, s.getSpanStart(u), s.getSpanEnd(u), 0); } textView.setText(s); 

这是基于robUx4build议的方法。

为了使链接可点击,您还需要调用:

 textView.setMovementMethod(LinkMovementMethod.getInstance()); 

UnderlineSpan已经存在,但只能设置下划线。

另一个解决scheme是在每个现有的URLSpan上添加一个没有下划线的跨度。 因此,在绘画之前,下划线状态被禁用。 这样你就可以保持你的URLSpan (可能是自定义的)类和其他地方的其他样式。

 public class NoUnderlineSpan extends UnderlineSpan { public NoUnderlineSpan() {} public NoUnderlineSpan(Parcel src) {} @Override public void updateDrawState(TextPaint ds) { ds.setUnderlineText(false); } } 

下面是您如何设置它,而不删除现有的URLSpan对象:

 URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class); for (URLSpan span: spans) { int start = s.getSpanStart(span); int end = s.getSpanEnd(span); NoUnderlineSpan noUnderline = new NoUnderlineSpan(); s.setSpan(noUnderline, start, end, 0); } 

每当尝试删除URL下划线与spannable我build议只有这些东西:

1> 创build自定义类:

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

这需要URLSpan的定制版本,它不启用TextPaint的“下划线”属性

2> 使用spannable文本的setSpan:

 spannableText.setSpan(new URLSpanNoUnderline(UrlText), 0, UrlText.length() , 0); 

这里, spannableTextSpannableString的对象。

我已经实现了一个解决scheme,在我看来,它更加优雅。 我做了一个自定义的TextView 。 这样您就不需要为每个带有超链接的TextView执行额外的代码。

 package com.example.view; import android.content.Context; import android.support.v7.widget.AppCompatTextView; import android.text.Spannable; import android.text.SpannableString; import android.text.style.URLSpan; import android.util.AttributeSet; import com.example.utils.UrlSpanNoUnderline; public class TextViewNoUnderline extends AppCompatTextView { public TextViewNoUnderline(Context context) { this(context, null); } public TextViewNoUnderline(Context context, AttributeSet attrs) { this(context, attrs, android.R.attr.textViewStyle); } public TextViewNoUnderline(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); setSpannableFactory(Factory.getInstance()); } private static class Factory extends Spannable.Factory { private final static Factory sInstance = new Factory(); public static Factory getInstance() { return sInstance; } @Override public Spannable newSpannable(CharSequence source) { return new SpannableNoUnderline(source); } } private static class SpannableNoUnderline extends SpannableString { public SpannableNoUnderline(CharSequence source) { super(source); } @Override public void setSpan(Object what, int start, int end, int flags) { if (what instanceof URLSpan) { what = new UrlSpanNoUnderline((URLSpan) what); } super.setSpan(what, start, end, flags); } } } 

和UrlSpanNoUnderline的代码:

 package com.jankstudios.smmagazine.utils; import android.text.TextPaint; import android.text.style.URLSpan; public class UrlSpanNoUnderline extends URLSpan { public UrlSpanNoUnderline(URLSpan src) { super(src.getURL()); } @Override public void updateDrawState(TextPaint ds) { super.updateDrawState(ds); ds.setUnderlineText(false); } } 
 public void setView() { TextView t=(TextView) findViewById(R.id.textView3); t.setText(Html.fromHtml("<a href=http://www.urdusms.net > UrduSMS ")); t.setTextColor(Color.BLACK); t.setGravity(Gravity.CENTER); t.setMovementMethod(LinkMovementMethod.getInstance()); Spannable s = (Spannable) t.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 URLSpanline_none(span.getURL()); s.setSpan(span, start, end, 0); } t.setText(s); } //inner class is private class URLSpanline_none extends URLSpan { public URLSpanline_none(String url) { super(url); } @Override public void updateDrawState(TextPaint ds) { super.updateDrawState(ds); ds.setUnderlineText(false); } } 

如果你只是想要的文字,不要打扰的URL链接

这将STRIP链接,但保持文本

 private Spannable stripLinks(String content) { Spannable s = new SpannableString(content); URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class); for (URLSpan span : spans) { s.removeSpan(span); } return s; } 

不需要额外的课程

 String content = "<a href='http://stackoverflow.com'>Stack Overflow</a> Rocks!"; textView.setText(stripLinks(content)); 

如果您使用Textview自动链接属性,并且想要删除下划线,则可以使用它:

首先,扩展UnderlineSpan并删除下划线:

 public class NoUnderlineSpan extends UnderlineSpan { @Override public void updateDrawState(TextPaint ds) { ds.setUnderlineText(false); } } 

其次,创buildNoUnderlineSpan的实例,从String文本创build一个Spannable,并将span设置为spannable:

 NoUnderlineSpan mNoUnderlineSpan = new NoUnderline(); if (yourTextView.getText() instanceof Spannable) { Spannable s = (Spannable) yourTextView.getText(); s.setSpan(mNoUnderlineSpan, 0, s.length(), Spanned.SPAN_MARK_MARK); } 

参考: http : //prog3.com/sbdm/blog/maosidiaoxian/article/details/39156563

android:autoLink下划线可以使用android:textAllCaps =“true”android:textIsSelectable =“false”

 <TextView android:autoLink="phone" android:text="070 300 1155" android:textAllCaps="true" android:textIsSelectable="false" /> 

这是我的方法

  public static void removeUnderlines(Spannable p_Text) { if (p_Text != null && p_Text.toString().length() > 0) { URLSpan[] spans = p_Text.getSpans(0, p_Text.length(), URLSpan.class); for (URLSpan span : spans) { int start = p_Text.getSpanStart(span); int end = p_Text.getSpanEnd(span); p_Text.removeSpan(span); span = new URLSpanNoUnderline(span.getURL()); p_Text.setSpan(span, start, end, 0); } } } 

像这样调用它

 AppController.removeUnderlines((Spannable) eventEmail.getText()); 

Appcontroller是我的应用程序类,我把这个方法,使我可以从任何地方访问它