美文网首页android奇葩bug
anroid textview自定义clickspan替换Url

anroid textview自定义clickspan替换Url

作者: 取悦自己的心心 | 来源:发表于2018-12-06 21:08 被阅读0次

    /**

    * 拦截超链接

    * @param tv

    */

    private void interceptHyperLink(TextView tv,String content) {

    CharSequence text = Html.fromHtml(content);

    if (textinstanceof Spannable) {

    int end = text.length();

    Spannable spannable = (Spannable) text;

    URLSpan[] urlSpans = spannable.getSpans(0, end, URLSpan.class);

    SpannableStringBuilder spannableStringBuilder =new SpannableStringBuilder(text);

    // 循环遍历并拦截 所有http://开头的链接

            for (URLSpan uri : urlSpans) {

    String url = uri.getURL();

    if (url.indexOf("http://") ==0 || url.indexOf("https://")==0) {

    CustomUrlSpan customUrlSpan =new CustomUrlSpan(getContext(), url,mIFragment);

    spannableStringBuilder.setSpan(customUrlSpan, spannableStringBuilder.getSpanStart(uri),

    spannableStringBuilder.getSpanEnd(uri), Spannable.SPAN_INCLUSIVE_INCLUSIVE);

    spannableStringBuilder.removeSpan(uri);

    }

    }

    tv.setText(spannableStringBuilder);

    tv.setMovementMethod(LinkMovementMethod.getInstance());

    }

    }

    相关文章

      网友评论

        本文标题:anroid textview自定义clickspan替换Url

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