如何让按住按钮启动计时器? [英] How to make press and hold button to Start a timer?

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

问题描述

我想做一个安卓定时器应用.当我们按下一个按钮并按住它时计时器开始计时,并在松开按钮时停止计时.请告诉我功能相同的方法.我正在关注一个例子 http://examples.javacodegeeks.com/android/core/os/handler/android-timer-example/ 制作一个计时器.

I want to make a android timer app. The timer starts when we press a button and hold it and stops when releasing a button. Please tell me the method that functions the same. I am following an example http://examples.javacodegeeks.com/android/core/os/handler/android-timer-example/ to make a timer.

推荐答案

使用定时器逻辑覆盖按钮的 onTouch 侦听器

override onTouch listener to the button with your timer logic

findViewById(R.id.btn_add).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    // start your timer

                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                   // stop your timer.

                }
                return false;
            }
        });

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

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