Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 進階開發

Android DialogFragment 類別詳解

編輯:進階開發

DialogFragment 是 Android 3.0 新增的 Fragment 子類,位於 android.app.DialogFragment 套件中。它讓對話框也能享有 Fragment 的優勢,但實作方式與傳統的 Dialog 略有不同。本文介紹兩種常見用法:一般對話框與類似 AlertDialog 的警示對話框。

一、一般對話框

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;
    }

    @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);
    }
}

要特別注意:與 Activity 不同,Fragment 的版面配置是在 onCreateView() 中設定,透過 inflater 把 XML 版面映射成 View:

@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();
        }
    });
    return v;
}

在 Activity 中顯示對話框的 showDialog() 方法:

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;
    }

    @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();
    }
}

在 Activity 中呼叫顯示:

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

public void doPositiveClick() {
    Log.i("FragmentAlertDialog", "Positive click!");
}

public void doNegativeClick() {
    Log.i("FragmentAlertDialog", "Negative click!");
}

關鍵差異

請留意兩者的分工:一般對話框透過 onCreateView() 設定自訂版面;而警示對話框則是重寫 onCreateDialog() 直接回傳一個 AlertDialog。掌握這個差別,就能依需求靈活選擇合適的實作方式。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved