LongClick 事件发生得太快.如何增加触发它所需的点击时间?

LongClick event happens too quickly. How can I increase the clicktime required to trigger it?(LongClick 事件发生得太快.如何增加触发它所需的点击时间?)

本文介绍了LongClick 事件发生得太快.如何增加触发它所需的点击时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在处理的应用程序中,我要求用户必须单击 &在某个动作发生之前持有一个组件一段时间.

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.

我目前正在使用OnLongClickListener来监听longclick,但是我发现触发OnLongClick事件的点击时长太短了.

I'm currently using an OnLongClickListener to listen for the longclick, but I find that the length of a click to trigger the OnLongClick event is too short.

例如,假设 LongClick 事件在单击 400 毫秒后触发,但我希望用户必须单击 &在事件触发前保持 1200ms.

For example, let's say the LongClick event triggers after a 400ms click, but I want the user to have to click & hold for 1200ms before the event triggers.

有什么方法可以将 LongClick 事件配置为需要更长的点击时间?
或者是否有其他构造可以让我听到更长的点击?

Is there any way I can configure the LongClick event to require a longer click?
Or is there perhaps another construct that would allow me to listen for longer clicks?

推荐答案

onLongClick事件不能改变定时器,由android自己管理.

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

可以使用 .setOnTouchListener().

What is possible is to use .setOnTouchListener().

然后在 MotionEvent 为 ACTION_DOWN 时注册.
请注意变量中的当前时间.
然后当一个带有 ACTION_UP 的 MotionEvent 被注册并且 current_time - actionDown 时间 > 1200 毫秒时,然后做一些事情.

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.

差不多:

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;
        }
    })

这篇关于LongClick 事件发生得太快.如何增加触发它所需的点击时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:LongClick 事件发生得太快.如何增加触发它所需的点击时间?

基础教程推荐