您的位置:首页 > 移动开发 > Android开发

AlertDialog的create和show

2016-01-12 17:40 656 查看
《疯狂Android讲义》中,对话框的设置用builder,对话框的显示需要先builder.create后builder.show,不知道是不是版本问题,不过从源代码来看,如果builder.show了,就不用调用builder.create。

注释中说到:create:用builder提供的参数来创建一个AlertDialog。调用该方法并不会显示对话框。如果不需要附加操作(processing),则调用show将会代替create和显示对话框。

show:创建对话框并立即显示。

所以如果创建对话框后还需要调整,则可以先create,再show,如果不需要调整,则可以直接show。

AlertDialog.show() : 1)final AlertDialog dialog = builder.create();

2)(AlertDialog)dialog.show();

/**
* Creates an {@link AlertDialog} with the arguments supplied to this
* builder.
* <p>
* Calling this method does not display the dialog. If no additional
* processing is needed, {@link #show()} may be called instead to both
* create and display the dialog.
*/
public AlertDialog create() {
// Context has already been wrapped with the appropriate theme.
final AlertDialog dialog = new AlertDialog(P.mContext, 0, false);
P.apply(dialog.mAlert);
dialog.setCancelable(P.mCancelable);
if (P.mCancelable) {
dialog.setCanceledOnTouchOutside(true);
}
dialog.setOnCancelListener(P.mOnCancelListener);
dialog.setOnDismissListener(P.mOnDismissListener);
if (P.mOnKeyListener != null) {
dialog.setOnKeyListener(P.mOnKeyListener);
}
return dialog;
}

/**
* Creates an {@link AlertDialog} with the arguments supplied to this
* builder and immediately displays the dialog.
* <p>
* Calling this method is functionally identical to:
* <pre>
*     AlertDialog dialog = builder.create();
*     dialog.show();
* </pre>
*/
public AlertDialog show() {
final AlertDialog dialog = create();
dialog.show();
return dialog;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android