AlertDialog inside alertdialog android

后端 未结 4 2104
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-18 07:25

I am trying to add an alertdialog within an alertdialog.But not able to see the second alertdialog..please help me here is my code shown

AlertDialog alertDia         


        
相关标签:
4条回答
  • 2020-12-18 07:56

    AlertDialogs aren't supposed to let another AlertDialog to be open. If it is really what you want, then change your main AlertDialog to Dialog. This way you can manually add the buttons and functionality you require for manage the secondary AlertDialog

    0 讨论(0)
  • 2020-12-18 08:01

    I found the way might be it will be helpful for someone so thats why i am sharing:

    //2nd Alert Dialog
                    AlertDialog.Builder alertDialogBuilderSuccess = new AlertDialog.Builder(
                            context);
                    alertDialogBuilderSuccess.setTitle("TopUp Success");
                    // set dialog message
                    alertDialogBuilderSuccess
                            .setMessage(
                                    "You voucher is printed, please go to the cashier.")
                            .setCancelable(false)
                            .setIcon(R.drawable.ic_launcher2)
                            .setPositiveButton("Confirm",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(
                                                DialogInterface dialog, int id) {
    
                                            finish();
                                        }
                                    });
    
                    // create alert dialog
                    final AlertDialog alertDialogSuccess = alertDialogBuilderSuccess.create();
    
    
    
    
    
    
    
    
                    //////////////////////////////////
                    //1st Alert
                    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                            context);
                    alertDialogBuilder.setTitle("TopUp Request");
                    // set dialog message
                    alertDialogBuilder
                            .setMessage(
                                    "Please confirm: " + vendor_name + ", "
                                            + tvLoadAmount.getText())
                            .setCancelable(false)
                            .setIcon(R.drawable.ic_launcher2)
                            .setPositiveButton("Confirm",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(
                                                DialogInterface dialog, int id) {
    
                                            //calling the second alert when it user press the confirm button
                                            alertDialogSuccess.show();
                                        }
                                    })
                            .setNegativeButton("Cancel",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(
                                                DialogInterface dialog, int id) {
                                            dialog.cancel();
                                        }
                                    });
    
                    // create alert dialog
                    AlertDialog alertDialog = alertDialogBuilder.create();
    
                    // show it
                    alertDialog.show();
    
    0 讨论(0)
  • 2020-12-18 08:06
    import android.widget.TextView;
    import android.widget.Button;
    import android.view.View;
    import android.content.DialogInterface;
    
    public class MainActivity extends AppCompatActivity {
        Button click;
    
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            click=(Button)findViewById(R.id.btnId);
    
            click.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    txtSubmit();
                }
            });
        }
    
    
    protected void txtSubmit(){
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("This is alert title");
            builder.setMessage("This is message for Alert dialog");
            builder.setPositiveButton("Yup", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
                    builder1.setTitle("This is alert title inside");
                    builder1.setMessage("This is message for Alert dialog inside");
                    builder1.show();
                }
            });
            builder.show();
        }
    }
    

    The txtSubmit function calling from the onClick event listener. Also for the second alert dialog need to open I have to pass

    AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
    

    Here, we have to bind the this event using className

    When we click on the yup button in the first dialog,it shows the second dialog that insides in the first one

    0 讨论(0)
  • 2020-12-18 08:08

    It is gonna be a late answer but you can create an AlertDialog inside onClickListener just like this:

    public void onClick(DialogInterface dialog, int which) {
        if (options[which] == "Manage") {
          //Do smtg
        } else {
            dialog.dismiss();
            final AlertDialog alert;
    
            AlertDialog.Builder dialog2 = new AlertDialog.Builder(CategoryPage.this);
            alert = dialog2.create();
            alert.setTitle("Delete " + title + "?");
            alert.setMessage("Are you sure you want to delete this category?");
    
            alert.setButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    Toast.makeText(CategoryPage.this, "YESS", Toast.LENGTH_LONG).show();
                }
            });
    
            alert.setButton2("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    alert.dismiss();
                }
            });
    
            alert.show();
        }
    }
    
    0 讨论(0)
提交回复
热议问题