public class DialogFragment
extends Fragment implements DialogInterface.OnCancelListener, DialogInterface.OnDismissListener
| java.lang.Object | ||
| android.app.Fragment | ||
| android.app.DialogFragment | ||
| |
| |
一个片段,显示一个对话窗口,在其活动窗口之上浮动。 这个片段包含一个Dialog对象,它根据片段的状态适当地显示它。 对话框的控制(决定何时显示,隐藏,解除)应通过此处的API完成,而不是直接调用对话框。
实现应覆盖此类并实现onCreateView(LayoutInflater, ViewGroup, Bundle)以提供对话的内容。 或者,他们可以覆盖onCreateDialog(Bundle)以创建具有自己内容的完全自定义对话框,例如AlertDialog。
涵盖的主题包括:
DialogFragment会执行各种操作来保持片段的生命周期,而不是Dialog。 请注意,对话框通常是自治实体 - 它们是它们自己的窗口,接收它们自己的输入事件,并经常决定何时消失(通过接收后退键事件或用户单击按钮)。
DialogFragment需要确保片段和对话状态发生的情况保持一致。 要做到这一点,它会监视对话中的退出事件,并在发生时删除自己的状态。 这意味着您应该使用show(FragmentManager, String)或show(FragmentTransaction, String)将show(FragmentTransaction, String)实例添加到您的UI中,因为这些实例将跟踪DialogFragment在关闭对话框时应如何移除。
DialogFragment最简单的用法是作为片段视图层次结构的浮动容器。 一个简单的实现可能如下所示:
public static class MyDialogFragment extends DialogFragment {
int mNum;
/**
* Create a new instance of MyDialogFragment, providing "num"
* as an argument.
*/
static MyDialogFragment newInstance(int num) {
MyDialogFragment f = new MyDialogFragment();
// Supply num input as an argument.
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");
// Pick a style based on the 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;
case 6: style = DialogFragment.STYLE_NO_TITLE; break;
case 7: style = DialogFragment.STYLE_NO_FRAME; break;
case 8: style = DialogFragment.STYLE_NORMAL; break;
}
switch ((mNum-1)%6) {
case 4: theme = android.R.style.Theme_Holo; break;
case 5: theme = android.R.style.Theme_Holo_Light_Dialog; break;
case 6: theme = android.R.style.Theme_Holo_Light; break;
case 7: theme = android.R.style.Theme_Holo_Light_Panel; break;
case 8: theme = android.R.style.Theme_Holo_Light; break;
}
setStyle(style, theme);
}
@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));
// Watch for button clicks.
Button button = (Button)v.findViewById(R.id.show);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// When button is clicked, call up to owning activity.
((FragmentDialog)getActivity()).showDialog();
}
});
return v;
}
}
Activity上的showDialog()方法示例可以是:
void showDialog() {
mStackLevel++;
// DialogFragment.show() will take care of adding the fragment
// in a transaction. We also want to remove any currently showing
// dialog, so make our own transaction and take care of that here.
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment prev = getFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
// Create and show the dialog.
DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel);
newFragment.show(ft, "dialog");
}
这将删除当前显示的任何对话框,使用参数创建一个新的DialogFragment,并将其显示为反向堆栈中的新状态。 当事务被弹出时,当前的DialogFragment及其对话框将被销毁,并且前一个(如果有的话)被重新显示。 请注意,在这种情况下,DialogFragment会照顾弹出对话框的事务被单独解除。
除了实现 onCreateView(LayoutInflater, ViewGroup, Bundle)以在对话框内生成视图层次结构之外(或除此之外),您可以实现 onCreateDialog(Bundle)以创建自己的自定义对话框对象。
这对于创建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();
}
}
创建此片段的活动可能具有以下方法来显示对话框并从中接收结果:
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!");
}
请注意,在这种情况下,片段不会放置在背堆栈上,它只会作为无限期运行的片段添加。 由于对话框通常是模态的,因此对话框将捕获用户输入,直到它被解散为止,它仍将作为后退堆栈运行。 当它被解雇时,DialogFragment将负责从自己的片段管理器中删除自己。
如果需要,DialogFragment仍可以选择用作普通片段。 如果你有一个片段,在某些情况下应该显示为对话框,而其他片段嵌入到更大的UI中,这非常有用。 此行为通常会根据您如何使用片段自动为您选择,但可以使用setShowsDialog(boolean)进行自定义。
例如,下面是一个简单的对话框片段:
public static class MyDialogFragment extends DialogFragment {
static MyDialogFragment newInstance() {
return new MyDialogFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.hello_world, container, false);
View tv = v.findViewById(R.id.text);
((TextView)tv).setText("This is an instance of MyDialogFragment");
return v;
}
}
这个片段的一个实例可以创建并显示为一个对话框:
void showDialog() {
// Create the fragment and show it as a dialog.
DialogFragment newFragment = MyDialogFragment.newInstance();
newFragment.show(getFragmentManager(), "dialog");
}
它也可以添加为视图层次结构中的内容:
FragmentTransaction ft = getFragmentManager().beginTransaction(); DialogFragment newFragment = MyDialogFragment.newInstance(); ft.add(R.id.embedded, newFragment); ft.commit();
Inherited XML attributes |
|
|---|---|
android.app.Fragment
|
|
常量(Constants) |
|
|---|---|
int |
STYLE_NORMAL 样式为 |
int |
STYLE_NO_FRAME 样式为 |
int |
STYLE_NO_INPUT 样式为 |
int |
STYLE_NO_TITLE 样式为 |
Inherited constants |
|---|
android.content.ComponentCallbacks2
|
Public constructors |
|
|---|---|
DialogFragment() |
|
公共方法(Public methods) |
|
|---|---|
void |
dismiss() 关闭片段及其对话框。 |
void |
dismissAllowingStateLoss() 版本 |
void |
dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) 将片段的状态打印到给定的流中。 |
Dialog |
getDialog() |
boolean |
getShowsDialog() 返回当前值 |
int |
getTheme() |
boolean |
isCancelable() 返回当前值 |
void |
onActivityCreated(Bundle savedInstanceState) 当片段的活动已经创建并且该片段的视图层次被实例化时调用。 |
void |
onAttach(Context context) 当片段首次附加到其上下文时调用。 |
void |
onCancel(DialogInterface dialog) 这个方法将在对话框被取消时被调用。 |
void |
onCreate(Bundle savedInstanceState) 被调用来做一个片段的初始创建。 |
Dialog |
onCreateDialog(Bundle savedInstanceState) 重写以构建您自己的自定义Dialog容器。 |
void |
onDestroyView() 删除对话框。 |
void |
onDetach() 当片段不再附加到其活动时调用。 |
void |
onDismiss(DialogInterface dialog) 该方法将在对话框关闭时调用。 |
void |
onSaveInstanceState(Bundle outState) 打电话询问片段以保存其当前的动态状态,以便稍后可以在重新启动其进程的新实例时重新构建它。 |
void |
onStart() 当片段对用户可见时调用。 |
void |
onStop() 当片段不再启动时调用。 |
void |
setCancelable(boolean cancelable) 控制显示的对话框是否可取消。 |
void |
setShowsDialog(boolean showsDialog) 控制这个片段是否应该显示在对话框中。 |
void |
setStyle(int style, int theme) 调用来自定义片段对话框的基本外观和行为。 |
void |
show(FragmentManager manager, String tag) 显示对话框,将片段添加到给定的FragmentManager。 |
int |
show(FragmentTransaction transaction, String tag) 显示对话框,使用现有事务添加片段,然后提交事务。 |
继承方法(Inherited methods) |
|
|---|---|
android.app.Fragment
|
|
java.lang.Object
|
|
android.content.ComponentCallbacks2
|
|
android.view.View.OnCreateContextMenuListener
|
|
android.content.DialogInterface.OnCancelListener
|
|
android.content.DialogInterface.OnDismissListener
|
|
android.content.ComponentCallbacks
|
|
int STYLE_NORMAL
样式为 setStyle(int, int) :基本的常规对话框。
常量值:0(0x00000000)
int STYLE_NO_FRAME
样式为setStyle(int, int) :根本不绘制任何框架; 由onCreateView(LayoutInflater, ViewGroup, Bundle)返回的视图层次结构完全负责绘制对话框。
常量值:2(0x00000002)
int STYLE_NO_INPUT
样式为setStyle(int, int) :类似STYLE_NO_FRAME ,但也会禁用对话框的所有输入。 用户不能触摸它,并且其窗口不会接收输入焦点。
常量值:3(0x00000003)
int STYLE_NO_TITLE
样式为 setStyle(int, int) :不包含标题区域。
常数值:1(0x00000001)
void dismiss ()
关闭片段及其对话框。 如果片段被添加到后端堆栈,则将弹出所有后退堆栈状态直到并包括该条目。 否则,一个新的事务将被提交去除片段。
void dismissAllowingStateLoss ()
版本dismiss()使用FragmentTransaction.commitAllowingStateLoss() 。 查看链接的文档以获取更多详细信
void dump (String prefix, FileDescriptor fd, PrintWriter writer, String[] args)
将片段的状态打印到给定的流中。
| 参数(Parameters) | |
|---|---|
prefix |
String: Text to print at the front of each line. |
fd |
FileDescriptor: The raw file descriptor that the dump is being sent to. |
writer |
PrintWriter: The PrintWriter to which you should dump your state. This will be closed for you after you return. |
args |
String: additional arguments to the dump request. |
boolean getShowsDialog ()
返回当前值 setShowsDialog(boolean) 。
| 返回(Returns) | |
|---|---|
boolean |
|
boolean isCancelable ()
返回当前值 setCancelable(boolean) 。
| 返回(Returns) | |
|---|---|
boolean |
|
void onActivityCreated (Bundle savedInstanceState)
当片段的活动已经创建并且该片段的视图层次被实例化时调用。 一旦这些部分到位,它就可以用来进行最终的初始化,例如检索视图或恢复状态。 对于使用setRetainInstance(boolean)保留其实例的片段也很有用,因为此回调告诉片段何时与新活动实例完全关联。 这是在onCreateView(LayoutInflater, ViewGroup, Bundle)之后和onViewStateRestored(Bundle)之前onViewStateRestored(Bundle) 。
| 参数(Parameters) | |
|---|---|
savedInstanceState |
Bundle: If the fragment is being re-created from a previous saved state, this is the state. |
void onAttach (Context context)
当片段首次附加到其上下文时调用。 onCreate(Bundle)将调用onCreate(Bundle) 。
| 参数(Parameters) | |
|---|---|
context |
Context
|
void onCancel (DialogInterface dialog)
这个方法将在对话框被取消时被调用。
| 参数(Parameters) | |
|---|---|
dialog |
DialogInterface: The dialog that was canceled will be passed into the method. |
void onCreate (Bundle savedInstanceState)
被调用来做一个片段的初始创建。 这是在onAttach(Activity)之后和onAttach(Activity)之前onCreateView(LayoutInflater, ViewGroup, Bundle) ,但如果片段实例在Activity重新创建期间保留(请参阅setRetainInstance(boolean) ),则不会调用它。
请注意,这可以在片段的活动仍处于创建过程中时调用。 因此,您不能依赖于此时正在初始化活动的内容视图层次结构。 如果您想在创建活动本身后进行工作,请参阅onActivityCreated(Bundle) 。
如果您的应用的targetSdkVersion为23或更低,则在onCreate返回后,将恢复从savedInstanceState恢复的子片段。 当瞄准N或更高版本并且在N或更新的平台版本上运行时,它们被Fragment.onCreate恢复。
| 参数(Parameters) | |
|---|---|
savedInstanceState |
Bundle: If the fragment is being re-created from a previous saved state, this is the state. |
Dialog onCreateDialog (Bundle savedInstanceState)
重写以构建您自己的自定义Dialog容器。 这通常用于显示AlertDialog而不是通用的Dialog; 这样做时,由于AlertDialog处理自己的内容, onCreateView(LayoutInflater, ViewGroup, Bundle)不需要实现。
此方法将在onCreate(Bundle)之后和onCreateView(LayoutInflater, ViewGroup, Bundle)之前onCreateView(LayoutInflater, ViewGroup, Bundle) 。 默认实现只是实例化并返回一个Dialog类。
注意:DialogFragment拥有Dialog.setOnCancelListener和Dialog.setOnDismissListener回调。 你不能自己设置它们。 要了解这些事件,请覆盖onCancel(DialogInterface)和onDismiss(DialogInterface) 。
| 参数(Parameters) | |
|---|---|
savedInstanceState |
Bundle: The last saved instance state of the Fragment, or null if this is a freshly created Fragment. |
| 返回(Returns) | |
|---|---|
Dialog |
Return a new Dialog instance to be displayed by the Fragment. |
void onDetach ()
当片段不再附加到其活动时调用。 这在onDestroy()之后onDestroy() ,除非在重新创建活动时保留片段实例(请参阅setRetainInstance(boolean) ),在这种情况下,它将在onStop()之后onStop() 。
void onDismiss (DialogInterface dialog)
该方法将在对话框关闭时调用。
| 参数(Parameters) | |
|---|---|
dialog |
DialogInterface: The dialog that was dismissed will be passed into the method. |
void onSaveInstanceState (Bundle outState)
打电话询问片段以保存其当前的动态状态,以便稍后可以在重新启动其进程的新实例时重新构建它。 如果片段的新实例后需要创建,您的包放在这里的数据将提供给包可onCreate(Bundle) , onCreateView(LayoutInflater, ViewGroup, Bundle) ,并onActivityCreated(Bundle) 。
这相当于Activity.onSaveInstanceState(Bundle) ,这里的大多数讨论也适用于此。 但请注意: 此方法可能在onDestroy()之前的任何时间被调用 。 有很多情况下,碎片可能大部分被拆除(例如,当放置在背堆栈上而没有UI显示时),但是它的状态不会被保存直到其拥有的活动实际上需要保存其状态。
| 参数(Parameters) | |
|---|---|
outState |
Bundle: Bundle in which to place your saved state. |
void setCancelable (boolean cancelable)
控制显示的对话框是否可取消。 使用它而不是直接调用Dialog.setCancelable(boolean) ,因为DialogFragment需要根据这个来改变它的行为。
| 参数(Parameters) | |
|---|---|
cancelable |
boolean: If true, the dialog is cancelable. The default is true. |
void setShowsDialog (boolean showsDialog)
控制这个片段是否应该显示在对话框中。 如果未设置,则不会在onActivityCreated(Bundle)创建对话框,因此不会将片段的视图层次添加到该对话框中。 这使您可以将其用作普通片段(嵌入到其活动的内部)。
这通常是基于片段是否与传递给FragmentTransaction.add(int, Fragment)的容器视图ID相关联而FragmentTransaction.add(int, Fragment) 。 如果片段添加了容器,则setShowsDialog将被初始化为false; 否则,这将是真实的。
| 参数(Parameters) | |
|---|---|
showsDialog |
boolean: If true, the fragment will be displayed in a Dialog. If false, no Dialog will be created and the fragment's view hierarchly left undisturbed. |
void setStyle (int style,
int theme)
调用来自定义片段对话框的基本外观和行为。 这可以用于一些常见的对话行为,为您选择标志,主题和其他选项。 通过自己手动设置对话框和窗口属性可以达到同样的效果。 在片段的对话框创建后调用它将不起作用。
| 参数(Parameters) | |
|---|---|
style |
int: Selects a standard style: may be STYLE_NORMAL, STYLE_NO_TITLE, STYLE_NO_FRAME, or STYLE_NO_INPUT. |
theme |
int: Optional custom theme. If 0, an appropriate theme (based on the style) will be selected for you. |
void show (FragmentManager manager, String tag)
显示对话框,将片段添加到给定的FragmentManager。 这对于显式创建事务来说很方便,可以使用给定的标记添加片段并提交它。 这并不事务添加到后退堆栈。 当片段被解散时,将执行新的事务以将其从活动中移除。
| 参数(Parameters) | |
|---|---|
manager |
FragmentManager: The FragmentManager this fragment will be added to. |
tag |
String: The tag for this fragment, as per FragmentTransaction.add. |
int show (FragmentTransaction transaction, String tag)
显示对话框,使用现有事务添加片段,然后提交事务。
| 参数(Parameters) | |
|---|---|
transaction |
FragmentTransaction: An existing transaction in which to add the fragment. |
tag |
String: The tag for this fragment, as per FragmentTransaction.add. |
| 返回(Returns) | |
|---|---|
int |
Returns the identifier of the committed transaction, as per FragmentTransaction.commit(). |