python弹框倒计时自动关闭_Dialog中显示倒计时,到时自己主动关闭

这里直接用系统Dialog中加入了倒计时的显示,假设用自己定义Dialog会更美观;

private TextView mOffTextView;

private Handler mOffHandler;

private Timer mOffTime;

private Dialog mDialog;

//创建对话框

void initDialog(){

mOffTextView = new TextView(this);

mDialog = new AlertDialog.Builder(this)

.setTitle("提示")

.setCancelable(false)

.setView(mOffTextView)

.setPositiveButton("确定", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int id) {

mOffTime.cancel();

off();关闭后的一些操作

}

})

.setNegativeButton("取消", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int id) {

dialog.cancel();

mOffTime.cancel();

}

})

.create();

mDialog.show();

mDialog.setCanceledOnTouchOutside(false);

mOffHandler = new Handler() {

public void handleMessage(Message msg) {

if (msg.what > 0) {

动态显示倒计时

mOffTextView.setText(" 即将关闭:"+msg.what);

} else {

倒计时结束自己主动关闭

if(mDialog!=null){

mDialog.dismiss();

}

off();关闭后的操作

mOffTime.cancel();

}

super.handleMessage(msg);

}

};

//倒计时

mOffTime = new Timer(true);

TimerTask tt = new TimerTask() {

int countTime = 10;

public void run() {

if (countTime > 0) {

countTime--;

}

Message msg = new Message();

msg.what = countTime;

mOffHandler.sendMessage(msg);

}

};

mOffTime.schedule(tt, 1000, 1000);

}

效果图

f7dc9647da599c444e8b3966f267c282.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值