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

查看:849
本文介绍了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来监听长按,但是我发现点击的长度触发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事件在点击400ms后触发,但我希望用户必须单击&在事件触发之前保持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 ms时,可以执行某些操作。

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事件发生得太快了。如何增加触发它所需的点击时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