首页 > 解决方案 > 单击“积极按钮”后警报对话框消失

问题描述

我正在创建自定义警报对话框。问题是 - 我没有调用 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();

}

标签: androidandroid-custom-viewandroid-alertdialogdismiss

解决方案


只需在您有评论说“在这里工作”的地方“做事”。

// 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

     }
 });

在该块中运行您需要的任何内容;执行后,对话框被关闭。


推荐阅读