android - 单击“积极按钮”后警报对话框消失
问题描述
我正在创建自定义警报对话框。问题是 - 我没有调用 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();
}
解决方案
只需在您有评论说“在这里工作”的地方“做事”。
// 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
}
});
在该块中运行您需要的任何内容;执行后,对话框被关闭。
推荐阅读
- r - 如何使用具有三个水平的 x 变量和比例不相关的二分 y 变量创建条形图
- r - 从日期开始的月、日、小时、分钟、秒的子集
- sqlite - 如何根据人数动态配置时段
- mongoose - 在 Mongoose 中使用变量来定位对象路径
- node.js - 如何通过 Firebase Cloud Messaging 从云功能发送通知?
- excel - 刷新功率支点-功率查询
- docusignapi - DocuSign - 从 Java SDK listStatusChanges() 获取签名文档正文
- python - 无法使用长度约束算法实现最大子数组
- r - 在 r 中分配和获取数据框变量
- kotlin - 在 Gradle 中从 Kotlin 调用 PCEnhancerTask