android - 如何关闭 AlertDialog.Builder?

标签 android memory dialog alert dismiss

在下面的代码中,如何关闭警报框?我不想造成内存泄漏。我在alertDialog上尝试了.dismiss(),但是没有用... 谢谢

// User pressed the stop button
public void StopMsg_button_action(View view){
    final EditText password_input = new EditText(this); // create an text input field
    password_input.setHint("Enter Password"); // put a hint in it
    password_input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); // change it to password type

    AlertDialog.Builder alertDialog = new Builder(this); // create an alert box
    alertDialog.setTitle("Enter Password"); // set the title
    alertDialog.setView(password_input);  // insert the password text field in the alert box
    alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { // define the 'OK' button
        public void onClick(DialogInterface dialog, int which) {
             String entered_password = password_input.getText().toString();
             if (entered_password.equals(my_password)) {
                locManager.removeUpdates(locListener); // stop listening for GPS coordinates
                startActivity(new Intent(Emergency_1Activity.this,Main_MenuActivity.class)); // go to main menu
             } else {
                 alert("Incorrect Password");
             }
        } 
    });
    alertDialog.setNeutralButton("Cancel", new DialogInterface.OnClickListener() { // define the 'Cancel' button
        public void onClick(DialogInterface dialog, int which) {

        } 
    });
    alertDialog.show(); // show the alert box
}

最佳答案

dismiss() 有什么不好的地方?

您应该可以使用Dialog.dismiss() , 或 Dialog.cancel()

alertDialog.setNeutralButton("Cancel", new DialogInterface.OnClickListener() { // define the 'Cancel' button
    public void onClick(DialogInterface dialog, int which) {
        //Either of the following two lines should work.
        dialog.cancel();
        //dialog.dismiss();
    } 
});

关于android - 如何关闭 AlertDialog.Builder?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11285235/

相关文章:

android - firebase (fcm) 说 401 未经授权

android - 在 java 中对数组列表使用 Getter 和 Setter

c++ - 结构的大小超过预期

powershell - PowerShell 中的处置和清理 - 手动还是自动?

java - 如何在java中创建对话框窗口?

cocoa - NSRunAlertPanel 显示在 "active window"后面

Android 应用程序可在模拟器上运行但不能在真实设备上运行

android - android 中的 ActionBar API

c++ - 错误[Lp001]内存不足,但我不应该这样做

android - 显示对话框时出现 BadTokenException