DialogFragment类介绍

DialogFragment是Android 3.0新增的Fragment子类,他的Package在android.app.DialogFragment 中,Dialog和Android常规的Dialog是不同的实现方法。

一、常规对话框

public static class MyDialogFragment extends DialogFragment {
int mNum;

static MyDialogFragment newInstance(int num) {
    MyDialogFragment f = new MyDialogFragment();

    Bundle args = new Bundle();
    args.putInt("num", num);
    f.setArguments(args);

    return f;
}

下面是Fragment的onCreate方法,需要注意的是,设置布局不在这里,和Activity有些不同。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mNum = getArguments().getInt("num");

    int style = DialogFragment.STYLE_NORMAL, theme = 0;
    switch ((mNum-1)%6) {
        case 1: style = DialogFragment.STYLE_NO_TITLE; break;
        case 2: style = DialogFragment.STYLE_NO_FRAME; break;
        case 3: style = DialogFragment.STYLE_NO_INPUT; break;
        case 4: style = DialogFragment.STYLE_NORMAL; break;
        case 5: style = DialogFragment.STYLE_NORMAL; break;
    }
    switch ((mNum-1)%6) {
        case 4: theme = android.R.style.Theme_Light; break;
        case 5: theme = android.R.style.Theme; break;
    }
    setStyle(style, theme);
}

在Fragment设置布局在onCreateView中,使用inflater可以映射一个xml方式布局的layout文件。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_dialog, container, false);
    View tv = v.findViewById(R.id.text);
    ((TextView)tv).setText("Dialog #" + mNum + ": using style "
            + getNameForNum(mNum));

    Button button = (Button)v.findViewById(R.id.show);
    button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            ((FragmentDialog)getActivity()).showDialog(); //显示fragmentdialog
        }
    });

    return v;
}

}

上面的showDialog方法在Activity中的定义如下:

void showDialog() {
mStackLevel++;

FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment prev = getFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
    ft.remove(prev);
}
ft.addToBackStack(null);

DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel);
newFragment.show(ft, "dialog");

}

二、警告对话框,类似AlertDialog

public static class MyAlertDialogFragment extends DialogFragment {

public static MyAlertDialogFragment newInstance(int title) { 
    MyAlertDialogFragment frag = new MyAlertDialogFragment(); 
    Bundle args = new Bundle(); 
    args.putInt("title", title); 
    frag.setArguments(args); 
    return frag; 
} 

这里Android开发网提示大家,下面重写了onCreateDialog方法,而不是onCreateView,希望大家注意:

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    int title = getArguments().getInt("title"); 

    return new AlertDialog.Builder(getActivity()) 
            .setIcon(R.drawable.alert_dialog_icon) 
            .setTitle(title) 
            .setPositiveButton(R.string.alert_dialog_ok, 
                new DialogInterface.OnClickListener() { 
                    public void onClick(DialogInterface dialog, int whichButton) { 
                        ((FragmentAlertDialog)getActivity()).doPositiveClick(); 
                    } 
                } 
            ) 
            .setNegativeButton(R.string.alert_dialog_cancel, 
                new DialogInterface.OnClickListener() { 
                    public void onClick(DialogInterface dialog, int whichButton) { 
                        ((FragmentAlertDialog)getActivity()).doNegativeClick(); 
                    } 
                } 
            ) 
            .create(); 
} 

}

显示Alert的Fragment类似AlertDialog,下面是Activity中使用的showDialog的实现代码,如下

void showDialog() {
DialogFragment newFragment = MyAlertDialogFragment.newInstance(
R.string.alert_dialog_two_buttons_title);
newFragment.show(getFragmentManager(), “dialog”);
}

public void doPositiveClick() {
// Do stuff here.
Log.i(“FragmentAlertDialog”, “Positive click!”);
}

public void doNegativeClick() {
// Do stuff here.
Log.i(“FragmentAlertDialog”, “Negative click!”);
}