解决Android TextView富文本拦截点击事件
2017-04-25 本文已影响1453人
王晨彦
前言
项目中有这样一个场景,一个消息流,消息中可能存在超链接,点击跳转到相应页面,消息item长按可以复制。
常规的解决思路:
- 使用SpannableString和ClickableSpan替换文本中的A标签
- TextView设置setMovementMethod(LinkMovementMethod.getInstance())使其支持超链接点击
踩坑
当我按照上述思路实现后,发现超链接确实可以点击跳转了,但是却遇到了另一个问题,消息item无法获得长按事件了。
猜测是TextView拦截了触摸事件,可是没有超链接的消息的触摸事件也被拦截了,这尼玛就太不科学了。经查发现是Android的一个bug,LinkMovementMethod中的onTouchEvent永远都是返回true。
填坑
在StackOverFlow上发现了解决方法,就是放弃setMovementMethod,改用setOnTouchListener,重写View.OnTouchListener,代码如下
public class ClickMovementMethod implements View.OnTouchListener {
private static ClickMovementMethod sInstance;
public static ClickMovementMethod getInstance() {
if (sInstance == null) {
sInstance = new ClickMovementMethod();
}
return sInstance;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
boolean ret = false;
TextView widget = (TextView) v;
CharSequence text = widget.getText();
Spannable spannable = Spannable.Factory.getInstance().newSpannable(text);
int action = event.getAction();
if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
int x = (int) event.getX();
int y = (int) event.getY();
x -= widget.getTotalPaddingLeft();
y -= widget.getTotalPaddingTop();
x += widget.getScrollX();
y += widget.getScrollY();
Layout layout = widget.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
ClickableSpan[] link = spannable.getSpans(off, off, ClickableSpan.class);
if (link.length != 0) {
if (action == MotionEvent.ACTION_UP) {
link[0].onClick(widget);
}
ret = true;
}
}
return ret;
}
}
然后在textView 上调用
textView.setOnTouchListener(ClickMovementMethod.getInstance());
这样做有一个小问题,就是超链接按下去没有高亮效果了,不过基本不影响用户体验。
20170918更新 添加超链接长按事件处理
这段代码用了半年没有问题,但是今天有用户反馈,无法复制电话号码
原因是因为 ClickMovementMethod 中并没有处理长按事件,因此我们需要添加长按事件。
以下代码是添加了长按事件的完整代码
public class ClickMovementMethod implements View.OnTouchListener {
private LongClickCallback longClickCallback;
public static ClickMovementMethod newInstance() {
return new ClickMovementMethod();
}
@Override
public boolean onTouch(final View v, MotionEvent event) {
if (longClickCallback == null) {
longClickCallback = new LongClickCallback(v);
}
TextView widget = (TextView) v;
// MovementMethod设为空,防止消费长按事件
widget.setMovementMethod(null);
CharSequence text = widget.getText();
Spannable spannable = Spannable.Factory.getInstance().newSpannable(text);
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_UP) {
int x = (int) event.getX();
int y = (int) event.getY();
x -= widget.getTotalPaddingLeft();
y -= widget.getTotalPaddingTop();
x += widget.getScrollX();
y += widget.getScrollY();
Layout layout = widget.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
ClickableSpan[] link = spannable.getSpans(off, off, ClickableSpan.class);
if (link.length != 0) {
if (action == MotionEvent.ACTION_DOWN) {
v.postDelayed(longClickCallback, ViewConfiguration.getLongPressTimeout());
} else {
v.removeCallbacks(longClickCallback);
link[0].onClick(widget);
}
return true;
}
} else if (action == MotionEvent.ACTION_CANCEL) {
v.removeCallbacks(longClickCallback);
}
return false;
}
private static class LongClickCallback implements Runnable {
private View view;
LongClickCallback(View view) {
this.view = view;
}
@Override
public void run() {
// 找到能够消费长按事件的View
View v = view;
boolean consumed = v.performLongClick();
while (!consumed) {
v = (View) v.getParent();
if (v == null) {
break;
}
consumed = v.performLongClick();
}
}
}
}
textView.setOnTouchListener(ClickMovementMethod.newInstance());