AlertDialog的create和show

来源:互联网 发布:ubuntu 16.04与16.10 编辑:程序博客网 时间:2024/05/04 02:58

《疯狂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;        }
2 0
原创粉丝点击