android texttview超链接用内置webview打开
后端接口返回的a标签的数据,如:
"测试数据<a href='http://www.baidu.com'>测试测试</a> 测试测试<a href='http://www.baidu.com'>内容测试</a>"
需要显示,超链接用不同的颜色标注并且可以,用内置webview打开
代码实现如下,关键点:要移除自带的span,添加自己重写的,否则,默认会调用外部浏览器打开链接
public void setTextShow(final Context context, String text) { Spanned htmlString = Html.fromHtml(text); if (htmlString instanceof SpannableStringBuilder) { SpannableStringBuilder spannableStringBuilder = (SpannableStringBuilder) htmlString; Object[] objs = spannableStringBuilder.getSpans(0, spannableStringBuilder.length(), URLSpan.class); for (Object obj : objs) { int start = spannableStringBuilder.getSpanStart(obj); int end = spannableStringBuilder.getSpanEnd(obj); //先移除这个Span,再新添加一个自己实现的Span URLSpan span = (URLSpan) obj; final String url = span.getURL(); spannableStringBuilder.removeSpan(obj); spannableStringBuilder.setSpan(new ClickableSpan() { @Override public void onClick(View widget) { startactivity(); } @Override public void updateDrawState(TextPaint ds) { //设置颜色 super.updateDrawState(ds); ds.setColor(context.getResources().getColor(R.color._004EFF)); ds.setUnderlineText(false); } }, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); } contentBottomView.setText(spannableStringBuilder); } }