鍍金池/ 問答/Android  網(wǎng)絡(luò)安全/ android7.0 TextView去除超鏈接下劃線就這么難嗎?

android7.0 TextView去除超鏈接下劃線就這么難嗎?

只想實(shí)現(xiàn)像微信那樣識別url,去除下劃線,網(wǎng)上滿篇的都是setUnderlineText(false),其實(shí)毫無作用,以下是網(wǎng)上普遍的解決辦法,但是下劃線依然沒有去掉,請問各位有可行解決辦法嗎

已經(jīng)解決了,添加一句就好了

@Override
    public void initComponents() {
        TextView textView = ((TextView) findViewById(R.id.summary));
        textView.setText(R.string.about_text);
        stripUnderlines(textView);
    }

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

    private void stripUnderlines(TextView textView) {
        if(null!=textView&&textView.getText() instanceof Spannable){
            Spannable s = (Spannable)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);
            }
            
            setAutoLinkMask(0);//已經(jīng)解決,添加這一句就好了
            textView.setText(s);
        }
    }
回答
編輯回答
有你在

setAutoLinkMask(0);//已經(jīng)解決,添加這一句就好了

2017年10月3日 22:51