Android 的觸控處理裡,一共內建了三種「按壓語意」的偵測:
把這三個階段攤在時間軸上,會看到系統用兩個門檻把「按下不放」切成三段:t0 之前算 tap,t0 到 t1 之間算 press,超過 t1 就認定為長按。
t0 與 t1 都定義在 ViewConfiguration 中:
/** 等待使用者是否移動的時間,超過則視為 tap 或 scroll */ private static final int TAP_TIMEOUT = 115; // t0 /** press 轉為 long press 之前的等待時間 */ private static final int LONG_PRESS_TIMEOUT = 500; // t1
真正的偵測邏輯寫在 View 的 onTouchEvent 裡。收到 ACTION_DOWN 時,View 先掛上一個延遲 t0 的 CheckForTap,並把狀態標記為 PREPRESSED:
case MotionEvent.ACTION_DOWN:
if (mPendingCheckForTap == null) {
mPendingCheckForTap = new CheckForTap();
}
mPrivateFlags |= PREPRESSED;
mHasPerformedLongPress = false;
postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
break;
接下來完全是一場「誰先到」的競賽:
ACTION_UP 絕大部分程式碼在做狀態轉換與焦點處理,順便決定要不要觸發 performClick:
case MotionEvent.ACTION_UP:
boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
boolean focusTaken = false;
if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
focusTaken = requestFocus();
}
if (!mHasPerformedLongPress) {
removeLongPressCallback();
if (!focusTaken) {
if (mPerformClick == null) mPerformClick = new PerformClick();
if (!post(mPerformClick)) performClick();
}
}
if (mUnsetPressedState == null) mUnsetPressedState = new UnsetPressedState();
if (prepressed) {
mPrivateFlags |= PRESSED;
refreshDrawableState();
postDelayed(mUnsetPressedState,
ViewConfiguration.getPressedStateDuration());
} else if (!post(mUnsetPressedState)) {
mUnsetPressedState.run();
}
removeTapCallback();
}
break;
值得注意的一行是 if (!mHasPerformedLongPress):一旦長按已經觸發過,抬手就不會再補一次 click,這正是「長按不會誤觸點擊」的原因。
它取得 UI 執行緒的 Handler,再把 Runnable 排入訊息佇列;若 View 尚未 attach,就先丟進 ViewRoot 的 run queue 等日後補跑。
public boolean postDelayed(Runnable action, long delayMillis) {
Handler handler;
if (mAttachInfo != null) {
handler = mAttachInfo.mHandler;
} else {
ViewRoot.getRunQueue().postDelayed(action, delayMillis);
return true;
}
return handler.postDelayed(action, delayMillis);
}
private final class CheckForTap implements Runnable {
public void run() {
// 走進來代表 TAP_TIMEOUT 已過,pre-pressed 結束
mPrivateFlags &= ~PREPRESSED;
mPrivateFlags |= PRESSED;
refreshDrawableState();
if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
postCheckForLongClick(ViewConfiguration.getTapTimeout());
}
}
}
private void postCheckForLongClick(int delayOffset) {
mHasPerformedLongPress = false;
if (mPendingCheckForLongPress == null) {
mPendingCheckForLongPress = new CheckForLongPress();
}
mPendingCheckForLongPress.rememberWindowAttachCount();
postDelayed(mPendingCheckForLongPress,
ViewConfiguration.getLongPressTimeout() - delayOffset);
}
這裡的 - delayOffset 是個小細節:長按計時不是從 ACTION_DOWN 重新算,而是扣除已經等掉的 t0,讓 t1 依然是從按下瞬間起算的絕對時間。
class CheckForLongPress implements Runnable {
private int mOriginalWindowAttachCount;
public void run() {
if (isPressed() && (mParent != null)
&& mOriginalWindowAttachCount == mWindowAttachCount) {
if (performLongClick()) {
mHasPerformedLongPress = true;
}
}
}
public void rememberWindowAttachCount() {
mOriginalWindowAttachCount = mWindowAttachCount;
}
}
public boolean performLongClick() {
sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
boolean handled = false;
if (mOnLongClickListener != null) {
handled = mOnLongClickListener.onLongClick(View.this);
}
if (!handled) {
handled = showContextMenu(); // 預設行為:彈出情境選單
}
if (handled) {
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
}
return handled;
}
三個判斷條件各有含義:必須仍處於 pressed 狀態(手指沒抬起)、必須仍有 Parent、而且 window 的 attach count 沒變過(畫面沒被重建),避免在畫面切換後誤觸長按。
整套路的核心是「延遲訊息 + 狀態位」的組合:用 postDelayed 排定兩個時間點,用 mPrivateFlags 記錄目前階段,任何一方提早發生就取消另一方的 callbacks。理解這條鏈條之後,自訂 View 要做類似「點擊 / 長按 / 拖曳」的分流,也就有了可以照搬的範式。