-1

我定义了一个 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);

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

4

1 回答 1

0
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);

        }
    }
于 2012-07-17T05:04:05.177 回答