2014-10-19 107 views
2

因爲我想在對話框中使用FragmentManager,所以我使用onCreateView而不是onCreateDialogAndroid DialogFragment - 使用onCreateView並添加對話框按鈕到對話框可能?

如果使用這種方法,我可以設置對話框的標題,但是我找不到如何設置對話框按鈕。通常我會做這createDialog與AlertDialog ...

問:是否可以使用onCreateView方法,並使用對話框按鈕?或者只能這樣做,如果我手動添加按鈕到我的視圖資源?

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{ 
    View v = inflater.inflate(R.layout.dialog_exercise_info, null); 
    // I could include the dialog buttons in the view 
    // but can I somehow add buttons to the dialog directly? 
    return v; 
} 

@Override 
protected Dialog createDialog(final Bundle savedInstanceState, Activity activity) 
{ 
    Dialog dialog = new Dialog(getActivity(), getTheme()); 
    dialog.setTitle(R.string.title_create_exercise); 
    return dialog; 
} 

回答

1

是和否......如果您沒有使用AlertDialog,那麼您必須在視圖資源中手動創建對話框按鈕。通過說你必須在你的視圖資源中手動創建它們,我的意思是你必須把它放在xml佈局文件中,或者你必須在onCreateView()中的代碼中創建和添加它們,根據你的問題這是可以接受的。

用於在AlertDialog中直接設置按鈕(正面和負面)的便利方法不是DialogFragment的一部分。你可以創建你自己的便利方法,但在這種情況下,你要回去手動完成它們。

+0

好的,謝謝。任何其他缺點,如果我不使用AlertDialog?我會然後手動添加按鈕佈局,並相應地調用聽衆... – prom85 2014-10-20 07:06:46

+0

不是。我更喜歡使用我自己的自定義對話框片段,因爲我可以在其他地方重複使用它們。我遵循的技巧是設計Save和Cancel(正面和負面按鈕),具有與AlertDialog相同的透明背景 - 這可以保持設計與Android UI的一致性。 – ucsunil 2014-10-20 16:36:54

+0

我有一個完整的基於總線的對話框片段/片段對話框庫,以及...我只是想知道如果我可以做到這一點,而無需手動添加按鈕。 Thx雖然 – prom85 2014-10-20 19:12:51

相關問題