AlertDialogs彼此显示/关闭

时间:2018-08-25 11:20:17

标签: java android for-loop handler alertdialog

我有一个消息列表和时间,我想用AlertDialog来显示它们; 时间过后,alertDialog应该关闭,下一个对话框应显示在列表的末尾

AlertList模型结构:(整数时间,字符串消息,布尔值可取消)

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();
//                }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

在这种情况下,您不应使用任何循环(for循环),因为它会同步创建所有对话框,相反,当您通知上一个对话框(或操作)完成时,您必须打开下一个对话框。

在这种情况下,您可以通过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();
}

然后您应该在showDialogs方法中调用一次onCreate方法。

答案 1 :(得分:0)

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