Change long click delay

后端 未结 7 1649
不知归路
不知归路 2020-12-09 08:45

I am listening for a View\'s long click events via setOnLongClickListener(). Can I change the long click delay / duration?

7条回答
  •  旧时难觅i
    2020-12-09 09:25

    This is my way for set duration to long press

    private int longClickDuration = 3000;
    private boolean isLongPress = false;
    
    numEquipeCheat.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    isLongPress = true;
                    Handler handler = new Handler();
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if (isLongPress) {
                                Vibrator vibrator = (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE);
                                vibrator.vibrate(100);
                                // set your code here
                                // Don't forgot to add  to vibrate.
                            }
                        }
                    }, longClickDuration);
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    isLongPress = false;
                }
                return true;
            }
        });
    

提交回复
热议问题