前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >TextView AutoLink, ClikSpan 与长按事件冲突的解决

TextView AutoLink, ClikSpan 与长按事件冲突的解决

作者头像
程序员徐公
发布2018-12-28 12:09:28
1.9K0
发布2018-12-28 12:09:28
举报

前言

首先,我们先来复习一下 autoLink 和 ClickableSpan 是干什么用的。

autoLink 当中有五个属性值:分别是 phone、email、map、web、all 和 none,前四个分别是自动识别电话号码、邮箱、地址和网址,而第五个是识别前四个的全部,none 是不识别;

在不设置 none 而设置其他值的情况下,当你的 TextView 当中有 phone/map/web/email 的值的时候,并且linksClickable=“true” 的时候,点击 TextView 控件会自动打开,有的机型是先会提示;例如设置 autoLink的值为 phone ,那么当 TextView 中出现连续的数字或者号码的时候,点击 TextView 会拨打该连续数字的号码或电话号码。

而 ClickableSpan 是用来设置部分文字的点击事件的。

当我们设置 TextView 的长按事件并且同时设置 autoLink 或者 ClickableSpan 的时候,你会发现,当我们长按 TextView 的时候,长按事件会响应,同时 autoLink 或者 ClickableSpan 也会响应,不管我们在 onLongClick 返回 true 还是 false。

