Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 高級開發 >> Android DialogFragment類介紹

Android 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!");

  }

  如果你不了解Fragment,可以查看android Fragment使用詳解一文

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved