设置dialog显示,自定义时间到后dialog消失

方法一:

public class  MyDialog extends Dialog {

 private int FLAG_DISMISS = 1;

 private boolean flag = true;

 

 public MyDialog(Context context) {

     super(context);

     setTitle("自动消失对话框测试!");

 }



@Override

 public void show() {

     super.show();

     mThread.start();

 }



@Override

 public void dismiss() {

     super.dismiss();

     flag = false;

 }



private Thread mThread = new Thread(){

     @Override

     public void run() {

  super.run();

  while(flag){

      try {

   Thread.sleep(2000);

   Message msg = mHandler.obtainMessage();

   msg.what = FLAG_DISMISS;

   mHandler.sendMessage(msg);

      } catch (InterruptedException e) {

   e.printStackTrace();

      }

  }

     }   

 };

 

 private Handler mHandler = new Handler(){

     @Override

     public void handleMessage(Message msg) {

  super.handleMessage(msg);

  if(msg.what == FLAG_DISMISS)

      dismiss();

     }

     

 };

 

    }

方法二:

 

 

自定义一个定时器:

public static class TimeCount extends CountDownTimer 

{

    public TimeCount(long millisInFuture, long countDownInterval) 

    {

        super(millisInFuture, countDownInterval);

    }

    @Override

    public void onFinish() {

    // TODO Auto-generated method stub

    alertDialog.dismiss();//alertDialog是你的对话框

    }

}

然后在你自己的程序中AlertDialog部分中添加:

TimeCount timer = new TimeCount(7000, 1000);//具体时间自定

timer.start();


 

 


 

你可能感兴趣的:(dialog)