首先将要显示的字符串定义成 str=“字符串” +“<a href=\"网址\">网址</a>“ +”字符串“
textview.setText(Html.fromHtml(str));
textview.setMovementMethod(LinkMovementMethod.getInstance());
CharSequence text = textview_content.getText();
if (text instanceof Spannable) {
int end = text.length();
Spannable sp = (Spannable) textview_content.getText();
URLSpan[] urls = sp.getSpans(0, end, URLSpan.class);//扫描出所有的可点击的url
SpannableStringBuilder style = new SpannableStringBuilder(text);
style.clearSpans();
for (URLSpan url : urls) {
URLClick urlClick = new URLClick(url.getURL(),this,dialog);
style.setSpan(urlClick, sp.getSpanStart(url), sp .getSpanEnd(url), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
}
textview_content.setText(style);
}
已经给textview添加了监听,下面编写监听类
public class URLClick extends ClickableSpan {
String url = null;
public URLClick(String url){
this.url = url;
}
@Override
public void onClick(View widget) {
//这里添加点击操作
}
}