2012-10-19 61 views
0

我剛剛嘗試轉換使用擴展DialogFragment而不是Dialog的ProgressDialog的應用程序。使用DialogFragment自定義對話框

你怎麼能

  1. 創建擴展DialogFragment
  2. 然後由活動調用這個類加載對話框定製對話框類

可我看到一個超級簡單的例子?什麼方法可以重寫?那構造函數呢?

現在這是我在做什麼(舊的方式):

private MyProgressDialog progressDialog = new MyProgressDialog(
        getActivity()); 
progressdialog.show(); 

回答

3

所以,你想一個進度對話框片段

其實很簡單。 DialogFragments只是包圍對話框的碎片,因此您只需像往常一樣在DialogFragment的onCreateDialog方法中創建對話框即可。

例如

class ProgressDialogFragment extends DialogFragment{ 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     ProgressDialog dialog = new ProgressDialog(getActivity()); 
     dialog.setMessage("Loading"); 
     return dialog; 
    } 
} 

,並顯示出了DialogFragment:

mButton = (Button)getView().findViewById(R.id.button1); 
mButton.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     new ProgressDialogFragment().show(getFragmentManager(), "MyProgressDialog"); 
    } 
});