按下菜单按钮时,如何创建单击事件和双击事件?

我希望能够在按下菜单按钮时检测单击或双击。 如果检测到单击,则会发生偶数,如果检测到双击,则会发生不同的事件。 这是我尝试过的(使用toast代替事件):

private static final long DOUBLE_PRESS_INTERVAL = 250; // in millis private long lastPressTime; @Override public boolean onPrepareOptionsMenu(Menu menu) { // Get current time in nano seconds. long pressTime = System.currentTimeMillis(); // If double click... if (pressTime - lastPressTime <= DOUBLE_PRESS_INTERVAL) { Toast.makeText(getApplicationContext(), "Double Click Event", Toast.LENGTH_SHORT).show(); return true; } // If not double click.... Toast.makeText(getApplicationContext(), "Single Click Event", Toast.LENGTH_SHORT).show(); // record the last time the menu button was pressed. lastPressTime = pressTime; return true; } 

问题是每次双击事件之前都会检测到单击事件。

简单的逻辑错误。 您在录制新的lastPressTime之前返回。 如果他们都返回相同的东西,你应该只有一个回复:

 boolean mHasDoubleClicked = false; @Override public boolean onPrepareOptionsMenu(Menu menu) { // Get current time in nano seconds. long pressTime = System.currentTimeMillis(); // If double click... if (pressTime - lastPressTime <= DOUBLE_PRESS_INTERVAL) { Toast.makeText(getApplicationContext(), "Double Click Event", Toast.LENGTH_SHORT).show(); mHasDoubleClicked = true; } else { // If not double click.... mHasDoubleClicked = false; Handler myHandler = new Handler() { public void handleMessage(Message m) { if (!mHasDoubleClicked) { Toast.makeText(getApplicationContext(), "Single Click Event", Toast.LENGTH_SHORT).show(); } } }; Message m = new Message(); myHandler.sendMessageDelayed(m,DOUBLE_PRESS_INTERVAL); } // record the last time the menu button was pressed. lastPressTime = pressTime; return true; } 

我修改了此代码以检测保留事件(长按)以及短按和双击。 它的工作原理是延迟检测短暂的点击,直到事件尚未处理的ACTION_UP事件(clickHandled == false)。

为onShortClick(),onLongClick()和onDoubleClick()提供自己的方法。

 private long thisTouchTime; private long previousTouchTime = 0; private long buttonHeldTime; private boolean clickHandled = false; private long DOUBLE_CLICK_INTERVAL = ViewConfiguration.getDoubleTapTimeout(); private long LONG_HOLD_TIMEOUT = ViewConfiguration.getLongPressTimeout(); @Override public boolean onTouch(View v, final MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: thisTouchTime = System.currentTimeMillis(); if (thisTouchTime - previousTouchTime <= DOUBLE_CLICK_INTERVAL) { // double click detected clickHandled = true; onDoubleClick(event); } else { // defer event handling until later clickHandled = false; } previousTouchTime = thisTouchTime; break; case MotionEvent.ACTION_UP: if (!clickHandled) { buttonHeldTime = System.currentTimeMillis() - thisTouchTime; if (buttonHeldTime > LONG_HOLD_TIMEOUT) { clickHandled = true; onLongClick(event); } else { Handler myHandler = new Handler() { public void handleMessage(Message m) { if (!clickHandled) { clickHandled = true; onShortClick(event); } } }; Message m = new Message(); myHandler.sendMessageDelayed(m, DOUBLE_CLICK_INTERVAL); } } break; case MotionEvent.ACTION_MOVE: myParams.x = initialDrawX + (int) (event.getRawX() - initialTouchX); myParams.y = initialDrawY + (int) (event.getRawY() - initialTouchY); windowManager.updateViewLayout(v, myParams); break; } return false; }