TextView AutoLink, ClikSpan 與長按事件衝突的解決
前言
首先,我們先來複習一下 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 成員變數中。
@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
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
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
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 的自動跳轉是在這裡處理的。
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 方法
@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 的相關邏輯之後,剩下的相關程式碼主要如下:
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) 方法。
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 方法
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 事件中呼叫的,跟我們的長按事件沒半毛錢關係。
重要的事情說三遍
ClickableSpan 的 onClick 方法是在 ACTION_UP 事件中呼叫的 ClickableSpan 的 onClick 方法是在 ACTION_UP 事件中呼叫的 ClickableSpan 的 onClick 方法是在 ACTION_UP 事件中呼叫的
知道了 ClickableSpan 的 onClick 方法是在 ACTION_UP 事件中呼叫的,下面讓我們一起來看一下怎樣解決 TextView 中 autolink 與 clickableSpan 與長按事件的衝突。
<span id="resolve">解決思路</span>
其實很簡單,既然,它是在 ACTION_UP 事件處理的,那麼我們只需要監聽到長按事件,並且當前 MotionEvent 是 ACTION_UP 的時候,我們直接返回 true,不讓他繼續往下處理就 ok 了。
由於時間關係,沒有詳細去了解 View 的長按事件的促發事件,這裡我們已按下的事件超過 500 s,即使別為長按事件。
這裡,我們定義一個 ControlClickSpanTextView,繼承 AppCompatTextView,程式碼如下。
- 在 ACTION_DOWN 的時候記錄下事件
- ACTION_UP 的時候,判斷事件是否超過 500 毫秒,超過 500 毫秒,不再處理事件,直接返回 true
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); } }
總結
寫程式碼其實跟我們生活一樣,遇到困難的時候,不要慌張,先靜下心來,分析這件事情的本質,這些事情背後的原因是什麼,有哪些解決方案,哪些是最優的。多記錄,多總結,有時候,你也會發現,在寫程式碼 “枯燥” 的過程中,也許多了一點“樂趣"。
明天就聖誕節了,大家有什麼活動,有沒有自己心儀的女神,趕緊行動。不要給小編我撒狗糧就好。

image
最後的最後,請容我賣一下廣告,歡迎大家關注我的微信公眾號,掃一掃下方二維碼或搜尋微訊號 Android 技術人。 目前專注於 Android 開發,主要分享 Android開發相關知識和一些相關的優秀文章,包括個人總結,職場經驗等。

image