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

时间:2010-11-04 12:55:10

标签: android hyperlink textview underline

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

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

我可以从XML或代码那样做吗?

12 个答案:

答案 0 :(得分:203)

您可以通过使用未加下划线的版本查找和替换URLSpan实例来在代码中执行此操作。致电Linkify.addLinks()后,请在每个stripUnderlines()上拨打下面粘贴的功能TextView

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

答案 1 :(得分:26)

给出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);

这是基于robUx4建议的方法。

为了使链接可以点击,您还需要致电:

textView.setMovementMethod(LinkMovementMethod.getInstance());

答案 2 :(得分:10)

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

另一种解决方案是在每个现有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);
}

答案 3 :(得分:6)

这是 Kotlin 扩展功能:

def hash_is_md5(_hash) -> bool:
    search = re.compile(r"([a-fA-F\d]{32})").search
    return not bool(search(_hash) )

def hash_is_sha1(_hash) -> bool:
    if len(_hash) != 40:
        return False
    try:
        hash_int = int(_hash, 16)
    except ValueError:
        return False
    return True

def hash_is_sha256(_hash) -> bool:
    if (len(_hash.encode("utf-8")) / 8) == 32:
        return True
    else:
        return False

用法:

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
}

答案 4 :(得分:2)

每当尝试使用spannable 删除带下划线的时,我建议只使用以下内容:

1&GT; 创建自定义类:

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 - ; setSpan with spannable text:

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

此处, spannableText SpannableString的对象 ... !!!

答案 5 :(得分:2)

我已经实施了一个解决方案,在我看来,它更优雅。我已经制作了自定义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);
    }
}

答案 6 :(得分:1)

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

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

public class NoUnderlineSpan extends UnderlineSpan {

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

其次,创建NoUnderlineSpan的实例,从String文本创建一个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

答案 7 :(得分:0)

如果您只是想要文字而不必担心网址链接

这将 STRIP 链接,但 KEEP 文字

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

答案 8 :(得分:0)

这是我的方法

 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是我的应用程序类,我把这个方法放在这里,以便我可以从任何地方访问它

答案 9 :(得分:0)

对于Xamarin用户找到此帖子,这是我如何使其工作的方法:

  1. 创建一个自定义的span类,以处理下划线。
    class URLSpanNoUnderline : URLSpan {
            public URLSpanNoUnderline (string url) : base (url) {
            }

            public override void UpdateDrawState (TextPaint ds) {
                base.UpdateDrawState (ds);
                ds.UnderlineText = false;
            }
        }
  1. 创建一个扩展方法来查找所有网址范围并将其替换为我们的自定义范围。
public static void StripUnderlinesFromLinks (this TextView textView) {
                var spannable = new SpannableStringBuilder (textView.TextFormatted);
                var spans = spannable.GetSpans (0, spannable.Length (), Java.Lang.Class.FromType (typeof (URLSpan)));
                foreach (URLSpan span in spans) {
                    var start = spannable.GetSpanStart (span);
                    var end = spannable.GetSpanEnd (span);
                    spannable.RemoveSpan(span);
                    var newSpan = new URLSpanNoUnderline (span.URL);
                    spannable.SetSpan(newSpan, start, end, 0);
                }
                textView.TextFormatted = spannable;
            }

答案 10 :(得分: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(caslString, HtmlCompat.FROM_HTML_MODE_LEGACY) as Spannable

val sampleSpannableWithoutUnderlines = sampleSpannable.removeAllUrlSpanUnderline()

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

答案 11 :(得分:-1)

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