简体   繁体   中英

Closing AlertDialog.Builder in Android

In the following code, I tried to dismiss the AlertDialog box but to no avail. However, if I remove compareKeys() function, the dismiss will work. So how can I make it dismiss after calling the compareKeys() function?

public void promptAdministratorPassword() {
    AlertDialog.Builder alert = new AlertDialog.Builder(this);

    alert.setTitle("Alert!");
    alert.setMessage("Please enter your password: ");

    // Set an EditText view to get user input
    final EditText input = new EditText(this);
    alert.setView(input);

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            password = input.getText().toString();

            if (password.equals("password")) {
                try {
                    compareKeys();
                } catch (IOException | NoSuchAlgorithmException | InvalidKeySpecException e) {
                    e.printStackTrace();
                }
            }
            dialog.dismiss();
        }
    });

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            // Canceled.
        }
    });
    alert.show();
}

password = input.getText().toString() dialog.dismiss()之前调用dialog.dismiss() ,并在setNegativeButtonOnClickListener添加dialog.dismiss()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM