2016-12-11 40 views
0

在下面的代碼中,我試圖解僱AlertDialog框但無濟於事。但是,如果我刪除了compareKeys()函數,解僱將起作用。那麼在調用compareKeys()函數之後,我該如何解僱呢?Android中關閉AlertDialog.Builder

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

回答

0

呼叫dialog.dismiss()password = input.getText().toString()之前,並添加dialog.dismiss()setNegativeButtonOnClickListener太裏面。

+0

謝謝,真的很感謝你的幫助! – hahas92

+0

解僱按鈕,如果它不符合密碼,我如何解僱它,當它匹配密碼?到目前爲止,當我輸入錯誤密碼時,它將被解僱。但是,當我輸入正確的密碼時,它不會解僱。 – hahas92