AlertDialogs show/dismiss 个接一个
AlertDialogs show/dismiss one after each other
我有一个消息和时间列表,我想用 AlertDialog
显示它们;
alertDialog 应该在时间过去后关闭,下一个对话框应该显示到列表的末尾
AlertList模型结构:(int time, String message, boolean cancelable)
AlertList a0 = new AlertList(5, "11111111111", false);
AlertList a1 = new AlertList(2, "222222222", true);
AlertList a2 = new AlertList(2, "3333333333333", false);
AlertList a3 = new AlertList(2, "4444444444444444444444", true);
List<AlertList> list = new ArrayList<>();
list.add(a0);
list.add(a1);
list.add(a2);
list.add(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();
// }
}
}
}
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();
在这种情况下你不应该使用任何循环(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();
}
然后你应该在 onCreate
方法中调用一次 showDialogs
方法。
我有一个消息和时间列表,我想用 AlertDialog
显示它们;
alertDialog 应该在时间过去后关闭,下一个对话框应该显示到列表的末尾
AlertList模型结构:(int time, String message, boolean cancelable)
AlertList a0 = new AlertList(5, "11111111111", false);
AlertList a1 = new AlertList(2, "222222222", true);
AlertList a2 = new AlertList(2, "3333333333333", false);
AlertList a3 = new AlertList(2, "4444444444444444444444", true);
List<AlertList> list = new ArrayList<>();
list.add(a0);
list.add(a1);
list.add(a2);
list.add(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();
// }
}
}
}
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();
在这种情况下你不应该使用任何循环(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();
}
然后你应该在 onCreate
方法中调用一次 showDialogs
方法。