简体   繁体   English

Alertdialog在while语句中弹出

[英]Alertdialog pop inside a while statement

I have an alert box where a user can enter a persons name presses Ok and the box comes up again if there is another persons name to be entered. 我有一个警报框,用户可以在其中输入人员姓名,然后按确定,如果要输入其他人员姓名,则再次出现该框。 The code below does work, but if there is a high number of people to be added (like 50) my tablet crashes and resets. 以下代码可以正常工作,但是如果要添加的人数过多(例如50位),我的平板电脑将崩溃并重置。 I'm pretty sure this happens because there are 50 dialog boxes drawn on top of each other and my tablet runs out of memory and shuts down. 我很确定会发生这种情况,因为有50个对话框彼此重叠,并且我的平板电脑用尽了内存并关闭。

How can I get it to wait for the user to input the name and then run again? 我如何才能等待用户输入名称然后再次运行?

        while(i < number)
        {
            final AlertDialog.Builder alert = new AlertDialog.Builder(this);
            final EditText input = new EditText(this);
            alert.setMessage("Enter persons name (" + (number - i) + " names left to enter)");
            alert.setView(input);
            alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() 
            {
                public void onClick(DialogInterface dialog, int whichButton) 
                {
                    String value = input.getText().toString().trim();
                    setUp(value);
                }
            });
            alert.show();
            i++;
        }

Yes, you are showing all alerts at the same time. 是的,您正在同时显示所有警报。

Pseudo code to avoid this problem: 伪代码可避免此问题:

function showAlerts(Integer i) {
    if (i < number) {
        // Build the alert and display it
        alert.setPositiveButton(..., new OnClickListener() {
            setup(...);
            showAlerts(i+1);
        }
    }
}

showAlerts(0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM