本文介绍了对话框onCreateDialog(int dialogID)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要创建对话框,我要重写以下方法:

To create the dialog, I'm overriding the following method:

protected Dialog onCreateDialog(final int dialogId) {
}

要使用这个,我正在使用:

and to call this, I'm using:

showDialog(id);

但是现在我想使用FragmentDialog,但是没有像这样的方法:

But now I want to use FragmentDialog, but there is no method like:

protected Dialog onCreateDialog(final int dialogId) {
    }

它有

Dialog onCreateDialog(Bundle savedInstanceState){}

我的问题是,如何显示基于FragmentDialog中不同ID的对话框.

My question is, how can I show dialogs based on different id's in FragmentDialog.

推荐答案

我猜FragmentDialog是指在片段内显示对话框,而不是 DialogFragment

I guess by FragmentDialog you mean showing Dialogs inside a Fragment and NOT a DialogFragment

但是您可以在片段中复制此行为,如下所示:

But you can replicate this behavior inside a Fragment Like this:

class SomeFragment extends Fragment{

  HashMap<Integer, Dialog> mDialogs = new HashMap<Integer, Dialog>();

  public void showDialog(int dialogId){

    Dialog d = mDialogs.get(dialogId);
    if (d == null){

      d = onCreateDialog(dialogId);
      mDialogs.put(dialogId, d);
    }
    if (d != null){
       onPrepareDialog(d, dialogId);
       d.show();
    }

  }

  public Dialog onCreateDialog(int dialogId){
    //just create your Dialog here, once
  }

  public void onPrepareDialog(Dialog d, int dialogId){
      super.onPrepareDialog(d, dialogId);
      // change something inside already created Dialogs here
  }
}

这只是对话框的简单缓存

it is just a simple caching of Dialogs

活动的原始来源:

/**
 * Show a dialog managed by this activity.  A call to {@link #onCreateDialog(int)}
 * will be made with the same id the first time this is called for a given
 * id.  From thereafter, the dialog will be automatically saved and restored.
 *
 * Each time a dialog is shown, {@link #onPrepareDialog(int, Dialog)} will
 * be made to provide an opportunity to do any timely preparation.
 *
 * @param id The id of the managed dialog.
 *
 * @see Dialog
 * @see #onCreateDialog(int)
 * @see #onPrepareDialog(int, Dialog)
 * @see #dismissDialog(int)
 * @see #removeDialog(int)
 */
public final void showDialog(int id) {
    if (mManagedDialogs == null) {
        mManagedDialogs = new SparseArray<Dialog>();
    }
    Dialog dialog = mManagedDialogs.get(id);
    if (dialog == null) {
        dialog = createDialog(id, null);
        mManagedDialogs.put(id, dialog);
    }

    onPrepareDialog(id, dialog);
    dialog.show();
}

/**
 * Provides an opportunity to prepare a managed dialog before it is being
 * shown.
 * <p>
 * Override this if you need to update a managed dialog based on the state
 * of the application each time it is shown. For example, a time picker
 * dialog might want to be updated with the current time. You should call
 * through to the superclass's implementation. The default implementation
 * will set this Activity as the owner activity on the Dialog.
 * 
 * @param id The id of the managed dialog.
 * @param dialog The dialog.
 * @see #onCreateDialog(int)
 * @see #showDialog(int)
 * @see #dismissDialog(int)
 * @see #removeDialog(int)
 */
protected void onPrepareDialog(int id, Dialog dialog) {
    dialog.setOwnerActivity(this);
}



 /**
 * Callback for creating dialogs that are managed (saved and restored) for you
 * by the activity.
 *
 * If you use {@link #showDialog(int)}, the activity will call through to
 * this method the first time, and hang onto it thereafter.  Any dialog
 * that is created by this method will automatically be saved and restored
 * for you, including whether it is showing.
 *
 * If you would like the activity to manage the saving and restoring dialogs
 * for you, you should override this method and handle any ids that are
 * passed to {@link #showDialog}.
 *
 * If you would like an opportunity to prepare your dialog before it is shown,
 * override {@link #onPrepareDialog(int, Dialog)}.
 *
 * @param id The id of the dialog.
 * @return The dialog
 *
 * @see #onPrepareDialog(int, Dialog)
 * @see #showDialog(int)
 * @see #dismissDialog(int)
 * @see #removeDialog(int)
 */
protected Dialog onCreateDialog(int id) {
    return null;
}

这篇关于对话框onCreateDialog(int dialogID)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 22:54