简体   繁体   English

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

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

I want to be able to detect a single click or a double click when the menu button is pressed. 我希望能够在按下菜单按钮时检测到单击或双击。 If a single click is detected one even will happen, if a double click is detected a different event will happen. 如果检测到单击,甚至会发生一次单击;如果检测到双击,则会发生另一事件。 Here is what I've tried(Using toast in place of events): 这是我尝试过的方法(使用吐司代替事件):

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

The problem is that a single click event is detected every time before a double click event. 问题是每次双击事件之前都会检测到一次单击事件。

Simple logic mistake. 简单的逻辑错误。 You are returning before recording the new lastPressTime. 您将在录制新的lastPressTime之前返回。 You should only have one return call if they are both returning the same thing: 如果它们都返回相同的内容,则您应该只有一个返回调用:

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

I modified this code to detect hold events (long clicks) as well as short and double clicks. 我修改了这段代码,以检测保持事件(长按)以及短按和双击。 It works by delaying detection of a short click until an ACTION_UP event, where the event has not already been handled (clickHandled == false). 它的工作方式是将对短暂点击的检测延迟到尚未处理该事件的ACTION_UP事件为止(clickHandled == false)。

Provide your own methods for onShortClick(), onLongClick() and and onDoubleClick(). 为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