ApiDemos--DialogFragment的使用
在ApiDemos中,FragmentDialog类展示了如何使用DialogFragment。使用DialogFragment可以达到和直接使用普通Dialog的效果。阅读本文前,请下载ApiDemos工程。
本文假设你已经熟悉了Fragment的基本使用方法。使用DialogFragment时,如果需要指定Dialog自定义的content view,那么需要覆写onCreateView()回调:
例如如下代码:
@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;
}
如果需要指定Dialog的主题和样式的话,建议在onCreate()回调中通过setStyle()函数设置:
@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);
}
关于DialogFragment的显示也很简单,直接调用它的方法show()就可以实现,原型如下:
int android.app.DialogFragment.show(FragmentTransaction transaction, String tag)
Display the dialog, adding the fragment using an existing transaction and then committing the transaction.
Parameters:
transaction An existing transaction in which to add the fragment.
tag The tag for this fragment, as per FragmentTransaction.add.
Returns:
Returns the identifier of the committed transaction, as per FragmentTransaction.commit().
那么如果我只想使用AlertDialog风格样式的Dialog怎么办呢?那么你只需要实现回调onCreateDialog(),返回一个AlertDialog即可,例如:
@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();
}
通常Fragment类都被设计成顶层类或者嵌套类,而不要设计成内部类,为什么要这样呢?这是由于Fragment的恢复机制决定的,假设我们将FragmentDialog中的MyDialogFragment设计成一个内部类,当然也可以完美运行,但是当我们旋转屏幕的时候,会收到以下异常log:
10-08 13:47:39.080: E/AndroidRuntime(16445): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.android.apis.app.FragmentDialog$MyDialogFragment: make sure class name exists, is public, and has an empty constructor that is public
上面的异常是由于FragmentManager试图恢复Fragment时去创建它的实例,但是发现无法创建成功,这是因为由于MyDialogFragment是一个内部类,不能通过类名直接创建,而必须先创建外部类,然后再创建内部类,所以导致创建失败的异常。
个人建议使用Fragment时,使用嵌套类。如果需要和外部类通信的话,建议传递外部类引用到你的嵌套类中。