一个接一个地显示多个AlertDialog

时间:2016-06-28 06:18:14

标签: android android-alertdialog multiple-alert-dialogs

假设我有4个字符串值的ArrayList。我希望将这4个字符串值显示为4 AlertDialogs作为消息。所以我的疑问是,我想在一个人被解雇后才显示一个AlertDialog。因此,当单击确定按钮/取消按钮时,这些应该背靠背显示。任何帮助将不胜感激。

ArrayList<String> messages = new ArrayList<String>();
    messages.add("One");
    messages.add("Two");
    messages.add("Three");
    messages.add("Four");
    for(int i=0; i<messages.size(); i++)
    {
        AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
        builder1.setMessage(messages.get(i));
        builder1.setCancelable(true);

        builder1.setPositiveButton(
            "Yes",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

        builder1.setNegativeButton(
            "No",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

        AlertDialog alert11 = builder1.create();
        alert11.show();
    }

3 个答案:

答案 0 :(得分:1)

首先创建一个接受计数和单个消息的函数,并创建一个全局变量int count = 0

private void buildAlertDialog(int length, String message) {
  if (count != lenght) {
  AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
    builder1.setMessage(message);
    builder1.setCancelable(true);

    builder1.setPositiveButton(
        "Yes",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
                count++;
                buildAlertDialog(messages.size(), message.get(lenght-count+1));
            }
        });

    builder1.setNegativeButton(
        "No",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
                count++;
                buildAlertDialog(messages.size(), message.get(lenght-count+1));
            }
        });

    AlertDialog alert11 = builder1.create();
    alert11.show();
   }

  }

请尝试让我知道,然后拨打您要显示第一个dialog

的功能

答案 1 :(得分:0)

你可以在递归中完成。

    public class MessagesHandler {
        private ArrayList<String> messages = new ArrayList<String>();

        public MessagesHandler() {
            messages.add("One");
            messages.add("Two");
            messages.add("Three");
            messages.add("Four");

            displayMessage(0);
        }

        public void displayMessage(int index) {
            if (index < 0 || index >= messages.size()) {
                return;
            }

            AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
            builder1.setMessage(messages.get(index));
            builder1.setCancelable(true);

            builder1.setPositiveButton("Yes",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                    displayMessage(++index);
                }
            });

            builder1.setNegativeButton("No",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                    displayMessage(++index);
                }
            });

            AlertDialog alert11 = builder1.create();
            alert11.show();

        }

    }

我现在正在上班,所以我无法测试这个解决方案,但你可以尝试:)

答案 2 :(得分:0)

试试这个,

 int count = 0;

 public void myAlert(int index)
 {

 AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
    builder1.setMessage(messages.get(count));
    builder1.setCancelable(true);

    builder1.setPositiveButton(
        "Yes",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
                if(count != messages.size())
                {
                    count++;
                    myAlert(count);
                }
            }
        });

    builder1.setNegativeButton(
        "No",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });

    AlertDialog alert11 = builder1.create();
    alert11.show();

}

呼叫

myAlert(count);

这可能有助于你