简体   繁体   English

如何正确设置自定义长按监听器?

[英]How to properly set custom long click listener?

I'm trying to set my own long click listener on Unlock button. 我试图在“解锁”按钮上设置自己的长按监听器。 Whenever I press the Unlock button it summarize duration and I can unlock permanently clicking. 每当我按下“解锁”按钮时,它就会总结持续时间,并且我可以永久地解锁。

Unlock.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(final View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        Unlock.setText("Press to unlock");
                        isLongPress = true;
                        Handler handler = new Handler();
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                if (isLongPress) {
                                    Unlock();
                                }
                            }
                        }, longClickDuration); //amount of time of long click
                    } else if (event.getAction() == MotionEvent.ACTION_UP) {
                        Unlock.setText("Unlock");
                        isLongPress = false;
                    }
                    return true;
                }
            });
        }catch (Exception e) {
            // TODO: handle exception
        }
    }

If you want to just handle long clicks consider using the following code: 如果您只想处理长点击,请考虑使用以下代码:

        Unlock.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                your code
            }
        });

But if the Unlock(); 但是如果Unlock(); should be invoked after a certain (customizable) amount of time, you should measure this time in MotionEvent.ACTION_UP handler. 应该在一定时间(可自定义)后调用,您应该在MotionEvent.ACTION_UP处理程序中测量该时间。 As @Attaullah Khan said, use SystemClock.elapsedRealtime() system timer to correctly count number of milliseconds at two moments (when button was pressed and released) and if the time is greater than longClickDuration then invoke Unlock 就像@Attaullah Khan所说的那样,请使用SystemClock.elapsedRealtime()系统计时器正确地计算两分钟(按下和释放按钮时)的毫秒数,如果时间大于longClickDuration则调用Unlock

The handler.postDelayed that you call in MotionEvent.ACTION_DOWN handler just invokes a check of pressed state after longClickDuration interval and if your button gets suddenly pressed at that moment, the verification passes that is not correct handler.postDelayed您在调用MotionEvent.ACTION_DOWN处理程序只是调用按下状态的检查之后longClickDuration间隔,如果你的按钮被在那一刻突然用力,验证通过,是不是正确的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在BottomNavigationView中的项目上设置长按侦听器? - How to set long click listener on items in BottomNavigationView? 如何为自定义按钮设置点击监听器? - How to set on click listener for a custom button? Android:如何在自定义适配器中设置侦听器并获取图像按钮单击的位置 - Android : how to set listener and get position of imagebutton click in a custom adapter 如何将单击/长按侦听器添加到 ViewPager2 - How to add click/long click listener to ViewPager2 如何为 AppWidgetHostView 制作长按侦听器 - How can I make a long click listener for an AppWidgetHostView 如何在单个列表视图中实现长按一下监听器和onclicklistener - how to implement a long click listener and onclicklistener in single listview LibGDX - 自定义单击侦听器? - LibGDX - Custom Click Listener? 如何设置“项目点击侦听器”上的视图寻呼机? 设置点击监听器不起作用 - How can I set view pager's on Item Click Listener? Set On Click Listener is not working 如何使用 Java Swing 在按钮操作侦听器上正确设置 session - How to properly set the session on the button action listener using Java Swing 自定义ListAdapter中的单击侦听器如何修改列表数据? - How can a click Listener in a custom ListAdapter modify the lists data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM