android奇葩bug

anroid textview自定义clickspan替换Url

2018-12-06  本文已影响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());

}

}

上一篇下一篇

猜你喜欢

热点阅读