在Android对话框后创建另一个对话框

时间:2016-12-15 09:45:24

标签: android dialog

在学习使用Android应用中的对话框时,我创建了这个对话框:

    dialogBuilder = new AlertDialog.Builder(this);
    dialogBuilder.setTitle("Title");

    dialogBuilder.setPositiveButton("Next", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

            Toast.makeText(getApplicationContext(), "Next dialog", Toast.LENGTH_LONG).show();

        }
    });

    dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getApplicationContext(), "Canceled", Toast.LENGTH_LONG).show();
        }
    });


    AlertDialog dialog = dialogBuilder.create();
    dialog.show();

如何在按下“下一步”后立即创建另一个对话框?我已阅读this帖子,但我不理解那里的答案。

5 个答案:

答案 0 :(得分:2)

在正面按钮的onclick中创建另一个对话框,如此

dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setTitle("Title");

dialogBuilder.setPositiveButton("Next", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {

        Toast.makeText(getApplicationContext(), "Next dialog", Toast.LENGTH_LONG).show();

    showDialog();


    }
});

dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(getApplicationContext(), "Canceled", Toast.LENGTH_LONG).show();
    }
});


AlertDialog dialog = dialogBuilder.create();
dialog.show();

和showdialog方法

public void showDialog(){

 dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setTitle("Title");

dialogBuilder.setPositiveButton("Next", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {

        Toast.makeText(getApplicationContext(), "Next dialog", Toast.LENGTH_LONG).show();

    }
});

dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(getApplicationContext(), "Canceled", Toast.LENGTH_LONG).show();
    }
});


AlertDialog dialog = dialogBuilder.create();
dialog.show();

}

答案 1 :(得分:1)

这就是我用于我的应用程序

Button more = (Button) findViewById(R.id.more);

more.setOnClickListener(new View.OnClickListener() {

    public void onClick(View view) {

        //Intent myIntent = new Intent(view.getContext(), agones.class);

        //startActivityForResult(myIntent, 0);


        AlertDialog alertDialog = new 

AlertDialog.Builder(<YourActivityName>this).create(); //Read Update

        alertDialog.setTitle("hi");

        alertDialog.setMessage("this is my app");


        alertDialog.setButton("Continue..", new 

DialogInterface.OnClickListener() {

           public void onClick(DialogInterface dialog, int which) {

              // here you can add functions
           }
        });

        alertDialog.show();  //<-- See This!
    }

});

答案 2 :(得分:1)

这是您的解决方案,您可以在“下一步”按下后创建另一个对话框。

<强> MainActivity.java

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        context);
                alertDialogBuilder.setTitle("First Dialog");
                alertDialogBuilder
                        .setMessage("Click yes to Open Second Dialog!")
                        .setCancelable(false)
                        .setPositiveButton("Next", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                //MainActivity.this.finish();

                                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                                        context);
                                alertDialogBuilder.setTitle("Your Title");
                                alertDialogBuilder
                                        .setMessage("Click yes to exit!")
                                        .setCancelable(false)
                                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog, int id) {
                                                //MainActivity.this.finish();
                                            }
                                        })
                                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog, int id) {
                                                // if this button is clicked, just close
                                                // the dialog box and do nothing
                                                dialog.cancel();
                                            }
                                        });

                                // create alert dialog
                                AlertDialog alertDialog = alertDialogBuilder.create();

                                // show it
                                alertDialog.show();

                            }
                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                // if this button is clicked, just close
                                // the dialog box and do nothing
                                dialog.cancel();
                            }
                        });

                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();
            }

答案 3 :(得分:1)

我的回答可能晚了,但可能对某些人有用

AlertDialog.Builder builder = new AlertDialog.Builder(this);
     builder.setMessage("Are you sure to make the changes?");
     builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
         @Override
         public void onClick(DialogInterface dialogInterface, int i) {
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
                 dialog.setMessage("This is the second dialog");
                 dialog.setNeutralButton("Okay", new DialogInterface.OnClickListener() {
                 @Override
                 public void onClick(DialogInterface dialogInterface, int i) {
                     Toast.makeText(this, "You clicked Okay", Toast.LENGTH_LONG).show();
                     }
                    });
          AlertDialog dial = dialog.create();
          dial.show();
         }
        });
      builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
         @Override
         public void onClick(DialogInterface dialogInterface, int i) {
              finish();
         }
        });
      AlertDialog dialog = builder.create();
      dialog.show();

答案 4 :(得分:0)

随时调用此方法。不需要一次又一次地编写代码。只需调用方法showConfirmAlertDialog(activity);

public static void showConfirmAlertDialog(Activity mActivity) {

            AlertDialog.Builder adb = new AlertDialog.Builder(mActivity).setTitle("title").setMessage("message");
            adb.setPositiveButton("Next", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) { // Confirmed
                    dialog.cancel();
                }
            });
            adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) { // Rejected 
                    dialog.cancel();
                }
            });
            adb.setCancelable(false);
            adb.show();
        }