Android|Android 拦截TextView中超链接点击事件
Android 拦截TextView中超链接的点击事件 【Android|Android 拦截TextView中超链接点击事件】1.在xml中为TextView添加autoLink属性
2.
private void setLinkClickable(final SpannableStringBuilder clickableHtmlBuilder, final URLSpan urlSpan) {
int start = clickableHtmlBuilder.getSpanStart(urlSpan);
int end = clickableHtmlBuilder.getSpanEnd(urlSpan);
int flags = clickableHtmlBuilder.getSpanFlags(urlSpan);
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
String originUrl = urlSpan.getURL();
//获取url地址
//do something
...
}
};
clickableHtmlBuilder.setSpan(clickableSpan, start, end, flags);
}private CharSequence getClickableHtml(Spanned spannedHtml) {
SpannableStringBuilder clickableHtmlBuilder = new SpannableStringBuilder(spannedHtml);
URLSpan[] urls = clickableHtmlBuilder.getSpans(0, spannedHtml.length(), URLSpan.class);
for (final URLSpan span : urls){
setLinkClickable(clickableHtmlBuilder, span);
}
return clickableHtmlBuilder;
}
3.
String urlText;
Spanned spanned = Html.fromHtml(urlText);
textView.setText(getClickableHtml(spanned));
textView.setMovementMethod(LinkMovementMethod.getInstance());
转载于:https://www.cnblogs.com/Ricardoldc/p/11233899.html
推荐阅读
- android第三方框架(五)ButterKnife
- Android中的AES加密-下
- 带有Hilt的Android上的依赖注入
- android|android studio中ndk的使用
- Android事件传递源码分析
- RxJava|RxJava 在Android项目中的使用(一)
- Android7.0|Android7.0 第三方应用无法访问私有库
- 深入理解|深入理解 Android 9.0 Crash 机制(二)
- android防止连续点击的简单实现(kotlin)
- Android|Android install 多个设备时指定设备