Android Studio的代码笔记--倒计时学习1:Timer + TimerTask

倒计时:Timer + TimerTask + handler

Dialog

提示框

textViewDialog = new TextView(this);
dialogDialog = new AlertDialog.Builder(this)
	.setTitle("提示")
    .setCancelable(false)
    .setView(textViewDialog)
    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
    	public void onClick(DialogInterface dialog, int id) {
        	timerDialog.cancel();//关闭
        }
    })
    .create();
dialogDialog.show();
dialogDialog.setCanceledOnTouchOutside(false);

Timer + TimerTask

倒计时

timerDialog = new Timer(true);//倒计时
TimerTask tt = new TimerTask() {
	int countTime = 10;
    public void run() {
    	if (countTime > 0) {
        	countTime--;
        }
        Message msg = new Message();
        msg.what = countTime;
        handlerDialog.sendMessage(msg);
    }
};
timerDialog.schedule(tt, 1000, 1000);

Handler

handlerDialog = new Handler() {
	public void handleMessage(Message msg) {
		if (msg.what > 0) {
        	//动态显示倒计时
            textViewDialog.setText("\n"+"                  升级固件"+"\n"+"                         倒计时进入升级:"+msg.what);
        } else {
            //倒计时结束自动关闭
            if(dialogDialog!=null){
            	dialogDialog.dismiss();
            }
            timerDialog.cancel();
        }
		super.handleMessage(msg);
	}
};

使用

定义

TextView textViewDialog;
Dialog dialogDialog;
Handler handlerDialog;
Timer timerDialog;

放入自定义函数中使用更加方便

	void RunDialog(){
        textViewDialog = new TextView(this);
        dialogDialog = new AlertDialog.Builder(this)
                .setTitle("提示")
                .setCancelable(false)
                .setView(textViewDialog)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        timerDialog.cancel();//关闭--
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                        timerDialog.cancel();
                    }
                })
                .create();
        dialogDialog.show();
        dialogDialog.setCanceledOnTouchOutside(false);
        handlerDialog = new Handler() {
            public void handleMessage(Message msg) {
                if (msg.what > 0) {
                    //动态显示倒计时
                    textViewDialog.setText("\n"+"                   倒计时:"+msg.what);
                } else {
                    //倒计时结束自动关闭--
                    if(dialogDialog!=null){
                        dialogDialog.dismiss();
                    }
                        timerDialog.cancel();
                }
                super.handleMessage(msg);
            }
        };
        timerDialog = new Timer(true);//倒计时
        TimerTask tt = new TimerTask() {
            int countTime = 10;
            public void run() {
                if (countTime > 0) {
                    countTime--;
                }
                Message msg = new Message();
                msg.what = countTime;
                handlerDialog.sendMessage(msg);
            }
        };
        timerDialog.schedule(tt, 1000, 1000);
    }

使用时调用

RunDialog();

效果如图

在这里插入图片描述

欢迎指错,一起学习

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值