美文网首页
触摸屏幕任一位置重置倒计时

触摸屏幕任一位置重置倒计时

作者: 河婆墟邓紫棋 | 来源:发表于2016-11-26 22:13 被阅读233次

需求:界面上有一个倒计时,倒计时结束返回主界面,用户触摸屏幕即刻重置倒计时

解决方案:
1.在activity中,重写dispatchTouchEvent方法,重置倒计时
2.在fragment中,写一个监听,让fragment去监听dispatchTouchEvent
3.在dialog中,继承dialog,重写dispatchTouchEvent方法

倒计时:CountDownTimer:

private long mTotalTime = 90 * 1000;
private long mTickTime = 1 * 1000;

private CountDownTimer mCountDownTimer;
/**
* 启动倒计时
*/
public void startCountDown() {
    if (mCountDownTimer != null) {
        mCountDownTimer.start();
    } else {
        mCountDownTimer = new CountDownTimer(mTotalTime, mTickTime) {
            @Override
            public void onTick(long millisUntilFinished) {
                mTextView.setText(millisUntilFinished / 1000 + "s");
            }

            @Override
            public void onFinish() {
                mTextView.setText("onFinish");
            }
        };
        mCountDownTimer.start();
    }
}

/*
* 重启倒计时
*/
public void restartCountDown() {
    if (mCountDownTimer != null) {
        mCountDownTimer.cancel();
        mCountDownTimer.start();
    } else {
        startCountDown();
    }
}

在activity中,dispatchTouchEvent方法重启倒计时

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    restartCountDown();
    return super.dispatchTouchEvent(ev);
}

在dialog中,先查看源码:

/**
 * Called to process touch screen events.  You can override this to
 * intercept all touch screen events before they are dispatched to the
 * window.  Be sure to call this implementation for touch screen events
 * that should be handled normally.
 * 
 * @param ev The touch screen event.
 * 
 * @return boolean Return true if this event was consumed.
 */
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (mWindow.superDispatchTouchEvent(ev)) {
        return true;
    }
    return onTouchEvent(ev);
}
/**
 * Called when a touch screen event was not handled by any of the views
 * under it. This is most useful to process touch events that happen outside
 * of your window bounds, where there is no view to receive it.
 * 
 * @param event The touch screen event being processed.
 * @return Return true if you have consumed the event, false if you haven't.
 *         The default implementation will cancel the dialog when a touch
 *         happens outside of the window bounds.
 */
public boolean onTouchEvent(MotionEvent event) {
    if (mCancelable && mShowing && mWindow.shouldCloseOnTouch(mContext, event)) {
        cancel();
        return true;
    }
    
    return false;
}

Dialog已经消费touch事件,现新建一个DispatchDialog继承Dialog,重写dispatchTouchEvent方法:

import android.app.Dialog;
import android.content.Context;
import android.view.MotionEvent;

/**
 * Created by ZP on 2016/11/26.
 */

public class DispatchDialog extends Dialog {


    public DispatchDialog(Context context) {
        super(context);
    }

    public DispatchDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    protected DispatchDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (mOnTouchListener != null) {
            mOnTouchListener.onTouch(ev);
        }
        return super.dispatchTouchEvent(ev);
    }


    public interface onTouchListener{
        void onTouch(MotionEvent ev);
    }

    private onTouchListener mOnTouchListener;

    public void setOnTouchListener(onTouchListener onTouchListener) {
        mOnTouchListener = onTouchListener;
    }
}

如何调用:

private DispatchDialog mDialog;
mDialog = new DispatchDialog(MainActivity.this);
mDialog.setOnTouchListener(new DispatchDialog.onTouchListener() {
    @Override
    public void onTouch(MotionEvent ev) {
        restartCountDown();
    }
});
mDialog.setCanceledOnTouchOutside(false);
mDialog.show();

总结:
读者自行尝试验证PopupWinodow

相关文章

  • 触摸屏幕任一位置重置倒计时

    需求:界面上有一个倒计时,倒计时结束返回主界面,用户触摸屏幕即刻重置倒计时 解决方案:1.在activity中,重...

  • iOS14开发-触摸与手势识别

    触摸 概念 UITouch 用于描述触摸的窗口、位置、运动和力度。一个手指触摸屏幕,就会生成一个 UITouch ...

  • van-count-down 倒计时相关问题

    1. 倒计时重置问题 背景:业务需要在开启倒计时、暂停、恢复倒计时实时判断是否展示倒计时,并重置倒计时 问题:直接...

  • iOS UITouch触摸事件简介

    UITouch表示屏幕上发生的触摸的位置,大小,移动和力度的对象。 //单点触摸按下事件UIControlEven...

  • UITouch笔记

    UITouch是什么表示在在屏幕上触摸事件,包括触摸的位置、大小、力度(3D touch)、运动。 在一系列触摸事...

  • 安卓事件分发

    事件分发 MotionEvent 当用户触摸屏幕时,将产生点击事件,Touch事件的相关细节(发生触摸的位置、时间...

  • Android事件分发机制

    1、基础知识 1.1 定义 当用户触摸手机屏幕会产生点事件(Touch事件),Touch事件的相关细节(触摸位置、...

  • Flutter了解之手势

    1. 原始指针事件 (移动设备上通常为触摸事件) 描述了屏幕上指针(触摸、鼠标、触控笔)的位置和移动。 监听 Fl...

  • UITouch对象详解

    概述 ios中对UITouch对象的定义是表示在屏幕上发生的触摸的位置、大小、运动和力的物体 触摸对象包含内容 1...

  • IOS事件传递和响应

    1、触摸屏幕产生触摸事件 触摸屏幕产生触摸事件后,触摸事件会被添加到由UIApplication管理的事件队列中(...

网友评论

      本文标题:触摸屏幕任一位置重置倒计时

      本文链接:https://www.haomeiwen.com/subject/pmmgpttx.html