需求
项目中常有需求:要求长段文本中实现点击局部文本并执行自定义事件。本文针对类似需求进行简易的分析并编写相关工具类。
分析
首先多TextView实现肯定是不行的:一是代码冗余;二是若可点击文本换行时不好处理。
接着看其效果类似超链接,自然想到使用Html.fromHtml,将可点击区域加上a标签。可是点击a标签默认会调用外部浏览器打开指定链接。跟进Html.fromHtml方法可知,其返回的是一个Spanned
public static Spanned fromHtml(String source)
既然是Spanned,那自然是可以自定义的。
思路:给可点击文本加上a标签,获取Html.fromHtml返回的Spanned中的UrlSpan部分,在相同位置添加自定义ClickSpan实现自定义事件,代码如下:
Spanned html = Html.fromHtml(text);
URLSpan[] spans = html.getSpans(0, text.length(), URLSpan.class);
SpannableStringBuilder builder = new SpannableStringBuilder(html);
builder.clearSpans();
for (URLSpan span : spans) {
String url = span.getURL();//获取a标签中的url
//添加点击事件
}
同时创建一个map,key可以为url,value为相应的点击事件。添加ClickSpan同时可以添加其他样式Span。
完整代码:
public class ClickSpanUtils {
public static class ClickOption {
public Runnable runnable;
public int color;
public boolean underline;
public ClickOption(Runnable runnable, int color, boolean underline) {
this.runnable = runnable;
this.color = color;
this.underline = underline;
}
public ClickOption(Runnable runnable, int color) {
this(runnable, color, true);
}
public ClickOption(Runnable runnable) {
this(runnable, 0);
}
}
public static Spanned parse(String text, Map<String, ClickOption> eventMap) {
Spanned html = Html.fromHtml(text);
URLSpan[] spans = html.getSpans(0, text.length(), URLSpan.class);
S