简体   繁体   English

我们如何在Android中增加按钮长点击监听时间?

[英]how can we increase button long click listener time in android?

i want to execute simple click listener as well as long click listener on same button. 我想在同一按钮上执行简单的单击侦听器以及长单击侦听器。 But i need to execute long click listener after 5 seconds delay in the longclicklistener executes after 1 seconds of hold .so using handler it will executes after 5 seocnds.but i need exact to press button for 5 seconds then code to executes ... 但是我需要在longclicklistener延迟1秒后执行5秒延迟后执行long click侦听器。因此使用处理程序将在5秒后执行。但是我需要精确按下按钮5秒钟然后执行代码...

It is not possible to change the timer on the onLongClick event, it is managed by android itself. 无法更改onLongClick事件上的计时器,它由android本身管理。

What is possible is to use .setOnTouchListener(). 可以使用.setOnTouchListener()。

Then register when the MotionEvent is a ACTION_DOWN. 然后在MotionEvent为ACTION_DOWN时注册。 Note the current time in a variable. 注意变量中的当前时间。 Then when a MotionEvent with ACTION_UP is registered and the current_time - actionDown time > 5000 ms then do something. 然后,当注册带有ACTION_UP的MotionEvent且current_time-actionDown时间> 5000 ms时,请执行以下操作。

so pretty much: 差不多了:

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) > 5000){
                    // 5 second of long click
                    return true;
                }
            }
            return false;
        }
    })

you can use Handler like this way: 您可以这样使用Handler

Button b=findViewById(R.id.btn);

    final Runnable run = new Runnable() {

        @Override
        public void run() {
            Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_SHORT).show();
            // Your code to run on long click

        }
    };
    final Handler handel = new Handler();
    b.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            switch (arg1.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    handel.postDelayed(run, 5000/* OR the amount of time you want */);
                    break;

                default:
                    handel.removeCallbacks(run);
                    break;

            }
            return true;
        }
    });

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

相关问题 我们可以将android中的Options Menu挂在Button Click侦听器上吗? - Can we hook Options Menu in android over a Button click listener? 在Android中单击“监听器”,如何检查在特定时间段内是否未单击任何按钮? - Click Listener in Android, How to check if no Button is clicked for particular time period? Android长按监听器 - Android long click listener 当我们在android中单击按钮时如何保存日期和时间 - how to save date and time when we click on a button in android 如何在点击Android中的Button时显示Page Curl Animation - How can we show Page Curl Animation on click of the Button in android 如何使用广播侦听器检测耳机按钮长按? - How to detect headset button long click with broadcast listener? android:如何增加android中listview项目的长按时间? - android: How to increase long press time for listview items in android? 我们可以在Android的ListView中同时拥有Button和onItemClick侦听器吗? - can we have both of Button and onItemClick listener in ListView in android? 如何在android中的长按监听器上编辑贴纸视图文本 - How to edit stickerview text on long click Listener in android 如何为 AppWidgetHostView 制作长按侦听器 - How can I make a long click listener for an AppWidgetHostView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM