java Android - 两个对话框,防止第一个对话框在第二个对话框退出后关闭

java Android - two dialogs, prevent first dialog from closing after second one exits

最初在我的应用程序中我创建了一个 AlertDialog,它有三个按钮,其中中间的按钮打开另一个 AlertDialog。问题是当按下按钮后第二个 AlertDialog 关闭时,第一个也随之关闭。我认为在我按下第二个 AlertDialog 上的按钮后 AlertDialogs 都关闭了。

我想要的是第一个 AlertDialog 打开另一个有自己按钮的 AlertDialog,当第二个 AlertDialog 按下按钮时,它只会自行关闭并返回到第一个。有什么办法可以实现吗?

这是用于打开 AlertDialog 按钮的代码:

final ImageButton fabgroup = (ImageButton) findViewById(R.id.groupButton);

这是一个按钮的代码,它打开一个 AlertDialog,其中包含另一个按钮,该按钮使用自身的中间按钮(创建按钮)打开另一个 AlertDialog,但当按钮打开时关闭它们按下第二个按钮(是或否按钮,这不是我想要的,因为我只希望第二个按钮自行关闭并返回到第一个 AlertDialog,是的,这在理论上听起来很混乱,所以如果需要,我可以尝试澄清):

    fabgroup.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final AlertDialog.Builder helpBuilder = new AlertDialog.Builder(CreateNote.this);
            helpBuilder.setTitle("Select a group");
            helpBuilder.setMessage("Add to group?");

            final TextView input = new TextView(mainactiv.this);
            input.setSingleLine();
            input.setText("");
            helpBuilder.setView(input);

            helpBuilder.setNegativeButton("Yes",
                    new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {
                            // Do nothing but close the dialog
                            Toast.makeText(CreateNote.this, "Page has been added to group", Toast.LENGTH_SHORT).show();
                        }
                    });


            helpBuilder.setNeutralButton("Create", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {


                    //open another alertbox
                    AlertDialog.Builder helpBuilder2 = new AlertDialog.Builder(CreateNote.this);
                    helpBuilder2.setTitle("Assign a new group");
                    helpBuilder2.setMessage("Create group?");

                    final EditText input = new EditText(CreateNote.this);
                    input.setSingleLine();
                    input.setText("");
                    helpBuilder2.setView(input);

                    helpBuilder2.setNegativeButton("Yes",
                            new DialogInterface.OnClickListener() {

                                public void onClick(DialogInterface dialog, int which) {
                                    // Create Group
                                    Toast.makeText(CreateNote.this, "Group has been created", Toast.LENGTH_SHORT).show();
                                }
                            });

                    helpBuilder2.setPositiveButton("No", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // Do nothing
                        }
                    });

                    // Remember, create doesn't show the dialog
                    AlertDialog helpDialog2 = helpBuilder2.create();
                    helpDialog2.show();

                }
            });

            helpBuilder.setPositiveButton("No", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // Do nothing
                }
            });

            // Remember, create doesn't show the dialog
            AlertDialog helpDialog = helpBuilder.create();
            helpDialog.show();
        }
    });

将不胜感激。

您可以将 activity 显示为对话框。把这个放在你的清单文件中。

<activity android:theme="@android:style/Theme.Dialog" android:excludeFromRecents="true"/>

来自这个答案:Android Activity as a dialog

我最终设法解决了这个问题,方法是创建两个单独的函数来生成每个对话框,当一个对话框关闭时,它会调用该函数来创建另一个对话框,有点像回收(或者可能更接近循环函数)。虽然,我不完全确定这对性能有多大影响,但它似乎可以完成这项工作,而我正在测试的内容没有任何问题。如果有人想插话这怎么可能成为一个问题,那么我很乐意听取其他人对以这种方式使用警告对话框的负面影响的看法。