AlertDialog在另一个alertdialog里面

时间:2014-09-25 07:37:00

标签: android alertdialog

我尝试使用3个选项创建一个alertdialog,其中一个选项会打开另一个带有editext的alertdialog。

问题是,一旦第二个alertdialog被打开,我无法设法完成或结束它之前的那个,它会隐藏在第二个对话框下面,当我开始输入editext时再次弹出。

 void generateAlertDialog(long timeSpent) {

        AlertDialog.Builder builder = new AlertDialog.Builder(
                GActivity.this);


        builder.setMessage(
                "The time stayed in the game is " + timeSpent + "s")
                .setTitle("You Lose")
                .setPositiveButton("Reset Game",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {

                                finish();
                                Intent intent = new Intent(GActivity.this,   GActivity.class);
                                Bundle setting = configureGame(RWIDTH, BSIZE, Speed);
                                intent.putExtra("setting", setting);
                                startActivity(intent);

                            }
                        })
                .setNeutralButton("Score List",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                // User clicked score button
                                AlertDialog.Builder builder = new AlertDialog.Builder(
                                        GActivity.this);
                                builder.setMessage(
                                        "Please input the player's name");
                                        setTitle("Score");
                                        final EditText input = new EditText(GActivity.this);
                                        builder.setView(input)
                                        .setPositiveButton("Reset Game",
                                                    new DialogInterface.OnClickListener() {
                                                    public void onClick(DialogInterface dialog,
                                                            int id) {

                                                                }

                                                            })
                                        .setNegativeButton("Exit",
                                                new DialogInterface.OnClickListener() {
                                                public void onClick(DialogInterface dialog,
                                                        int id) {

                                                                }
                                                            });
                                // 3. Get the AlertDialogfrom create()
                                AlertDialog dialogsnd = builder.create();

                                // 4. Show dialog
                                dialogsnd.show();
                            }

                })
                .setNegativeButton("Exit",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                finish();
                                // User cancelled the dialog
                            }
                        });

        // 3. Get the AlertDialogfrom create()
        AlertDialog dialog = builder.create();

        // 4. Show dialog
        dialog.show();
    }

3 个答案:

答案 0 :(得分:1)

Just u need to put dialog.dismiss(); where u want to close alert box(i.e previous one) as below


void generateAlertDialog(long timeSpent) {

        AlertDialog.Builder builder = new AlertDialog.Builder(
                GActivity.this);


        builder.setMessage(
                "The time stayed in the game is " + timeSpent + "s")
                .setTitle("You Lose")
                .setPositiveButton("Reset Game",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {

                                finish();
                                Intent intent = new Intent(GActivity.this,   GActivity.class);
                                Bundle setting = configureGame(RWIDTH, BSIZE, Speed);
                                intent.putExtra("setting", setting);
                                startActivity(intent);

                            }
                        })
                .setNeutralButton("Score List",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {


//------------------> put dialog.dismiss();

dialog.dismiss();
                                // User clicked score button
                                AlertDialog.Builder builder = new AlertDialog.Builder(
                                        GActivity.this);
                                builder.setMessage(
                                        "Please input the player's name");
                                        setTitle("Score");
                                        final EditText input = new EditText(GActivity.this);
                                        builder.setView(input)
                                        .setPositiveButton("Reset Game",
                                                    new DialogInterface.OnClickListener() {
                                                    public void onClick(DialogInterface dialog,
                                                            int id) {

                                                                }

                                                            })
                                        .setNegativeButton("Exit",
                                                new DialogInterface.OnClickListener() {
                                                public void onClick(DialogInterface dialog,
                                                        int id) {

                                                                }
                                                            });
                                // 3. Get the AlertDialogfrom create()
                                AlertDialog dialogsnd = builder.create();

                                // 4. Show dialog
                                dialogsnd.show();
                            }

                })
                .setNegativeButton("Exit",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                finish();
                                // User cancelled the dialog
                            }
                        });

        // 3. Get the AlertDialogfrom create()
        AlertDialog dialog = builder.create();

        // 4. Show dialog
        dialog.show();
    }

答案 1 :(得分:1)

如果为true,您可以创建一个调用上一个警报的布尔值。将第二个警报放入新方法中。从第一个警报调用第二个警报时,请确保将第一个警报的布尔值设置为false,关闭对话框,然后调用第二个警报。这样第一个对话框就不会再出现了。

答案 2 :(得分:0)

在创建新的Dialog之前调用dialog.dismiss();