Java 警报对话框依次显示/关闭
我有一个消息和时间列表,我想用Java 警报对话框依次显示/关闭,java,android,for-loop,handler,android-alertdialog,Java,Android,For Loop,Handler,Android Alertdialog,我有一个消息和时间列表,我想用AlertDialog显示它们; alertDialog应在时间过后关闭,下一个对话框应显示在列表的末尾 AlertList模型结构:(整数时间,字符串消息,布尔可取消) AlertList a0=新的AlertList(5,“11111”,false); AlertList a1=新的AlertList(2,“222222”,真); AlertList a2=新的AlertList(2,“3333”,false); AlertList a3=新的AlertList
AlertDialog
显示它们;
alertDialog应在时间过后关闭,下一个对话框应显示在列表的末尾
AlertList模型结构:(整数时间,字符串消息,布尔可取消)
AlertList a0=新的AlertList(5,“11111”,false);
AlertList a1=新的AlertList(2,“222222”,真);
AlertList a2=新的AlertList(2,“3333”,false);
AlertList a3=新的AlertList(2,“444444”,真);
列表=新的ArrayList();
列表。添加(a0);
增加(a1);
增加(a2);
增加(a3);
我想在完成另一个alertDialog后显示alertDialog
if (list != null && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
// synchronized (this) {
int finalI = i;
AlertDialog dialog = new AlertDialog.Builder(context).create();
new Handler().post(() -> {
dialog.setMessage(list.get(finalI).getStrComment());
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
if (list.get(finalI).isCancelable()) {
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
// } else {
}
dialog.show();
new Handler().postDelayed(() -> {
dialog.dismiss();
// resume();
}, list.get(finalI).getTime() * 1000);
dialog.setOnDismissListener(dialog1 -> notify());
});
try {
Thread.sleep(list.get(finalI).getTime() * 1000 + 500);
} catch (InterruptedException e) {
e.printStackTrace();
}
// if (!paused)
// pause(finalI);
// notify();
// }
}
}
}
if(list!=null&&list.size()>0){
对于(int i=0;i{
setMessage(list.get(finalI.getStrComment());
对话框。可设置可取消(false);
对话框。setCanceledOnTouchOutside(false);
if(list.get(finalI.isCancelable()){
对话框。可设置可取消(true);
对话框。setCanceledOnTouchOutside(true);
//}其他{
}
dialog.show();
新处理程序().postDelayed(()->{
dialog.dismise();
//恢复();
},list.get(finalI.getTime()*1000);
setOnDismissListener(dialog1->notify());
});
试一试{
Thread.sleep(list.get(finalI.getTime()*1000+500);
}捕捉(中断异常e){
e、 printStackTrace();
}
//如果(!暂停)
//暂停(最后);
//通知();
// }
}
}
}
List List=new ArrayList();
列表。添加(a0);
增加(a1);
增加(a2);
增加(a3);
列表对话框=新建ArrayList();
用于(警报列表项:列表){
AlertDialog AlertDialog=新建AlertDialog.Builder(此)
.setMessage(item.getStrComment())
.setOnDismissListener(对话框->{
如果(!dialogs.isEmpty()){
//检查对话框列表是否为非空,则会删除顶部项目
对话框。删除(0);
//并在处理程序逻辑之后关闭当前对话框
dialog.dismise();
//然后显示列表中的下一个对话框
如果(!dialogs.isEmpty())
dialogs.get(0.show();
}
}).create();
添加(alertDialog);
}
//开始显示对话框的命令
dialogs.get(0.show();
在这种情况下,不应使用任何循环(for loop),因为它会同步创建所有对话框,而必须在通知上一个对话框(或操作)已完成时打开下一个对话框
在这种情况下,您可以通过setOnDismissListener
获得通知,或者当用户执行类似于单击按钮的操作时,递归函数调用可以帮助您完成想要做的事情
private void showDialogs(List<AlertList> list) {
if (list == null || list.size() == 0) return;
AlertList data = list.get(0);
list.remove(0);
AlertDialog dialog = new AlertDialog.Builder(context)
.setMessage(data.getStrComment())
.setCancelable(data.isCancelable())
.create();
dialog.setCanceledOnTouchOutside(data.isCancelable());
CountDownTimer timer = new CountDownTimer(data.getTime() * 1000, data.getTime() * 1000) {
@Override
public void onTick(long millisUntilFinished) { }
@Override
public void onFinish() {
dialog.dismiss();
}
};
dialog.setOnDismissListener(dialog1 -> {
timer.cancel();
showDialogs(list);
});
dialog.show();
timer.start();
}
private void显示对话框(列表){
if(list==null | | list.size()==0)返回;
AlertList数据=list.get(0);
列表。删除(0);
AlertDialog=新建AlertDialog.Builder(上下文)
.setMessage(data.getStrComment())
.setCancelable(data.isCancelable())
.create();
setCanceledOnTouchOutside(data.isCancelable());
CountDownTimer timer=新的CountDownTimer(data.getTime()*1000,data.getTime()*1000){
@凌驾
公共void onTick(长毫秒直至完成){}
@凌驾
公共无效onFinish(){
dialog.dismise();
}
};
dialog.setOnDismissListener(dialog1->{
timer.cancel();
显示对话框(列表);
});
dialog.show();
timer.start();
}
然后,您应该在onCreate
方法中调用showDialogs
方法一次
List<AlertList> list = new ArrayList<>();
list.add(a0);
list.add(a1);
list.add(a2);
list.add(a3);
List<Dialog> dialogs = new ArrayList<>();
for (AlertList item : list) {
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setMessage(item.getStrComment())
.setOnDismissListener(dialog -> {
if (!dialogs.isEmpty()) {
//checking if dialogs list not empty then will remove the top item
dialogs.remove(0);
//and dismiss the current dialog after your handler logic
dialog.dismiss();
//then showing then next dialog from the list
if (!dialogs.isEmpty())
dialogs.get(0).show();
}
}).create();
dialogs.add(alertDialog);
}
// the command to start showing dialogs
dialogs.get(0).show();
private void showDialogs(List<AlertList> list) {
if (list == null || list.size() == 0) return;
AlertList data = list.get(0);
list.remove(0);
AlertDialog dialog = new AlertDialog.Builder(context)
.setMessage(data.getStrComment())
.setCancelable(data.isCancelable())
.create();
dialog.setCanceledOnTouchOutside(data.isCancelable());
CountDownTimer timer = new CountDownTimer(data.getTime() * 1000, data.getTime() * 1000) {
@Override
public void onTick(long millisUntilFinished) { }
@Override
public void onFinish() {
dialog.dismiss();
}
};
dialog.setOnDismissListener(dialog1 -> {
timer.cancel();
showDialogs(list);
});
dialog.show();
timer.start();
}