首页 > 其他 > 详细

Dialog中显示倒计时,到时自己主动关闭

时间:2015-02-08 20:38:43      阅读:255      评论:0      收藏:0      [点我收藏+]

这里直接用系统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);
     }


 

效果图

技术分享

Dialog中显示倒计时,到时自己主动关闭

原文:http://www.cnblogs.com/mengfanrong/p/4280390.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!