LongClick event happens too quickly. How can I increase the clicktime required to trigger it?

后端 未结 8 1517
暖寄归人
暖寄归人 2020-12-08 21:26

In an application I\'m working on, I have the requirement that a user must click & hold a component for a period time before a certain action occurs.

I\'m curren

8条回答
  •  萌比男神i
    2020-12-08 22:16

    It is not possible to change the timer on the onLongClick event, it is managed by android itself.

    What is possible is to use .setOnTouchListener().

    Then register when the MotionEvent is a ACTION_DOWN.
    Note the current time in a variable.
    Then when a MotionEvent with ACTION_UP is registered and the current_time - actionDown time > 1200 ms then do something.

    so pretty much:

    Button button = new Button();
    long then = 0;
        button.setOnTouchListener(new OnTouchListener() {
    
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    then = (Long) System.currentTimeMillis();
                }
                else if(event.getAction() == MotionEvent.ACTION_UP){
                    if(((Long) System.currentTimeMillis() - then) > 1200){
                        return true;
                    }
                }
                return false;
            }
        })
    

提交回复
热议问题