2013-01-17 14 views
0

當我使用警告對話框中,我可以通過添加一個「取消」按鈕:實現我自己的片段作爲DialogFragment

AlertDialog.Builder builder = new Builder(getActivity()); 
builder.setTitle("Students").setNegativeButton("Cancel",new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog,int id) { 
    // if this button is clicked, just close 
    // the dialog box and do nothing 
    dialog.cancel(); 
    } 
});; 

現在我實現我自己的片段。我怎樣才能設置相同的取消按鈕?

回答

0

你可以簡單地覆蓋DialogFragmentonCreateDialog()方法:

@Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     AlertDialog.Builder builder = new Builder(getActivity()); 
builder.setTitle("Students").setNegativeButton("Cancel",new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog,int id) { 
    // if this button is clicked, just close 
    // the dialog box and do nothing 
    dialog.cancel(); 
    } 
});; 
     return builder.create(); 
    } 

希望這有助於。

+0

感謝這個看上去有益的,但我遇到了疑難問題加入你建議的功能,我得到一個例外:requestFeature()必須在添加內容之前被調用。 – Libathos