为什么会这样呢,且听下文分析。(不想看源码分析的也可以直接跳过该部分,直接看 解决思路 , 不过建议还是看一下源码分析过程,以后遇到类似的问题,我们能够举一反三。

从源码的角度分析 autoLink

想一下,如果是你分析,你会从那些入口开始分析,这个很重要,找对正确的入口,往往能事半功倍。

这里说一下我的思维,大概分为以下三步:

  • TextView 是如何解析 autolink 的
  • autolink 的 onclick 事件是在哪里响应的
  • autolink 的 onclick 事件是在哪里被调用的

TextView 是如何解析 autolink 的

这个问题比较简单,写过自定义控件的人都知道,一般是从 xml 解析的,这里也不例外。

下面,我们一起来看一下 TextView 是如何解析 autoLink 的值的。 从代码中可以看出,在构造方法中,获取 autoLink 属性在 xml 中定义的值,储存在 mAutoLinkMask 成员变量中。

代码语言:javascript
复制
 @SuppressWarnings("deprecation")
public TextView(
        Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);

 -------  // 跳过一大堆代码

            case com.android.internal.R.styleable.TextView_autoLink:
                mAutoLinkMask = a.getInt(attr, 0);

autolink 的 onclick 事件是在哪里响应的

首先我们需要查找 mAutoLinkMask 在 TextView 哪些地方被调用,很快,我们发现在 setText 里面使用了 mAutoLinkMask

代码语言:javascript
复制
private void setText(CharSequence text, BufferType type,
                     boolean notifyBefore, int oldlen) {
    
	-----

    if (mAutoLinkMask != 0) {
        Spannable s2;

        if (type == BufferType.EDITABLE || text instanceof Spannable) {
            s2 = (Spannable) text;
        } else {
            s2 = mSpannableFactory.newSpannable(text);
        }

        if (Linkify.addLinks(s2, mAutoLinkMask)) {
            text = s2;
            type = (type == BufferType.EDITABLE) ? BufferType.EDITABLE : BufferType.SPANNABLE;

            /*
             * We must go ahead and set the text before changing the
             * movement method, because setMovementMethod() may call
             * setText() again to try to upgrade the buffer type.
             */
            setTextInternal(text);

            // Do not change the movement method for text that support text selection as it
            // would prevent an arbitrary cursor displacement.
            if (mLinksClickable && !textCanBeSelected()) {
                setMovementMethod(LinkMovementMethod.getInstance());
            }
        }
    }
  • 首先调用 Linkify.addLinks 方法解析 autolink 的相关属性
  • 判断是否 mLinksClickable mLinksClickable && !textCanBeSelected() ,若返回 true, 设置 setMovementMethod

我们先来看一下 Linkify 类, 里面定义了几个常量, 分别对应 web , email ,phone ,map,他们的值是位上错开的,这样定义的好处是

  • 方便组合多种值
  • 组合值之后不会丢失状态,即可以获取是否含有某种状态, web, email, phone , map
代码语言:javascript
复制
public class Linkify {
 
    public static final int WEB_URLS = 0x01;

    public static final int EMAIL_ADDRESSES = 0x02;

    public static final int PHONE_NUMBERS = 0x04;

    public static final int MAP_ADDRESSES = 0x08;
	
}

看一下 linkify 的 addLinks 方法

  • 根据 mask 的标志位,进行相应的正则表达式进行匹配,找到 text 里面的相应的 WEB_URLS, EMAIL_ADDRESSES, PHONE_NUMBERS, MAP_ADDRESSES. 并将相应的文本从 text 里面移除,封装成 LinkSpec,并添加到 links 里面
  • 遍历 links,设置相应的 URLSpan
代码语言:javascript
复制
private static boolean addLinks(@NonNull Spannable text, @LinkifyMask int mask,
        @Nullable Context context) {
    if (mask == 0) {
        return false;
    }

    URLSpan[] old = text.getSpans(0, text.length(), URLSpan.class);

    for (int i = old.length - 1; i >= 0; i--) {
        text.removeSpan(old[i]);
    }

    ArrayList<LinkSpec> links = new ArrayList<LinkSpec>();
/ /  根据正则表达式提取 text 里面相应的 WEB_URLS,并且从 text 移除
    if ((mask & WEB_URLS) != 0) {
	  
        gatherLinks(links, text, Patterns.AUTOLINK_WEB_URL,
            new String[] { "http://", "https://", "rtsp://" },
            sUrlMatchFilter, null);
    }

    if ((mask & EMAIL_ADDRESSES) != 0) {
        gatherLinks(links, text, Patterns.AUTOLINK_EMAIL_ADDRESS,
            new String[] { "mailto:" },
            null, null);
    }

    if ((mask & PHONE_NUMBERS) != 0) {
        gatherTelLinks(links, text, context);
    }

    if ((mask & MAP_ADDRESSES) != 0) {
        gatherMapLinks(links, text);
    }

    pruneOverlaps(links);

    if (links.size() == 0) {
        return false;
    }

// 遍历 links,设置相应的 URLSpan
    for (LinkSpec link: links) {
        applyLink(link.url, link.start, link.end, text);
    }

    return true;
}

private static final void applyLink(String url, int start, int end, Spannable text) {
    URLSpan span = new URLSpan(url);

    text.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}

接下来我们一起来看一下这个 URLSpan 是何方神圣,它继承了 ClickableSpan(注意下文会用到它),并且重写了 onClick 方法,我们可以看到在 onClick 方法里面,他通过相应的 intent 取启动相应的 activity。因此,我们可以断定 autolink 的自动跳转是在这里处理的。

代码语言:javascript
复制
public class URLSpan extends ClickableSpan implements ParcelableSpan {

    private final String mURL;

    /**
     * Constructs a {@link URLSpan} from a url string.
     *
     * @param url the url string
     */
    public URLSpan(String url) {
        mURL = url;
    }

    /**
     * Constructs a {@link URLSpan} from a parcel.
     */
    public URLSpan(@NonNull Parcel src) {
        mURL = src.readString();
    }

    @Override
    public int getSpanTypeId() {
        return getSpanTypeIdInternal();
    }

     -----

    @Override
    public void onClick(View widget) {
        Uri uri = Uri.parse(getURL());
        Context context = widget.getContext();
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName());
        try {
            context.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Log.w("URLSpan", "Actvity was not found for intent, " + intent.toString());
        }
    }
}

解决了 autolink 属性点击事件在哪里响应了,接下来我们一起看一下 URLSpan 的 onClick 方法是在哪里调用的。

autolink 的 onclick 事件是在哪里被调用的

我们先来复习一下 View 的事件分发机制:

  • dispatchTouchEvent ,这个方法主要是用来分发事件的
  • onInterceptTouchEvent,这个方法主要是用来拦截事件的(需要注意的是ViewGroup才有这个方法,- View没有onInterceptTouchEvent这个方法
  • onTouchEvent 这个方法主要是用来处理事件的 requestDisallowInterceptTouchEvent(true),这个方法能够影响父View是否拦截事件,true 表示父 View 不拦截事件,false 表示父 View 拦截事件

因此我们猜测 URLSpan 的 onClick 事件是在 TextView 的 onTouchEvent 事件里面调用的。下面让我们一起来看一下 TextView 的 onTouchEvent 方法

代码语言:javascript
复制
@Override
public boolean onTouchEvent(MotionEvent event) {
    final int action = event.getActionMasked();
    if (mEditor != null) {
        mEditor.onTouchEvent(event);

        if (mEditor.mSelectionModifierCursorController != null
                && mEditor.mSelectionModifierCursorController.isDragAcceleratorActive()) {
            return true;
        }
    }

    final boolean superResult = super.onTouchEvent(event);

    /*
     * Don't handle the release after a long press, because it will move the selection away from
     * whatever the menu action was trying to affect. If the long press should have triggered an
     * insertion action mode, we can now actually show it.
     */
    if (mEditor != null && mEditor.mDiscardNextActionUp && action == MotionEvent.ACTION_UP) {
        mEditor.mDiscardNextActionUp = false;

        if (mEditor.mIsInsertionActionModeStartPending) {
            mEditor.startInsertionActionMode();
            mEditor.mIsInsertionActionModeStartPending = false;
        }
        return superResult;
    }

    final boolean touchIsFinished = (action == MotionEvent.ACTION_UP)
            && (mEditor == null || !mEditor.mIgnoreActionUpEvent) && isFocused();

    if ((mMovement != null || onCheckIsTextEditor()) && isEnabled()
            && mText instanceof Spannable && mLayout != null) {
        boolean handled = false;

        if (mMovement != null) {
            handled |= mMovement.onTouchEvent(this, mSpannable, event);
        }

        final boolean textIsSelectable = isTextSelectable();
        if (touchIsFinished && mLinksClickable && mAutoLinkMask != 0 && textIsSelectable) {
            // The LinkMovementMethod which should handle taps on links has not been installed
            // on non editable text that support text selection.
            // We reproduce its behavior here to open links for these.
            ClickableSpan[] links = mSpannable.getSpans(getSelectionStart(),
                getSelectionEnd(), ClickableSpan.class);

            if (links.length > 0) {
                links[0].onClick(this);
                handled = true;
            }
        }

        if (touchIsFinished && (isTextEditable() || textIsSelectable)) {
            // Show the IME, except when selecting in read-only text.
            final InputMethodManager imm = InputMethodManager.peekInstance();
            viewClicked(imm);
            if (isTextEditable() && mEditor.mShowSoftInputOnFocus && imm != null) {
                imm.showSoftInput(this, 0);
            }

            // The above condition ensures that the mEditor is not null
            mEditor.onTouchUpEvent(event);

            handled = true;
        }

        if (handled) {
            return true;
        }
    }

    return superResult;
}

首先如果 mEditor != null 会将touch事件交给mEditor处理,这个 mEditor 其实是和 EditText 有关系的,没有使用 EditText 这里应该是不会被创建的。

去除 mEditor != null 的相关逻辑之后,剩下的相关代码主要如下:

代码语言:javascript
复制
  final boolean touchIsFinished = (action == MotionEvent.ACTION_UP)
            && (mEditor == null || !mEditor.mIgnoreActionUpEvent) && isFocused();

    if ((mMovement != null || onCheckIsTextEditor()) && isEnabled()
            && mText instanceof Spannable && mLayout != null) {
        boolean handled = false;

        if (mMovement != null) {
            handled |= mMovement.onTouchEvent(this, mSpannable, event);
        }

        final boolean textIsSelectable = isTextSelectable();
        if (touchIsFinished && mLinksClickable && mAutoLinkMask != 0 && textIsSelectable) {
            // The LinkMovementMethod which should handle taps on links has not been installed
            // on non editable text that support text selection.
            // We reproduce its behavior here to open links for these.
            ClickableSpan[] links = mSpannable.getSpans(getSelectionStart(),
                getSelectionEnd(), ClickableSpan.class);

            if (links.length > 0) {
                links[0].onClick(this);
                handled = true;
            }
        }

        if (touchIsFinished && (isTextEditable() || textIsSelectable)) {
            // Show the IME, except when selecting in read-only text.
            final InputMethodManager imm = InputMethodManager.peekInstance();
            viewClicked(imm);
            if (isTextEditable() && mEditor.mShowSoftInputOnFocus && imm != null) {
                imm.showSoftInput(this, 0);
            }

            // The above condition ensures that the mEditor is not null
            mEditor.onTouchUpEvent(event);

            handled = true;
        }

        if (handled) {
            return true;
        }
    }

首先我们先来看一下, mMovement 是否可能为 null,若不为 null,则会调用 handled |= mMovement.onTouchEvent(this, mSpannable, event) 方法。

找啊找,发现在 setText 里面有调用这一段代码,setMovementMethod(LinkMovementMethod.getInstance()); 即 mLinksClickable && !textCanBeSelected() 为 true 的时候给 TextView 设置 MovementMethod。

查看 TextView 的源码我们容易得知 mLinksClickable 的值默认为 true, 而 textCanBeSelected 方法会返回 false,即 mLinksClickable && !textCanBeSelected() 为 true,这个时候会给 TextView 设置 setMovementMethod。 因此在 TextView 的 onTouchEvent 方法中,若 autoLink 等于 true,并且 text 含有 email,phone, webAddress 等的时候,会调用 mMovement.onTouchEvent(this, mSpannable, event) 方法。

代码语言:javascript
复制
if (Linkify.addLinks(s2, mAutoLinkMask)) {
    text = s2;
    type = (type == BufferType.EDITABLE) ? BufferType.EDITABLE : BufferType.SPANNABLE;

    /*
     * We must go ahead and set the text before changing the
     * movement method, because setMovementMethod() may call
     * setText() again to try to upgrade the buffer type.
     */
    setTextInternal(text);

    // Do not change the movement method for text that support text selection as it
    // would prevent an arbitrary cursor displacement.
    if (mLinksClickable && !textCanBeSelected()) {
        setMovementMethod(LinkMovementMethod.getInstance());
    }
}

boolean textCanBeSelected() {
    // prepareCursorController() relies on this method.
    // If you change this condition, make sure prepareCursorController is called anywhere
    // the value of this condition might be changed.
	// 默认 mMovement 为 null
    if (mMovement == null || !mMovement.canSelectArbitrarily()) return false;
    return isTextEditable()
            || (isTextSelectable() && mText instanceof Spannable && isEnabled());
}

ok ,我们一起在来看一下 mMovement 的 onTouchEvent 方法

MovementMethod 是一个借口,实现子类有 ArrowKeyMovementMethod, LinkMovementMethod, ScrollingMovementMethod 。

这里我们先来看一下 LinkMovementMethod 的 onTouchEvent 方法

代码语言:javascript
复制
public boolean onTouchEvent(TextView widget, Spannable buffer,
                            MotionEvent event) {
    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[] links = buffer.getSpans(off, off, ClickableSpan.class);

        if (links.length != 0) {
            ClickableSpan link = links[0];
            if (action == MotionEvent.ACTION_UP) {
                if (link instanceof TextLinkSpan) {
                    ((TextLinkSpan) link).onClick(
                            widget, TextLinkSpan.INVOCATION_METHOD_TOUCH);
                } else {
                    link.onClick(widget);
                }
				
				
				----
				
}

这里我们重点关注代码 20 - 31 行,可以看到,他会先取出所有的 ClickableSpan,而我们的 URLSpan 正是 ClickableSpan 的子类,接着判断是否是 ACTION_UP 事件,然后调用 onClick 事件。因此,ClickableSpan 的 onClick 方法是在 ACTION_UP 事件中调用的,跟我们的长按事件没半毛钱关系。

重要的事情说三遍

代码语言:javascript
复制
ClickableSpan 的 onClick 方法是在 ACTION_UP 事件中调用的
ClickableSpan 的 onClick 方法是在 ACTION_UP 事件中调用的
ClickableSpan 的 onClick 方法是在 ACTION_UP 事件中调用的

知道了 ClickableSpan 的 onClick 方法是在 ACTION_UP 事件中调用的,下面让我们一起来看一下怎样解决 TextView 中 autolink 与 clickableSpan 与长按事件的冲突。


解决思路

其实很简单,既然,它是在 ACTION_UP 事件处理的,那么我们只需要监听到长按事件,并且当前 MotionEvent 是 ACTION_UP 的时候,我们直接返回 true,不让他继续往下处理就 ok 了。

由于时间关系,没有详细去了解 View 的长按事件的促发事件,这里我们已按下的事件超过 500 s,即使别为长按事件。

这里,我们定义一个 ControlClickSpanTextView,继承 AppCompatTextView,代码如下。

  • 在 ACTION_DOWN 的时候记录下事件
  • ACTION_UP 的时候,判断事件是否超过 500 毫秒,超过 500 毫秒,不再处理事件,直接返回 true
代码语言:javascript
复制
public class ControlClickSpanTextView extends AppCompatTextView {

    private static final String TAG = "AutoLinkTextView";

    private long mTime;
    private boolean mLinkIsResponseLongClick = false;

    public ControlClickSpanTextView(Context context) {
        super(context);
    }

    public ControlClickSpanTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ControlClickSpanTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public boolean isLinkIsResponseLongClick() {
        return mLinkIsResponseLongClick;
    }

    public void setLinkIsResponseLongClick(boolean linkIsResponseLongClick) {
        this.mLinkIsResponseLongClick = linkIsResponseLongClick;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        CharSequence text = getText();

        if (text == null) {
            return super.onTouchEvent(event);
        }

        if (!mLinkIsResponseLongClick && text instanceof Spannable) {
            int end = text.length();
            Spannable spannable = (Spannable) text;
            ClickableSpan[] clickableSpans = spannable.getSpans(0, end, ClickableSpan.class);

            if (clickableSpans == null || clickableSpans.length == 0) {
                return super.onTouchEvent(event);
            }

            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                mTime = System.currentTimeMillis();
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                if (System.currentTimeMillis() - mTime > 500) {
                    return true;
                }
            }
        }

        return super.onTouchEvent(event);
    }
}

总结

写代码其实跟我们生活一样,遇到困难的时候,不要慌张,先静下心来,分析这件事情的本质,这些事情背后的原因是什么,有哪些解决方案,哪些是最优的。多记录,多总结,有时候,你也会发现,在写代码 “枯燥” 的过程中,也许多了一点“乐趣"。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年12月24日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 从源码的角度分析 autoLink
    • TextView 是如何解析 autolink 的
      • autolink 的 onclick 事件是在哪里响应的
        • autolink 的 onclick 事件是在哪里被调用的
        • 解决思路
        • 总结
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档