更改长按延迟

fhu*_*cho 20 android view

我正在通过setOnLongClickListener()监听View的长按事件.我可以更改长按延迟/持续时间吗?

Com*_*are 17

AFAIK,没有.它通过getLongPressTimeout()on 在框架中硬连线ViewConfiguration.

欢迎您处理自己的触摸事件并定义自己的"长按"概念.请确保它与用户期望的不同,并且很可能用户会期望所有其他应用程序使用的内容,即标准的500毫秒持续时间.

  • 您将如何定义自己的长按概念? (2认同)

小智 16

这是我设定持续时间长按的方式

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 <uses-permission android:name="android.permission.VIBRATE" /> to vibrate.
                        }
                    }
                }, longClickDuration);
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                isLongPress = false;
            }
            return true;
        }
    });
Run Code Online (Sandbox Code Playgroud)


Cum*_*bus 6

这是我找到的针对此限制的最简单的工作解决方案:

//Define these variables at the beginning of your Activity or Fragment:
private long then;
private int longClickDuration = 5000; //for long click to trigger after 5 seconds

...

//This can be a Button, TextView, LinearLayout, etc. if desired
ImageView imageView = (ImageView) findViewById(R.id.desired_longclick_view);
imageView.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 ((System.currentTimeMillis() - then) > longClickDuration) {
            /* Implement long click behavior here */
            System.out.println("Long Click has happened!");
            return false;
          } else {
            /* Implement short click behavior here or do nothing */
            System.out.println("Short Click has happened...");
            return false;
          }
        }
        return true;
      }
    });
Run Code Online (Sandbox Code Playgroud)


vov*_*ost 6

我在Kotlin中定义了一个扩展函数,其灵感来自@Galoway答案:

fun View.setOnVeryLongClickListener(listener: () -> Unit) {
    setOnTouchListener(object : View.OnTouchListener {

        private val longClickDuration = 2000L
        private val handler = Handler()

        override fun onTouch(v: View?, event: MotionEvent?): Boolean {
            if (event?.action == MotionEvent.ACTION_DOWN) {
                handler.postDelayed({ listener.invoke() }, longClickDuration)
            } else if (event?.action == MotionEvent.ACTION_UP) {
                handler.removeCallbacksAndMessages(null)
            }
            return true
        }
    })
}
Run Code Online (Sandbox Code Playgroud)

像这样使用它:

button.setOnVeryLongClickListener{
    // Do something here
}
Run Code Online (Sandbox Code Playgroud)


mpk*_*uth 5

这就是我使用的。它与 Cumulo Nimbus 的回答相似,但有两个显着差异。

  1. 使用已存储的事件值作为停机时间和当前时间。除了不重复工作之外,这还具有使侦听器可同时用于多个视图的良好效果,而无需跟踪每个单独事件的开始时间。
  2. 检查view.isPressed以确保用户在触摸事件期间没有离开视图。这模仿了onClick和的默认系统行为onLongClick

long longPressTimeout = 2000;

@Override
public boolean onTouch(View view, MotionEvent event) {
    if (view.isPressed() && event.getAction() == MotionEvent.ACTION_UP) {
        long eventDuration = event.getEventTime() - event.getDownTime();
        if (eventDuration > longPressTimeout) {
            onLongClick(view);
        } else {
            onClick(view);
        }
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

如果视图通常不会点击你需要调用view.setClickable(true)view.isPressed()检查工作。