简体   繁体   English

AlertDialog里面的AlertDialog

[英]AlertDialog inside AlertDialog

I am trying to make a AlertDialog inside AlertDialog , but when I run the code , the secound AlertDialog didnt appear 我试图在AlertDialog中创建一个AlertDialog,但是当我运行代码时,secound AlertDialog没有出现

This is my code , I want to make it like if the user click cancel in the first AlertDialog , the second AlertDialog will be appear , user can type his name and both the score and the name will pass to ScoreActivity using intern . 这是我的代码,我希望如果用户在第一个AlertDialog中单击取消,第二个AlertDialog将出现,用户可以输入他的名字,分数和名称将使用实习生传递给ScoreActivity。

void generateAlertDialog(final long timeSpent) {
        // 1. Instantiate an AlertDialog.Builder with its constructor
        AlertDialog.Builder builder = new AlertDialog.Builder(
                GameActivity.this);
        // 2. Chain together various setter methods to set the dialog
        // characteristics
        builder.setMessage(
                "The time stayed in the game is " + timeSpent + "s")
                .setTitle("You Lose!!")
                .setPositiveButton("Replay",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                                int id) {
                                // User clicked OK button
                                finish();
                                Intent intent = new Intent(getApplicationContext(), GameActivity.class);
                                startActivity(intent);
                            }
                        })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                                int id) {
                                AlertDialog.Builder builder2 = new AlertDialog.Builder(
                                        GameActivity.this);
                                builder2.setMessage(
                                        "Please input you name")
                                        .setTitle("Score");
                                final EditText input = new EditText(GameActivity.this);
                                input.setInputType(InputType.TYPE_CLASS_TEXT);
                                builder2.setView(input);
                                builder2.setPositiveButton("Show Score LIst",
                                        new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog,
                                                                int id) {
                                                finish();
                                                Intent intent = new Intent(getApplicationContext(), ScoreActivity.class);
                                                m_Text = input.getText().toString();
                                                intent.putExtra("name", m_Text);
                                                intent.putExtra("result", timeSpent);
                                                startActivity(intent);

                                            }
                                        });
                                builder2.setNegativeButton("Cancel",
                                        new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog,
                                                                int id) {
                                                finish();
                                                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                                                startActivity(intent);

                                            }
                                        });

                                // User cancelled the dialog

                            }
                        });

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

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

You never call builder2.create() or the show() method of whatever builder2.create() returns. 您永远不会调用builder2.create()或任何builder2.create()返回的show()方法。 You need to do both of those things right where you have the comment "// User cancelled the dialog". 您需要在注释“//用户取消对话框”的位置执行这两项操作。

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

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