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

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

问题描述

我想制作一个Android计时器应用.当我们按住一个按钮时,计时器开始计时,而当释放按钮时,计时器停止计时.请告诉我功能相同的方法.我正在举一个 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天全站免登陆