zoukankan      html  css  js  c++  java
  • 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);
         }
    
    


     

    效果图

  • 相关阅读:
    bzoj 1071: [SCOI2007]组队
    bzoj 4872: [Shoi2017]分手是祝愿
    (python)循环中动态产生变量
    Python中的exec、eval的区别
    MATLAB中feval与eval的区别
    用intellij idea 写第一个Java程序
    Python词云的中文问题
    python里的apply,applymap和map的区别
    Python 正则表达式匹配小数
    字典的深拷贝与浅拷贝
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4280390.html
Copyright © 2011-2022 走看看