2011-01-24 29 views
4

對於自定義AlertDialog,我可以覆蓋肯定按鈕以不關閉對話框嗎?相反,我想運行一些編輯檢查,並保持對話框打開,如果我的檢查失敗。AlertDialog - 我如何在用戶點擊「確定」時運行檢查

protected Dialog onCreateDialog(int id) { 
    Dialog alertDialog = null; 
    builder = new AlertDialog.Builder(this); 
    switch(id) { 
    case LOGIN_USERID_BLANK: 
     builder.setMessage((String)getString(R.string.username_not_blank)); 
     builder.setPositiveButton((String)getString(R.string.ok), new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 
     // Can I do something here so that the dialog does not close? 
} 
}); 

break;

回答

1

好的,這裏只是一個如何實現的想法。

AlertDialog.BuildersetView(View v)方法。所以可以添加一個自定義的LinearLayout(從Dialog構建之前的資源中填充)和按鈕。然後只需在按鈕上設置通常的android.view.View.OnClickListener(s)即可。在這種情況下,根本不要對AlertDialog.Builder按鈕使用「內置/本機」。

+0

雖然這個建議可能有效,但我不認爲這是AlertDialog的預期用途。在那個時候,我不確定AlertDialog比通用定製對話提供了什麼好處,並且可能會混淆未來的代碼維護者。 –

+0

@glenviewjeff:我同意這是一種黑客行爲,它確實需要在代碼中給出一個很好的評論。另外請注意,沒有其他人提出任何更好的建議,而我的建議實際上完成了要求。還有一點你可能缺少的是,「內置」對話框按鈕在偵聽器運行後自動關閉對話框,因此它們不適合所請求的行爲。你有沒有實現過一個通用的定製對話框?如果是的話 - 你能否分享一下代碼(我從來沒有這樣做過,所以看到一個樣本很有趣)。 –

+0

我推遲現在驗證數據,直到我可以用自定義對話框正確地完成它。我希望他們在未來的Android API的內置對話框中解決這個缺點。當我解決這個問題時,我會嘗試發佈一些內容。 –

0

下面是我做到的。從技術上講,它在技術上並沒有保持對話框打開,它暫時關閉並重新打開它,但最終結果是相同的。

class MyAlertDialog implements OnDismissListener, OnCancelListener { 
    final private EditText editText; 
    final private AlertDialog alertDialog; 
    final private EventManager eventManager; 
    final private CategorySelector categorySelector; 

    private Boolean canceled; 

    MyAlertDialog(Context context) { 
     editText = new EditText(context); 
     alertDialog = buildAlertDialog(context); 
     alertDialog.setOnDismissListener(this); 
     alertDialog.setOnCancelListener(this); 
     show(); 
    } 

    private AlertDialog buildAlertDialog(Context context) { 
     return new AlertDialog.Builder(context) 
     .setTitle(context.getString(R.string.enter_name)) 
     .setMessage(context.getString(R.string.enter_name)) 
     .setView(editText) 
     .setNeutralButton(context.getString(R.string.save_text), null) 
     .setNegativeButton(context.getString(R.string.cancel_text), null).create(); 
    } 

    public void show() { 
     canceled = false; 
     alertDialog.show(); 
    } 

    @Override 
    public void onDismiss(DialogInterface dialog) { 
     if(!canceled) { 
      final String name = editText.getText().toString(); 
      if(name.equals("")) { 
       editText.setError("Please enter a non-empty name"); 
       show(); 
      } else { 
       doWhateverYouWantHere(name); 
      } 
     } 
    } 

    @Override 
    public void onCancel(DialogInterface dialog) { 
     canceled = true; 
    } 
} 
6

這裏有一個解決辦法,直到谷歌改變了對話框API:

LayoutInflater factory = LayoutInflater.from(this); 
final View view = factory.inflate(R.layout.yoMamma, null); 
final Dialog dlg = new AlertDialog.Builder(this) 
    .setTitle(R.string.yoTitle) 
    .setView(view) 
    .setPositiveButton(R.string.dlgOK, new DialogInterface.OnClickListener() { 
     @Override public void onClick(DialogInterface dialog, int which) { 
     // This won't be called. 
     }}) 
    .create(); 
dlg.show(); 
View button = ((AlertDialog)dlg).getButton(DialogInterface.BUTTON_POSITIVE); 
button.setOnClickListener(new View.OnClickListener() { 
    @Override public void onClick(View v) { 
    // This WILL be called. 
    // Remove the following if you don't want the dialog to dismiss 
    dlg.dismiss(); 
    }}); 
+0

感謝它的工作。 –

+0

你可以像這樣簡化第一個setPositiveButton()調用:「.setPositiveButton(R.string.dlgOk,null)」 – Mike

0

我面臨着同樣的問題,其中我是不是能夠從越來越駁回停止對話,甚至當我想在對話框中收集到的輸入有驗證問題。爲了解決這個問題,我在對話框的自定義視圖中添加了按鈕,以便更好地控制。

如果您使用dialogBuilder'ssetNeutralButtonsetPositiveButtonsetNegativeButton,似乎並沒有乾淨的方法來停止對話框被取消。

相關問題