美文网首页
Android 中关于 TextView中链接的拦截

Android 中关于 TextView中链接的拦截

作者: xiechengfa | 来源:发表于2018-05-11 13:54 被阅读69次

    private void setTextViewHTML(TextView text, String html) {
    CharSequence sequence = Html.fromHtml(html);
    SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
    URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class);
    for (URLSpan span : urls) {
    makeLinkClickable(strBuilder, span);
    }
    text.setText(strBuilder);
    text.setMovementMethod(LinkMovementMethod.getInstance());
    }

    private void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span) {
    int start = strBuilder.getSpanStart(span);
    int end = strBuilder.getSpanEnd(span);
    int flags = strBuilder.getSpanFlags(span);
    ClickableSpan clickable = new ClickableSpan() {
    public void onClick(View view) {
    BrowserAct.startAty((EMActivity) view.getContext(), span.getURL(), null);
    }
    };
    strBuilder.setSpan(clickable, start, end, flags);
    strBuilder.removeSpan(span);
    }

    相关文章

      网友评论

          本文标题:Android 中关于 TextView中链接的拦截

          本文链接:https://www.haomeiwen.com/subject/adefdftx.html