【问题标题】:Detect button long press and press android button检测按钮长按并按下android按钮
【发布时间】:2015-11-23 22:46:14
【问题描述】:

我有一个按钮,我试图在一个按钮中触发两个动作。 在按钮长按时调用方法takeVideo。在按钮按下时我想调用方法imageCapture

以下代码可用于长按。但我无法检测到按下按钮以仅调用 imageCapture 方法。

takePhotoBtn.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            long down;
            int action = motionEvent.getAction();
            if (action == MotionEvent.ACTION_BUTTON_PRESS) {
           imageCapture();
                return true;
            }
            if (action == MotionEvent.ACTION_DOWN) {

                    takeVideo();
                    timer.start();
                    return true;

            } else if (action == MotionEvent.ACTION_UP) {
                takeVideo();
                timer.cancel();
                return true;
            }

            return false;
        }


    });

编辑

我没有使用OnLongClickListenerOnClickListener

我想在MotionEvent.ACTION_UP 上执行其他操作,所以我尝试在这种情况下使用setOnTouchListener 来解决我的问题

【问题讨论】:

标签: android button ontouchlistener


【解决方案1】:

您可以通过official documentation (getLongPressTimeOut) 获得长按的持续时间,它曾经是 1000 毫秒,现在是 500 毫秒……它可能会改变。这就是为什么你需要得到超时。

然后,在 ACTION_DOWN 上,您可以计算毫秒...如果 ms >= 500(如果当前为 500 毫秒)则为 LongPress。但是,就像马里奥斯所说的那样,使用 OnLongClickListenerOnClickListener

【讨论】:

    【解决方案2】:

    【讨论】:

    • 我想使用MotionEvent.ACTION_UP 执行其他操作,这就是为什么我不使用onLongClickListener
    【解决方案3】:

    setOnLongClickListener 可以用于此。

    takePhotoBtn.setOnLongClickListener(new OnLongClickListener() { 
        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            return true;
        }
    });
    

    【讨论】:

    • 我想使用MotionEvent.ACTION_UP 执行其他操作,这就是为什么我不使用onLongClickListener
    猜你喜欢
    • 2012-02-11
    • 2015-04-12
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多