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

后端 未结 8 1423
暖寄归人
暖寄归人 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:04

    I found a simple solution studying how the long press event works. Each time a view is clicked, a Runnable of type CheckForLongPress is added to a queue with a delay. If the delay ends, the OnLongClickListener is called. If there is a different event before the delay ends, then, the CheckForLongPress Runnable is removed from de queue.

    I just override the public method postDelayed(Runnable action, long delayMillis) of the view to change the OS delay

    @Override public boolean postDelayed(Runnable action, long delayMillis) {
        boolean isLongPress = action.getClass().getSimpleName().equals("CheckForLongPress");
        return super.postDelayed(action, isLongPress ? LONG_PRESS_MILLIS : delayMillis);
    }
    

    I set LONG_PRESS_MILLIS to 100 and it's working!

    Hope it helps!!! ;)

提交回复
热议问题