0

假设我有ArrayList4 个字符串值。我想AlertDialogs用这 4 个字符串值显示 4 作为消息。所以我的疑问是,我只想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();
    }
4

3 回答 3

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

于 2016-06-28T06:30:04.767 回答
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);

这可能会帮助你

于 2016-06-28T07:03:19.333 回答
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();

        }

    }

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

于 2016-06-28T06:29:12.837 回答