单击“正按钮”后关闭警报对话框

时间:2018-12-14 06:35:09

标签: android-custom-view android-alertdialog dismiss

我正在创建自定义警报对话框。问题是-我没有调用dialogInterface.dismiss();在setPositiveButton()方法中 单击确定按钮后,我的对话框仍然被关闭。因此,我无法对其进行进一步的操作。

为什么会发生这种情况,如何使“正”按钮起作用?

public void changePassword() {
    LayoutInflater layoutInflater = getLayoutInflater();

    View alertChangePassLayout = layoutInflater.inflate(R.layout.change_password_alertdialog_layout, null);

    final EditText oldPasswordET = alertChangePassLayout.findViewById(R.id.old_password_ET_alert_dialog);
    final EditText newPasswordET = alertChangePassLayout.findViewById(R.id.new_password_ET_alert_dialog);
    final EditText confirmNewPasswordET = alertChangePassLayout.findViewById(R.id.confirm_new_password_ET_alert_dialog);
    Button changePasswordButton = alertChangePassLayout.findViewById(R.id.change_password_BB_alert_dialog);



    AlertDialog.Builder alertBuilder = new AlertDialog.Builder(dialog_activity.this);

    alertBuilder.setTitle("Change Password");

    // Set view of xml inside alert dialog
    alertBuilder.setView(alertChangePassLayout);


    // disable dismiss dialog when clicked outside the dialog
    alertBuilder.setCancelable(false);

    // Set positive button
     alertBuilder.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
         @Override
         public void onClick(DialogInterface dialogInterface, int i) {

             Toast.makeText(dialog_activity.this, "Submit button..", Toast.LENGTH_LONG).show();
             // do work here

         }
     });

    // Set negative button
    alertBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            Toast.makeText(dialog_activity.this, "Cancel button..", Toast.LENGTH_LONG).show();
            dialogInterface.dismiss();
        }
    });

    AlertDialog alertDialog = alertBuilder.create();
    alertDialog.show();

}

2 个答案:

答案 0 :(得分:0)

只需“做些事情”,然后在注释中说“在这里工作”即可。

// Set positive button
 alertBuilder.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialogInterface, int i) {

         Toast.makeText(dialog_activity.this, "Submit button..", Toast.LENGTH_LONG).show();
         // do work here

     }
 });

运行该块中所需的任何内容;执行后,对话框将关闭。

答案 1 :(得分:0)

positiveButton总是在执行侦听器主体后关闭对话框。