2014-10-22 60 views
0

我想顯示一個對話框。當在對話框外單擊時,我想要對話框來取消。但在解除對話之前,我想檢查一個條件,如果正確,則關閉對話框,否則如果值不正確,請不要解除它。如何在關閉對話框之前檢查值?

任何人都可以幫助我嗎?謝謝。

+2

請分享相關代碼 – Parker 2014-10-22 04:40:45

+0

警報對話框?請具體說明並顯示代碼。 – 2014-10-22 04:41:58

+1

確實有辦法做到這一點,但我強烈建議您在對話框中使用負面和正面按鈕,並且setCancelable(false)(僅當您的應用程序無法在用戶輸入值的情況下無法繼續進行)。 – Varundroid 2014-10-22 04:44:36

回答

1

保留setCancelabe(false);,直到EditText中的值不正確。使用TextWatcher檢查數值,只要您的數值正確,然後設置setCancelabe(true);

0

您可以使用TextWatcher輸入時檢查數值。或者將自定義onClickListener設置爲正面按鈕。

AlertDialog.Builder builder = new AlertDialog.Builder(); 

// build dialog 

AlertDialog dialog = builder.create(); 
dialog.setOnShowListener(new DialogInterface.OnShowListener(){ 

    @Override 
    public void onShow(DialogInterface dialogInterface){ 


     ((AlertDialog)dialogInterface).getButton(AlertDialog.BUTTON_POSITIVE) 
             .setOnClickListener(// check value here) 


    } 
} 

dialog.show(); 

記得在準備解僱它時調用dialogInterface.dismiss()