代码之家  ›  专栏  ›  技术社区  ›  Makarand

点击“正按钮”后警报对话框消失

  •  0
  • Makarand  · 技术社区  · 7 年前

    我正在创建自定义警报对话框。问题是-我没有在setPositiveButton()方法中调用dialogInterface.dispose();函数 但是我的对话在点击肯定的按钮后还是被取消了。所以,我不能用它做进一步的手术。

    为什么会发生这样的事情,我怎么能做出“积极”按钮来做这些事情呢?

    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();
    
    }
    
    0 回复  |  直到 7 年前
        1
  •  0
  •   Nick    7 年前

    简单的“做事情”你有一个评论说“在这里做工作”。

    // 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
    
         }
     });
    

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