我定义了一个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);
它不起作用。这是一种创建自定义对话框的正式方法吗?如果不是,有人能告诉我创建一个的正确方法吗?
答案 0 :(得分: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);
}
}