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

后端 未结 8 1544
暖寄归人
暖寄归人 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条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-08 22:17

    final boolean[] isLongPress = {false};
    final int duration = 3000;
    final Handler someHandler = new Handler();
        final Runnable someCall = new Runnable() {
            @Override
            public void run() {
                if(isLongPress[0]) {
                    // your code goes here 
                }
            }
        };
    
        someButton.setOnTouchListener(new View.OnTouchListener() {
    
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int eventAction = event.getAction();
                if(eventAction == MotionEvent.ACTION_DOWN){
                    isLongPress[0] = true;
                    someHandler.postDelayed(someCall, duration);
                }
                else if (eventAction == MotionEvent.ACTION_UP) {
                    isLongPress[0] = false;
                    someHandler.removeCallbacks(someCall);
                }
                return false;
            }
        });
    

提交回复
热议问题