多个警报对话框

时间:2018-02-19 11:35:32

标签: android android-alertdialog

我创建了两个警报对话框。我希望只有在第一个警报对话框中单击是时才会出现第二个警报对话框。但两者都在显示。

警报对话代码

    DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which){
                case DialogInterface.BUTTON_POSITIVE:
                    //Yes button clicked
                    //Would like the second Alert Dialog to Display Now
                    YesOrNo = "Yes";
                    break;

                case DialogInterface.BUTTON_NEGATIVE:
                    //No button clicked
                    YesOrNo = "No";
                    GIVE.setBackgroundColor(Color.RED);
                    break;
            }
        }
    };

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Are you Giving or Teaching for Free?").setPositiveButton("Yes", dialogClickListener)
            .setNegativeButton("No", dialogClickListener).show();

if ( YesOrNo == "Yes" ) {
    DialogInterface.OnClickListener dialogClickListener1 = new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch(which) {
                case DialogInterface.BUTTON_POSITIVE:
                    break;

                case DialogInterface.BUTTON_NEGATIVE:
                    PROBLEM.setBackgroundColor(Color.RED);
            }
        }
    };
    AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
    builder1.setMessage("Are you Really Happy With Your Current Giving or Teaching For Free?").setPositiveButton("Yes", dialogClickListener1)
            .setNegativeButton("No", dialogClickListener1).show();
}

由于

2 个答案:

答案 0 :(得分:0)

当您使用dialog打开第二个dialog时,先点击dialog.dismiss();,当您点击“是”时,它将dismiss首先dialog然后开第二个dialog

参考:https://stackoverflow.com/a/14853382/8448886

完整代码:

AlertDialog.Builder builder;

     DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which){
                case DialogInterface.BUTTON_POSITIVE:
                    //Yes button clicked
                    //Would like the second Alert Dialog to Display Now
                    YesOrNo = "Yes";
                    //dialog.dismiss(); 
                    dialog.cancel();
                    break;

                case DialogInterface.BUTTON_NEGATIVE:
                    //No button clicked
                    YesOrNo = "No";
                    GIVE.setBackgroundColor(Color.RED);
                    break;
            }
        }
    };

    builder = new AlertDialog.Builder(this);
    builder.setMessage("Are you Giving or Teaching for Free?").setPositiveButton("Yes", dialogClickListener)
            .setNegativeButton("No", dialogClickListener).show();

答案 1 :(得分:0)

尝试这个,

 DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which){
            case DialogInterface.BUTTON_POSITIVE:
                //Yes button clicked
                //Would like the second Alert Dialog to Display Now
                DialogInterface.OnClickListener dialogClickListener1 = new 
                DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                      switch(which) {
                               case DialogInterface.BUTTON_POSITIVE:
                               break;

                               case DialogInterface.BUTTON_NEGATIVE:
                                  PROBLEM.setBackgroundColor(Color.RED);
                     }
               }
      };
     AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
     builder1.setMessage("Are you Really Happy With Your Current Giving or 
     Teaching For Free?").setPositiveButton("Yes", dialogClickListener1)
     .setNegativeButton("No", dialogClickListener1).show();                    
     break;

            case DialogInterface.BUTTON_NEGATIVE:
                //No button clicked
                YesOrNo = "No";
                GIVE.setBackgroundColor(Color.RED);
                break;
        }
    }
};

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you Giving or Teaching for Free?").setPositiveButton("Yes", dialogClickListener)
        .setNegativeButton("No", dialogClickListener).show();