按住按钮时如何获得按钮按下时间 [英] How can I get button pressed time when I holding button on

查看:63
本文介绍了按住按钮时如何获得按钮按下时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,我按下它并继续按住它,如果按住时间超过一定的时间间隔,它会触发某种意图,我该怎么做.谢谢

I have a button, I press it and continue to holding it, if holding time exceeds certain time interval it fires some kind of intent, how can i do this. Thanks

推荐答案

试试这个

您可以使用 Touch Listener 来做到这一点.

You can use Touch Listener to do this.

试试:

Handler handler = new Handler();
    b.setOnTouchListener(new View.OnTouchListener() {

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

            default:
                handler.removeCallbacks(run);
                break;

            }
            return true;
        }
    });

其中 b 是您想要长按的 view(在您的情况下它应该是按钮).

Where b is the view (in your case it should button) on which you want to make long click.

Runnable run如下

Runnable run = new Runnable() {

    @Override
    public void run() {
        // Your code to run on long click

    }
};

希望它会有所帮助... :)

Hope it will help... :)

这篇关于按住按钮时如何获得按钮按下时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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