繁体   English   中英

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

[英]How can I create a Single Click Event and Double Click Event when the Menu Button is pressed?

我希望能够在按下菜单按钮时检测到单击或双击。 如果检测到单击,甚至会发生一次单击;如果检测到双击,则会发生另一事件。 这是我尝试过的方法(使用吐司代替事件):

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM