我有一个TextView(在我的recyclerview上)正在呈现基本的HTML,其中包含1个链接。我需要捕获链接上的点击,然后从该链接中开始新的活动。我怎样才能做到这一点?我已经尝试过此解决方案,但无法正常工作。
protected void makeLinkClickable(Spannable 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) {
// Do something with span.getURL() to handle the link click...
Toast.makeText(mContext, "click", Toast.LENGTH_SHORT).show();
}
};
strBuilder.setSpan(clickable, start, end, flags);
strBuilder.removeSpan(span);
}
protected void setTextViewHTML(TextView text, String html) {
CharSequence sequence = Html.fromHtml(html);
Spannable strBuilder = (Spannable) (sequence);
URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class);
for (URLSpan span : urls) {
makeLinkClickable(strBuilder, span);
}
text.setText(strBuilder);
text.setMovementMethod(LinkMovementMethod.getInstance());
text.setClickable(true);
text.setFocusable(false);
}