如何关闭 AlertDialog.Builder?

时间:2023-04-23
本文介绍了如何关闭 AlertDialog.Builder?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

In the following code below, how do I dismiss the alert box? I don't want to cause a memory leak. I tried the .dismiss() on alertDialog, but that didn't work... Thanks

// 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() 有什么问题?

What didn't work about dismiss()?

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

You should be able to use either Dialog.dismiss(), or 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();
    } 
});

这篇关于如何关闭 AlertDialog.Builder?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:从 Android 的视图中打开带有文本输入的对话框 下一篇:如何知道一个对话框是否在 Android 中被关闭?

相关文章