Remove underline from links in TextView - Android

2019-01-02 23:00发布

I am using two textview to display links from database, I managed to change link colors but I want to remove the underline

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

Can I do that from XML or Code ?

10条回答
再贱就再见
2楼-- · 2019-01-02 23:20

Underline in android:autoLink can be removed using android:textAllCaps="true" and android:textIsSelectable="false"

<TextView
    android:autoLink="phone"
    android:text="070 300 1155"
    android:textAllCaps="true"
    android:textIsSelectable="false"
     />
查看更多
时光不老,我们不散
3楼-- · 2019-01-02 23:21
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);
            }
        }
查看更多
你好瞎i
4楼-- · 2019-01-02 23:24

You can do it in code by finding and replacing the URLSpan instances with versions that don't underline. After you call Linkify.addLinks(), call the function stripUnderlines() pasted below on each of your TextViews:

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

This requires a customized version of URLSpan which doesn't enable the TextPaint's "underline" property:

    private class URLSpanNoUnderline extends URLSpan {
        public URLSpanNoUnderline(String url) {
            super(url);
        }
        @Override public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setUnderlineText(false);
        }
    }
查看更多
啃猪蹄的小仙女
5楼-- · 2019-01-02 23:28

Here is My method

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

Call It like this

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

Appcontroller is my application class where i put this method so that i can access it from anywhere

查看更多
爷的心禁止访问
6楼-- · 2019-01-02 23:32

If you are using Textview autolink property and you want to remove underlines you can use it:

First, extend UnderlineSpan and remove underline:

public class NoUnderlineSpan extends UnderlineSpan {

    @Override
    public void updateDrawState(TextPaint ds) {
        ds.setUnderlineText(false);
    }
}

Second, create and instance of NoUnderlineSpan, create a Spannable from the String text and set the span to the 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);
}

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

查看更多
我想做一个坏孩纸
7楼-- · 2019-01-02 23:33

UnderlineSpan already exists, but can only set the underline.

Another solution is to add a no underline span on each existing URLSpan. Thus the underline state is disabled just before painting. This way you keep your URLSpan (possibly custom) classes and all other styles set elsewhere.

public class NoUnderlineSpan extends UnderlineSpan {
    public NoUnderlineSpan() {}

    public NoUnderlineSpan(Parcel src) {}

    @Override
    public void updateDrawState(TextPaint ds) {
        ds.setUnderlineText(false);
    }
}

Here is how you set it without removing the existing URLSpan object:

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);
}
查看更多
登录 后发表回答