【问题标题】:Why my custom dialog can not close automatically?为什么我的自定义对话框不能自动关闭?
【发布时间】:2012-07-17 04:16:59
【问题描述】:

我定义了一个 CustomDialog 类如下:

public class CustomDialog {
    Dialog dialog;
    public void show(Contex contex, int duration){
        Handler timeHandler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
    switch (msg.what) {
    case 1:
    dialog.dismiss();
    break;
    default:
    break;
    }               
    };
       dialog = new dialog(contex);
       dialog.show();
       Message message = new Message();
   message.what = 1 ;
   timeHandler.sendMessageDelayed(message, duration);
    }
}

在Activity中,我新建了一个CustomDialog对象,调用了show()方法,但是对话框不能自动关闭,谁能告诉我为什么? 活动中的代码:

CustomDialog myDialog = new CustomDialog();
myDialog.show(this,2000);

它不起作用。这是创建自定义对话框的正式方式吗?如果不是,谁能告诉我创建一个的正确方法是什么?

【问题讨论】:

    标签: android dialog


    【解决方案1】:
    Try this:
    
        public class CustomDialog {
            Dialog dialog;
            public void show(Contex contex, int duration){
                Handler timeHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
            switch (msg.what) {
            case 1:
            dismiss();
            break;
            default:
            break;
            }               
            };
               dialog = new dialog(contex);
               dialog.show();Timer timer = new Timer();
    Timer timer=new Timer(); 
       timer.schedule(new TimerTask() {
            @Override
            public void run() {
            //dismiss the dialog here
            dialog.dismiss();
            }
        }, duration);
    
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-06
      • 1970-01-01
      • 1970-01-01
      • 2011-09-18
      • 1970-01-01
      • 2013-07-26
      • 1970-01-01
      相关资源
      最近更新 更多