简体   繁体   English

如何检测Android中按下按钮的时间

[英]how to detect the period for which a button is press in android

Is there any way i can detect for how long a button is press?? 有什么方法可以检测按钮按下了多长时间? I want to capture the time for which a button is press and act accordingly. 我想记录一下按下按钮的时间并采取相应的措施。 So if a user kept pressing the button for 5 sec i want to detect that 5 sec on android. 因此,如果用户持续按住按钮5秒钟,我想在Android上检测到5秒钟。

Please let me know 请告诉我

Thanks Pranay 谢谢普兰尼

Use following to determine the touch duration.You can use that in an if statement: event.getEventTime() - event.getDownTime() > 5000 It calculates in ms, which means that for your 5 sec you need this number to be 5000 使用以下命令确定触摸持续时间。您可以在if语句中使用它:event.getEventTime()-event.getDownTime()> 5000它以毫秒为单位计算,这意味着在5秒钟内您需要将此数字设为5000

DON'T USE: android.os.SystemClock.elapsedRealtime()-event.getDownTime() It might work on the simulator, but it won't work on the device! 请勿使用:android.os.SystemClock.elapsedRealtime()-event.getDownTime()它可能会在模拟器上运行,但无法在设备上运行! Don't ask me how I know it ;) 不要问我我怎么知道;)

Give the button a View.OnTouchListener . 给按钮一个View.OnTouchListener The onTouch method you'll implement will give you access to a MotionEvent . 您将实现的onTouch方法将使您能够访问MotionEvent Then, using getFlags(), you'll know when the user starts pressing the button (ACTION_DOWN) and when he stops (ACTION_UP). 然后,使用getFlags(),您将知道用户何时开始按下按钮(ACTION_DOWN)和何时停止(ACTION_UP)。 Simply record the system time when these occur (or as suggested in another answer, getDownTime() will give the time you need, but only when you have the ACTION_UP flag). 只需记录发生这种情况的系统时间即可(或按照另一个答案中的建议,getDownTime()会给出您需要的时间,但仅当您具有ACTION_UP标志时)。

    private long timeElapsed = 0L; //make this a global variable

    //tvTouches could be a TextView or Button or other views
    tvTouches.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    timeElapsed = event.getDownTime();
                    Log.d("setOnTouchListener", "ACTION_DOWN at>>>" + event.getDownTime());
                    break;
                case MotionEvent.ACTION_UP:
                    timeElapsed = event.getEventTime() - timeElapsed;
                    Log.d("setOnTouchListener", "ACTION_UP at>>>" + event.getEventTime());
                    Log.d("setOnTouchListener", "Period of time the view is pressed>>>" + timeElapsed);
                    Toast.makeText(getApplicationContext(), "Period of time the view is pressed in milliseconds>>>" + timeElapsed, Toast.LENGTH_SHORT).show();
                    timeElapsed = 0L;
                    //TODO do something when a certain period of time has passed
                    break;
                default:
                    break;
            }
            return true;
        }
    });

Register a OnTouchListener on the Button. 在按钮上注册一个OnTouchListener。 Then in the listener use the MotionEvent: 然后在侦听器中使用MotionEvent:

http://developer.android.com/reference/android/view/MotionEvent.html http://developer.android.com/reference/android/view/MotionEvent.html

Then use the getDownTime() method of the Event: 然后使用Event的getDownTime()方法:

http://developer.android.com/reference/android/view/MotionEvent.html#getDownTime%28%29 http://developer.android.com/reference/android/view/MotionEvent.html#getDownTime%28%29

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM