Android Dailog详解三

来源:互联网 发布:vb中xor是什么意思 编辑:程序博客网 时间:2024/04/29 07:30

Android   Dailog详解三

附录AlertDialog源码
64public class AlertDialog extends Dialog implements DialogInterface {65    private AlertController mAlert;6667    /**68     * Special theme constant for {@link #AlertDialog(Context, int)}: use69     * the traditional (pre-Holo) alert dialog theme.70     */71    public static final int THEME_TRADITIONAL = 1;7273    /**74     * Special theme constant for {@link #AlertDialog(Context, int)}: use75     * the holographic alert theme with a dark background.76     */77    public static final int THEME_HOLO_DARK = 2;7879    /**80     * Special theme constant for {@link #AlertDialog(Context, int)}: use81     * the holographic alert theme with a light background.82     */83    public static final int THEME_HOLO_LIGHT = 3;8485    /**86     * Special theme constant for {@link #AlertDialog(Context, int)}: use87     * the device's default alert theme with a dark background.88     */89    public static final int THEME_DEVICE_DEFAULT_DARK = 4;9091    /**92     * Special theme constant for {@link #AlertDialog(Context, int)}: use93     * the device's default alert theme with a light background.94     */95    public static final int THEME_DEVICE_DEFAULT_LIGHT = 5;9697    protected AlertDialog(Context context) {98        this(context, resolveDialogTheme(context, 0), true);99    }100101    /**102     * Construct an AlertDialog that uses an explicit theme.  The actual style103     * that an AlertDialog uses is a private implementation, however you can104     * here supply either the name of an attribute in the theme from which105     * to get the dialog's style (such as {@link android.R.attr#alertDialogTheme}106     * or one of the constants {@link #THEME_TRADITIONAL},107     * {@link #THEME_HOLO_DARK}, or {@link #THEME_HOLO_LIGHT}.108     */109    protected AlertDialog(Context context, int theme) {110        this(context, theme, true);111    }112113    AlertDialog(Context context, int theme, boolean createThemeContextWrapper) {114        super(context, resolveDialogTheme(context, theme), createThemeContextWrapper);115116        mWindow.alwaysReadCloseOnTouchAttr();117        mAlert = new AlertController(getContext(), this, getWindow());118    }119120    protected AlertDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {121        super(context, resolveDialogTheme(context, 0));122        mWindow.alwaysReadCloseOnTouchAttr();123        setCancelable(cancelable);124        setOnCancelListener(cancelListener);125        mAlert = new AlertController(context, this, getWindow());126    }127128    static int resolveDialogTheme(Context context, int resid) {129        if (resid == THEME_TRADITIONAL) {130            return com.android.internal.R.style.Theme_Dialog_Alert;131        } else if (resid == THEME_HOLO_DARK) {132            return com.android.internal.R.style.Theme_Holo_Dialog_Alert;133        } else if (resid == THEME_HOLO_LIGHT) {134            return com.android.internal.R.style.Theme_Holo_Light_Dialog_Alert;135        } else if (resid == THEME_DEVICE_DEFAULT_DARK) {136            return com.android.internal.R.style.Theme_DeviceDefault_Dialog_Alert;137        } else if (resid == THEME_DEVICE_DEFAULT_LIGHT) {138            return com.android.internal.R.style.Theme_DeviceDefault_Light_Dialog_Alert;139        } else if (resid >= 0x01000000) {   // start of real resource IDs.140            return resid;141        } else {142            TypedValue outValue = new TypedValue();143            context.getTheme().resolveAttribute(com.android.internal.R.attr.alertDialogTheme,144                    outValue, true);145            return outValue.resourceId;146        }147    }148149    /**150     * Gets one of the buttons used in the dialog.151     * <p>152     * If a button does not exist in the dialog, null will be returned.153     *154     * @param whichButton The identifier of the button that should be returned.155     *            For example, this can be156     *            {@link DialogInterface#BUTTON_POSITIVE}.157     * @return The button from the dialog, or null if a button does not exist.158     */159    public Button getButton(int whichButton) {160        return mAlert.getButton(whichButton);161    }162163    /**164     * Gets the list view used in the dialog.165     *166     * @return The {@link ListView} from the dialog.167     */168    public ListView getListView() {169        return mAlert.getListView();170    }171172    @Override173    public void setTitle(CharSequence title) {174        super.setTitle(title);175        mAlert.setTitle(title);176    }177178    /**179     * @see Builder#setCustomTitle(View)180     */181    public void setCustomTitle(View customTitleView) {182        mAlert.setCustomTitle(customTitleView);183    }184185    public void setMessage(CharSequence message) {186        mAlert.setMessage(message);187    }188189    /**190     * Set the view to display in that dialog.191     */192    public void setView(View view) {193        mAlert.setView(view);194    }195196    /**197     * Set the view to display in that dialog, specifying the spacing to appear around that198     * view.199     *200     * @param view The view to show in the content area of the dialog201     * @param viewSpacingLeft Extra space to appear to the left of {@code view}202     * @param viewSpacingTop Extra space to appear above {@code view}203     * @param viewSpacingRight Extra space to appear to the right of {@code view}204     * @param viewSpacingBottom Extra space to appear below {@code view}205     */206    public void setView(View view, int viewSpacingLeft, int viewSpacingTop, int viewSpacingRight,207            int viewSpacingBottom) {208        mAlert.setView(view, viewSpacingLeft, viewSpacingTop, viewSpacingRight, viewSpacingBottom);209    }210211    /**212     * Set a message to be sent when a button is pressed.213     *214     * @param whichButton Which button to set the message for, can be one of215     *            {@link DialogInterface#BUTTON_POSITIVE},216     *            {@link DialogInterface#BUTTON_NEGATIVE}, or217     *            {@link DialogInterface#BUTTON_NEUTRAL}218     * @param text The text to display in positive button.219     * @param msg The {@link Message} to be sent when clicked.220     */221    public void setButton(int whichButton, CharSequence text, Message msg) {222        mAlert.setButton(whichButton, text, null, msg);223    }224225    /**226     * Set a listener to be invoked when the positive button of the dialog is pressed.227     *228     * @param whichButton Which button to set the listener on, can be one of229     *            {@link DialogInterface#BUTTON_POSITIVE},230     *            {@link DialogInterface#BUTTON_NEGATIVE}, or231     *            {@link DialogInterface#BUTTON_NEUTRAL}232     * @param text The text to display in positive button.233     * @param listener The {@link DialogInterface.OnClickListener} to use.234     */235    public void setButton(int whichButton, CharSequence text, OnClickListener listener) {236        mAlert.setButton(whichButton, text, listener, null);237    }238239    /**240     * @deprecated Use {@link #setButton(int, CharSequence, Message)} with241     *             {@link DialogInterface#BUTTON_POSITIVE}.242     */243    @Deprecated244    public void setButton(CharSequence text, Message msg) {245        setButton(BUTTON_POSITIVE, text, msg);246    }247248    /**249     * @deprecated Use {@link #setButton(int, CharSequence, Message)} with250     *             {@link DialogInterface#BUTTON_NEGATIVE}.251     */252    @Deprecated253    public void setButton2(CharSequence text, Message msg) {254        setButton(BUTTON_NEGATIVE, text, msg);255    }256257    /**258     * @deprecated Use {@link #setButton(int, CharSequence, Message)} with259     *             {@link DialogInterface#BUTTON_NEUTRAL}.260     */261    @Deprecated262    public void setButton3(CharSequence text, Message msg) {263        setButton(BUTTON_NEUTRAL, text, msg);264    }265266    /**267     * Set a listener to be invoked when button 1 of the dialog is pressed.268     *269     * @param text The text to display in button 1.270     * @param listener The {@link DialogInterface.OnClickListener} to use.271     * @deprecated Use272     *             {@link #setButton(int, CharSequence, android.content.DialogInterface.OnClickListener)}273     *             with {@link DialogInterface#BUTTON_POSITIVE}274     */275    @Deprecated276    public void setButton(CharSequence text, final OnClickListener listener) {277        setButton(BUTTON_POSITIVE, text, listener);278    }279280    /**281     * Set a listener to be invoked when button 2 of the dialog is pressed.282     * @param text The text to display in button 2.283     * @param listener The {@link DialogInterface.OnClickListener} to use.284     * @deprecated Use285     *             {@link #setButton(int, CharSequence, android.content.DialogInterface.OnClickListener)}286     *             with {@link DialogInterface#BUTTON_NEGATIVE}287     */288    @Deprecated289    public void setButton2(CharSequence text, final OnClickListener listener) {290        setButton(BUTTON_NEGATIVE, text, listener);291    }292293    /**294     * Set a listener to be invoked when button 3 of the dialog is pressed.295     * @param text The text to display in button 3.296     * @param listener The {@link DialogInterface.OnClickListener} to use.297     * @deprecated Use298     *             {@link #setButton(int, CharSequence, android.content.DialogInterface.OnClickListener)}299     *             with {@link DialogInterface#BUTTON_POSITIVE}300     */301    @Deprecated302    public void setButton3(CharSequence text, final OnClickListener listener) {303        setButton(BUTTON_NEUTRAL, text, listener);304    }305306    /**307     * Set resId to 0 if you don't want an icon.308     * @param resId the resourceId of the drawable to use as the icon or 0309     * if you don't want an icon.310     */311    public void setIcon(int resId) {312        mAlert.setIcon(resId);313    }314315    public void setIcon(Drawable icon) {316        mAlert.setIcon(icon);317    }318319    /**320     * Set an icon as supplied by a theme attribute. e.g. android.R.attr.alertDialogIcon321     *322     * @param attrId ID of a theme attribute that points to a drawable resource.323     */324    public void setIconAttribute(int attrId) {325        TypedValue out = new TypedValue();326        mContext.getTheme().resolveAttribute(attrId, out, true);327        mAlert.setIcon(out.resourceId);328    }329330    public void setInverseBackgroundForced(boolean forceInverseBackground) {331        mAlert.setInverseBackgroundForced(forceInverseBackground);332    }333334    @Override335    protected void onCreate(Bundle savedInstanceState) {336        super.onCreate(savedInstanceState);337        mAlert.installContent();338    }339340    @Override341    public boolean onKeyDown(int keyCode, KeyEvent event) {342        if (mAlert.onKeyDown(keyCode, event)) return true;343        return super.onKeyDown(keyCode, event);344    }345346    @Override347    public boolean onKeyUp(int keyCode, KeyEvent event) {348        if (mAlert.onKeyUp(keyCode, event)) return true;349        return super.onKeyUp(keyCode, event);350    }351352    public static class Builder {353        private final AlertController.AlertParams P;354        private int mTheme;355356        /**357         * Constructor using a context for this builder and the {@link AlertDialog} it creates.358         */359        public Builder(Context context) {360            this(context, resolveDialogTheme(context, 0));361        }362363        /**364         * Constructor using a context and theme for this builder and365         * the {@link AlertDialog} it creates.  The actual theme366         * that an AlertDialog uses is a private implementation, however you can367         * here supply either the name of an attribute in the theme from which368         * to get the dialog's style (such as {@link android.R.attr#alertDialogTheme}369         * or one of the constants370         * {@link AlertDialog#THEME_TRADITIONAL AlertDialog.THEME_TRADITIONAL},371         * {@link AlertDialog#THEME_HOLO_DARK AlertDialog.THEME_HOLO_DARK}, or372         * {@link AlertDialog#THEME_HOLO_LIGHT AlertDialog.THEME_HOLO_LIGHT}.373         */374        public Builder(Context context, int theme) {375            P = new AlertController.AlertParams(new ContextThemeWrapper(376                    context, resolveDialogTheme(context, theme)));377            mTheme = theme;378        }379380        /**381         * Returns a {@link Context} with the appropriate theme for dialogs created by this Builder.382         * Applications should use this Context for obtaining LayoutInflaters for inflating views383         * that will be used in the resulting dialogs, as it will cause views to be inflated with384         * the correct theme.385         *386         * @return A Context for built Dialogs.387         */388        public Context getContext() {389            return P.mContext;390        }391392        /**393         * Set the title using the given resource id.394         *395         * @return This Builder object to allow for chaining of calls to set methods396         */397        public Builder setTitle(int titleId) {398            P.mTitle = P.mContext.getText(titleId);399            return this;400        }401402        /**403         * Set the title displayed in the {@link Dialog}.404         *405         * @return This Builder object to allow for chaining of calls to set methods406         */407        public Builder setTitle(CharSequence title) {408            P.mTitle = title;409            return this;410        }411412        /**413         * Set the title using the custom view {@code customTitleView}. The414         * methods {@link #setTitle(int)} and {@link #setIcon(int)} should be415         * sufficient for most titles, but this is provided if the title needs416         * more customization. Using this will replace the title and icon set417         * via the other methods.418         *419         * @param customTitleView The custom view to use as the title.420         *421         * @return This Builder object to allow for chaining of calls to set methods422         */423        public Builder setCustomTitle(View customTitleView) {424            P.mCustomTitleView = customTitleView;425            return this;426        }427428        /**429         * Set the message to display using the given resource id.430         *431         * @return This Builder object to allow for chaining of calls to set methods432         */433        public Builder setMessage(int messageId) {434            P.mMessage = P.mContext.getText(messageId);435            return this;436        }437438        /**439         * Set the message to display.440          *441         * @return This Builder object to allow for chaining of calls to set methods442         */443        public Builder setMessage(CharSequence message) {444            P.mMessage = message;445            return this;446        }447448        /**449         * Set the resource id of the {@link Drawable} to be used in the title.450         *451         * @return This Builder object to allow for chaining of calls to set methods452         */453        public Builder setIcon(int iconId) {454            P.mIconId = iconId;455            return this;456        }457458        /**459         * Set the {@link Drawable} to be used in the title.460          *461         * @return This Builder object to allow for chaining of calls to set methods462         */463        public Builder setIcon(Drawable icon) {464            P.mIcon = icon;465            return this;466        }467468        /**469         * Set an icon as supplied by a theme attribute. e.g. android.R.attr.alertDialogIcon470         *471         * @param attrId ID of a theme attribute that points to a drawable resource.472         */473        public Builder setIconAttribute(int attrId) {474            TypedValue out = new TypedValue();475            P.mContext.getTheme().resolveAttribute(attrId, out, true);476            P.mIconId = out.resourceId;477            return this;478        }479480        /**481         * Set a listener to be invoked when the positive button of the dialog is pressed.482         * @param textId The resource id of the text to display in the positive button483         * @param listener The {@link DialogInterface.OnClickListener} to use.484         *485         * @return This Builder object to allow for chaining of calls to set methods486         */487        public Builder setPositiveButton(int textId, final OnClickListener listener) {488            P.mPositiveButtonText = P.mContext.getText(textId);489            P.mPositiveButtonListener = listener;490            return this;491        }492493        /**494         * Set a listener to be invoked when the positive button of the dialog is pressed.495         * @param text The text to display in the positive button496         * @param listener The {@link DialogInterface.OnClickListener} to use.497         *498         * @return This Builder object to allow for chaining of calls to set methods499         */500        public Builder setPositiveButton(CharSequence text, final OnClickListener listener) {501            P.mPositiveButtonText = text;502            P.mPositiveButtonListener = listener;503            return this;504        }505506        /**507         * Set a listener to be invoked when the negative button of the dialog is pressed.508         * @param textId The resource id of the text to display in the negative button509         * @param listener The {@link DialogInterface.OnClickListener} to use.510         *511         * @return This Builder object to allow for chaining of calls to set methods512         */513        public Builder setNegativeButton(int textId, final OnClickListener listener) {514            P.mNegativeButtonText = P.mContext.getText(textId);515            P.mNegativeButtonListener = listener;516            return this;517        }518519        /**520         * Set a listener to be invoked when the negative button of the dialog is pressed.521         * @param text The text to display in the negative button522         * @param listener The {@link DialogInterface.OnClickListener} to use.523         *524         * @return This Builder object to allow for chaining of calls to set methods525         */526        public Builder setNegativeButton(CharSequence text, final OnClickListener listener) {527            P.mNegativeButtonText = text;528            P.mNegativeButtonListener = listener;529            return this;530        }531532        /**533         * Set a listener to be invoked when the neutral button of the dialog is pressed.534         * @param textId The resource id of the text to display in the neutral button535         * @param listener The {@link DialogInterface.OnClickListener} to use.536         *537         * @return This Builder object to allow for chaining of calls to set methods538         */539        public Builder setNeutralButton(int textId, final OnClickListener listener) {540            P.mNeutralButtonText = P.mContext.getText(textId);541            P.mNeutralButtonListener = listener;542            return this;543        }544545        /**546         * Set a listener to be invoked when the neutral button of the dialog is pressed.547         * @param text The text to display in the neutral button548         * @param listener The {@link DialogInterface.OnClickListener} to use.549         *550         * @return This Builder object to allow for chaining of calls to set methods551         */552        public Builder setNeutralButton(CharSequence text, final OnClickListener listener) {553            P.mNeutralButtonText = text;554            P.mNeutralButtonListener = listener;555            return this;556        }557558        /**559         * Sets whether the dialog is cancelable or not.  Default is true.560         *561         * @return This Builder object to allow for chaining of calls to set methods562         */563        public Builder setCancelable(boolean cancelable) {564            P.mCancelable = cancelable;565            return this;566        }567568        /**569         * Sets the callback that will be called if the dialog is canceled.570         *571         * <p>Even in a cancelable dialog, the dialog may be dismissed for reasons other than572         * being canceled or one of the supplied choices being selected.573         * If you are interested in listening for all cases where the dialog is dismissed574         * and not just when it is canceled, see575         * {@link #setOnDismissListener(android.content.DialogInterface.OnDismissListener) setOnDismissListener}.</p>576         * @see #setCancelable(boolean)577         * @see #setOnDismissListener(android.content.DialogInterface.OnDismissListener)578         *579         * @return This Builder object to allow for chaining of calls to set methods580         */581        public Builder setOnCancelListener(OnCancelListener onCancelListener) {582            P.mOnCancelListener = onCancelListener;583            return this;584        }585586        /**587         * Sets the callback that will be called when the dialog is dismissed for any reason.588         *589         * @return This Builder object to allow for chaining of calls to set methods590         */591        public Builder setOnDismissListener(OnDismissListener onDismissListener) {592            P.mOnDismissListener = onDismissListener;593            return this;594        }595596        /**597         * Sets the callback that will be called if a key is dispatched to the dialog.598         *599         * @return This Builder object to allow for chaining of calls to set methods600         */601        public Builder setOnKeyListener(OnKeyListener onKeyListener) {602            P.mOnKeyListener = onKeyListener;603            return this;604        }605606        /**607         * Set a list of items to be displayed in the dialog as the content, you will be notified of the608         * selected item via the supplied listener. This should be an array type i.e. R.array.foo609         *610         * @return This Builder object to allow for chaining of calls to set methods611         */612        public Builder setItems(int itemsId, final OnClickListener listener) {613            P.mItems = P.mContext.getResources().getTextArray(itemsId);614            P.mOnClickListener = listener;615            return this;616        }617618        /**619         * Set a list of items to be displayed in the dialog as the content, you will be notified of the620         * selected item via the supplied listener.621         *622         * @return This Builder object to allow for chaining of calls to set methods623         */624        public Builder setItems(CharSequence[] items, final OnClickListener listener) {625            P.mItems = items;626            P.mOnClickListener = listener;627            return this;628        }629630        /**631         * Set a list of items, which are supplied by the given {@link ListAdapter}, to be632         * displayed in the dialog as the content, you will be notified of the633         * selected item via the supplied listener.634         *635         * @param adapter The {@link ListAdapter} to supply the list of items636         * @param listener The listener that will be called when an item is clicked.637         *638         * @return This Builder object to allow for chaining of calls to set methods639         */640        public Builder setAdapter(final ListAdapter adapter, final OnClickListener listener) {641            P.mAdapter = adapter;642            P.mOnClickListener = listener;643            return this;644        }645646        /**647         * Set a list of items, which are supplied by the given {@link Cursor}, to be648         * displayed in the dialog as the content, you will be notified of the649         * selected item via the supplied listener.650         *651         * @param cursor The {@link Cursor} to supply the list of items652         * @param listener The listener that will be called when an item is clicked.653         * @param labelColumn The column name on the cursor containing the string to display654         *          in the label.655         *656         * @return This Builder object to allow for chaining of calls to set methods657         */658        public Builder setCursor(final Cursor cursor, final OnClickListener listener,659                String labelColumn) {660            P.mCursor = cursor;661            P.mLabelColumn = labelColumn;662            P.mOnClickListener = listener;663            return this;664        }665666        /**667         * Set a list of items to be displayed in the dialog as the content,668         * you will be notified of the selected item via the supplied listener.669         * This should be an array type, e.g. R.array.foo. The list will have670         * a check mark displayed to the right of the text for each checked671         * item. Clicking on an item in the list will not dismiss the dialog.672         * Clicking on a button will dismiss the dialog.673         *674         * @param itemsId the resource id of an array i.e. R.array.foo675         * @param checkedItems specifies which items are checked. It should be null in which case no676         *        items are checked. If non null it must be exactly the same length as the array of677         *        items.678         * @param listener notified when an item on the list is clicked. The dialog will not be679         *        dismissed when an item is clicked. It will only be dismissed if clicked on a680         *        button, if no buttons are supplied it's up to the user to dismiss the dialog.681         *682         * @return This Builder object to allow for chaining of calls to set methods683         */684        public Builder setMultiChoiceItems(int itemsId, boolean[] checkedItems,685                final OnMultiChoiceClickListener listener) {686            P.mItems = P.mContext.getResources().getTextArray(itemsId);687            P.mOnCheckboxClickListener = listener;688            P.mCheckedItems = checkedItems;689            P.mIsMultiChoice = true;690            return this;691        }692693        /**694         * Set a list of items to be displayed in the dialog as the content,695         * you will be notified of the selected item via the supplied listener.696         * The list will have a check mark displayed to the right of the text697         * for each checked item. Clicking on an item in the list will not698         * dismiss the dialog. Clicking on a button will dismiss the dialog.699         *700         * @param items the text of the items to be displayed in the list.701         * @param checkedItems specifies which items are checked. It should be null in which case no702         *        items are checked. If non null it must be exactly the same length as the array of703         *        items.704         * @param listener notified when an item on the list is clicked. The dialog will not be705         *        dismissed when an item is clicked. It will only be dismissed if clicked on a706         *        button, if no buttons are supplied it's up to the user to dismiss the dialog.707         *708         * @return This Builder object to allow for chaining of calls to set methods709         */710        public Builder setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems,711                final OnMultiChoiceClickListener listener) {712            P.mItems = items;713            P.mOnCheckboxClickListener = listener;714            P.mCheckedItems = checkedItems;715            P.mIsMultiChoice = true;716            return this;717        }718719        /**720         * Set a list of items to be displayed in the dialog as the content,721         * you will be notified of the selected item via the supplied listener.722         * The list will have a check mark displayed to the right of the text723         * for each checked item. Clicking on an item in the list will not724         * dismiss the dialog. Clicking on a button will dismiss the dialog.725         *726         * @param cursor the cursor used to provide the items.727         * @param isCheckedColumn specifies the column name on the cursor to use to determine728         *        whether a checkbox is checked or not. It must return an integer value where 1729         *        means checked and 0 means unchecked.730         * @param labelColumn The column name on the cursor containing the string to display in the731         *        label.732         * @param listener notified when an item on the list is clicked. The dialog will not be733         *        dismissed when an item is clicked. It will only be dismissed if clicked on a734         *        button, if no buttons are supplied it's up to the user to dismiss the dialog.735         *736         * @return This Builder object to allow for chaining of calls to set methods737         */738        public Builder setMultiChoiceItems(Cursor cursor, String isCheckedColumn, String labelColumn,739                final OnMultiChoiceClickListener listener) {740            P.mCursor = cursor;741            P.mOnCheckboxClickListener = listener;742            P.mIsCheckedColumn = isCheckedColumn;743            P.mLabelColumn = labelColumn;744            P.mIsMultiChoice = true;745            return this;746        }747748        /**749         * Set a list of items to be displayed in the dialog as the content, you will be notified of750         * the selected item via the supplied listener. This should be an array type i.e.751         * R.array.foo The list will have a check mark displayed to the right of the text for the752         * checked item. Clicking on an item in the list will not dismiss the dialog. Clicking on a753         * button will dismiss the dialog.754         *755         * @param itemsId the resource id of an array i.e. R.array.foo756         * @param checkedItem specifies which item is checked. If -1 no items are checked.757         * @param listener notified when an item on the list is clicked. The dialog will not be758         *        dismissed when an item is clicked. It will only be dismissed if clicked on a759         *        button, if no buttons are supplied it's up to the user to dismiss the dialog.760         *761         * @return This Builder object to allow for chaining of calls to set methods762         */763        public Builder setSingleChoiceItems(int itemsId, int checkedItem,764                final OnClickListener listener) {765            P.mItems = P.mContext.getResources().getTextArray(itemsId);766            P.mOnClickListener = listener;767            P.mCheckedItem = checkedItem;768            P.mIsSingleChoice = true;769            return this;770        }771772        /**773         * Set a list of items to be displayed in the dialog as the content, you will be notified of774         * the selected item via the supplied listener. The list will have a check mark displayed to775         * the right of the text for the checked item. Clicking on an item in the list will not776         * dismiss the dialog. Clicking on a button will dismiss the dialog.777         *778         * @param cursor the cursor to retrieve the items from.779         * @param checkedItem specifies which item is checked. If -1 no items are checked.780         * @param labelColumn The column name on the cursor containing the string to display in the781         *        label.782         * @param listener notified when an item on the list is clicked. The dialog will not be783         *        dismissed when an item is clicked. It will only be dismissed if clicked on a784         *        button, if no buttons are supplied it's up to the user to dismiss the dialog.785         *786         * @return This Builder object to allow for chaining of calls to set methods787         */788        public Builder setSingleChoiceItems(Cursor cursor, int checkedItem, String labelColumn,789                final OnClickListener listener) {790            P.mCursor = cursor;791            P.mOnClickListener = listener;792            P.mCheckedItem = checkedItem;793            P.mLabelColumn = labelColumn;794            P.mIsSingleChoice = true;795            return this;796        }797798        /**799         * Set a list of items to be displayed in the dialog as the content, you will be notified of800         * the selected item via the supplied listener. The list will have a check mark displayed to801         * the right of the text for the checked item. Clicking on an item in the list will not802         * dismiss the dialog. Clicking on a button will dismiss the dialog.803         *804         * @param items the items to be displayed.805         * @param checkedItem specifies which item is checked. If -1 no items are checked.806         * @param listener notified when an item on the list is clicked. The dialog will not be807         *        dismissed when an item is clicked. It will only be dismissed if clicked on a808         *        button, if no buttons are supplied it's up to the user to dismiss the dialog.809         *810         * @return This Builder object to allow for chaining of calls to set methods811         */812        public Builder setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener) {813            P.mItems = items;814            P.mOnClickListener = listener;815            P.mCheckedItem = checkedItem;816            P.mIsSingleChoice = true;817            return this;818        }819820        /**821         * Set a list of items to be displayed in the dialog as the content, you will be notified of822         * the selected item via the supplied listener. The list will have a check mark displayed to823         * the right of the text for the checked item. Clicking on an item in the list will not824         * dismiss the dialog. Clicking on a button will dismiss the dialog.825         *826         * @param adapter The {@link ListAdapter} to supply the list of items827         * @param checkedItem specifies which item is checked. If -1 no items are checked.828         * @param listener notified when an item on the list is clicked. The dialog will not be829         *        dismissed when an item is clicked. It will only be dismissed if clicked on a830         *        button, if no buttons are supplied it's up to the user to dismiss the dialog.831         *832         * @return This Builder object to allow for chaining of calls to set methods833         */834        public Builder setSingleChoiceItems(ListAdapter adapter, int checkedItem, final OnClickListener listener) {835            P.mAdapter = adapter;836            P.mOnClickListener = listener;837            P.mCheckedItem = checkedItem;838            P.mIsSingleChoice = true;839            return this;840        }841842        /**843         * Sets a listener to be invoked when an item in the list is selected.844         *845         * @param listener The listener to be invoked.846         * @see AdapterView#setOnItemSelectedListener(android.widget.AdapterView.OnItemSelectedListener)847         *848         * @return This Builder object to allow for chaining of calls to set methods849         */850        public Builder setOnItemSelectedListener(final AdapterView.OnItemSelectedListener listener) {851            P.mOnItemSelectedListener = listener;852            return this;853        }854855        /**856         * Set a custom view to be the contents of the Dialog. If the supplied view is an instance857         * of a {@link ListView} the light background will be used.858         *859         * @param view The view to use as the contents of the Dialog.860         *861         * @return This Builder object to allow for chaining of calls to set methods862         */863        public Builder setView(View view) {864            P.mView = view;865            P.mViewSpacingSpecified = false;866            return this;867        }868869        /**870         * Set a custom view to be the contents of the Dialog, specifying the871         * spacing to appear around that view. If the supplied view is an872         * instance of a {@link ListView} the light background will be used.873         *874         * @param view The view to use as the contents of the Dialog.875         * @param viewSpacingLeft Spacing between the left edge of the view and876         *        the dialog frame877         * @param viewSpacingTop Spacing between the top edge of the view and878         *        the dialog frame879         * @param viewSpacingRight Spacing between the right edge of the view880         *        and the dialog frame881         * @param viewSpacingBottom Spacing between the bottom edge of the view882         *        and the dialog frame883         * @return This Builder object to allow for chaining of calls to set884         *         methods885         *886         *887         * This is currently hidden because it seems like people should just888         * be able to put padding around the view.889         * @hide890         */891        public Builder setView(View view, int viewSpacingLeft, int viewSpacingTop,892                int viewSpacingRight, int viewSpacingBottom) {893            P.mView = view;894            P.mViewSpacingSpecified = true;895            P.mViewSpacingLeft = viewSpacingLeft;896            P.mViewSpacingTop = viewSpacingTop;897            P.mViewSpacingRight = viewSpacingRight;898            P.mViewSpacingBottom = viewSpacingBottom;899            return this;900        }901902        /**903         * Sets the Dialog to use the inverse background, regardless of what the904         * contents is.905         *906         * @param useInverseBackground Whether to use the inverse background907         *908         * @return This Builder object to allow for chaining of calls to set methods909         */910        public Builder setInverseBackgroundForced(boolean useInverseBackground) {911            P.mForceInverseBackground = useInverseBackground;912            return this;913        }914915        /**916         * @hide917         */918        public Builder setRecycleOnMeasureEnabled(boolean enabled) {919            P.mRecycleOnMeasure = enabled;920            return this;921        }922923924        /**925         * Creates a {@link AlertDialog} with the arguments supplied to this builder. It does not926         * {@link Dialog#show()} the dialog. This allows the user to do any extra processing927         * before displaying the dialog. Use {@link #show()} if you don't have any other processing928         * to do and want this to be created and displayed.929         */930        public AlertDialog create() {931            final AlertDialog dialog = new AlertDialog(P.mContext, mTheme, false);932            P.apply(dialog.mAlert);933            dialog.setCancelable(P.mCancelable);934            if (P.mCancelable) {935                dialog.setCanceledOnTouchOutside(true);936            }937            dialog.setOnCancelListener(P.mOnCancelListener);938            dialog.setOnDismissListener(P.mOnDismissListener);939            if (P.mOnKeyListener != null) {940                dialog.setOnKeyListener(P.mOnKeyListener);941            }942            return dialog;943        }944945        /**946         * Creates a {@link AlertDialog} with the arguments supplied to this builder and947         * {@link Dialog#show()}'s the dialog.948         */949        public AlertDialog show() {950            AlertDialog dialog = create();951            dialog.show();952            return dialog;953        }954    }955956}
AlertDialog  布局文件
<LinearLayout21    xmlns:android="http://schemas.android.com/apk/res/android"22    android:id="@+id/parentPanel"23    android:layout_width="match_parent"24    android:layout_height="wrap_content"25    android:layout_marginStart="8dip"26    android:layout_marginEnd="8dip"27    android:orientation="vertical">2829    <LinearLayout android:id="@+id/topPanel"30        android:layout_width="match_parent"31        android:layout_height="wrap_content"32        android:orientation="vertical">33        <View android:id="@+id/titleDividerTop"34            android:layout_width="match_parent"35            android:layout_height="2dip"36            android:visibility="gone"37            android:background="@android:color/holo_blue_light" />38        <LinearLayout android:id="@+id/title_template"39            android:layout_width="match_parent"40            android:layout_height="wrap_content"41            android:orientation="horizontal"42            android:gravity="center_vertical|start"43            android:minHeight="@dimen/alert_dialog_title_height"44            android:layout_marginStart="16dip"45            android:layout_marginEnd="16dip">46            <ImageView android:id="@+id/icon"47                android:layout_width="wrap_content"48                android:layout_height="wrap_content"49                android:paddingEnd="8dip"50                android:src="@null" />51            <com.android.internal.widget.DialogTitle android:id="@+id/alertTitle"52                style="?android:attr/windowTitleStyle"53                android:singleLine="true"54                android:ellipsize="end"55                android:layout_width="match_parent"56                android:layout_height="wrap_content"57                android:textAlignment="viewStart" />58        </LinearLayout>59        <View android:id="@+id/titleDivider"60            android:layout_width="match_parent"61            android:layout_height="2dip"62            android:visibility="gone"63            android:background="@android:color/holo_blue_light" />64        <!-- If the client uses a customTitle, it will be added here. -->65    </LinearLayout>6667    <LinearLayout android:id="@+id/contentPanel"68        android:layout_width="match_parent"69        android:layout_height="wrap_content"70        android:layout_weight="1"71        android:orientation="vertical"72        android:minHeight="64dp">73        <ScrollView android:id="@+id/scrollView"74            android:layout_width="match_parent"75            android:layout_height="wrap_content"76            android:clipToPadding="false">77            <TextView android:id="@+id/message"78                style="?android:attr/textAppearanceMedium"79                android:layout_width="match_parent"80                android:layout_height="wrap_content"81                android:paddingStart="16dip"82                android:paddingEnd="16dip"83                android:paddingTop="8dip"84                android:paddingBottom="8dip"/>85        </ScrollView>86    </LinearLayout>8788    <FrameLayout android:id="@+id/customPanel"89        android:layout_width="match_parent"90        android:layout_height="wrap_content"91        android:layout_weight="1"92        android:minHeight="64dp">93        <FrameLayout android:id="@+android:id/custom"94            android:layout_width="match_parent"95            android:layout_height="wrap_content" />96    </FrameLayout>9798    <LinearLayout android:id="@+id/buttonPanel"99        android:layout_width="match_parent"100        android:layout_height="wrap_content"101        android:minHeight="@dimen/alert_dialog_button_bar_height"102        android:orientation="vertical"103        android:divider="?android:attr/dividerHorizontal"104        android:showDividers="beginning"105        android:dividerPadding="0dip">106        <LinearLayout107            style="?android:attr/buttonBarStyle"108            android:layout_width="match_parent"109            android:layout_height="wrap_content"110            android:orientation="horizontal"111            android:layoutDirection="locale"112            android:measureWithLargestChild="true">113            <Button android:id="@+id/button2"114                android:layout_width="wrap_content"115                android:layout_gravity="start"116                android:layout_weight="1"117                android:maxLines="2"118                style="?android:attr/buttonBarButtonStyle"119                android:textSize="14sp"120                android:minHeight="@dimen/alert_dialog_button_bar_height"121                android:layout_height="wrap_content" />122            <Button android:id="@+id/button3"123                android:layout_width="wrap_content"124                android:layout_gravity="center_horizontal"125                android:layout_weight="1"126                android:maxLines="2"127                style="?android:attr/buttonBarButtonStyle"128                android:textSize="14sp"129                android:minHeight="@dimen/alert_dialog_button_bar_height"130                android:layout_height="wrap_content" />131            <Button android:id="@+id/button1"132                android:layout_width="wrap_content"133                android:layout_gravity="end"134                android:layout_weight="1"135                android:maxLines="2"136                android:minHeight="@dimen/alert_dialog_button_bar_height"137                style="?android:attr/buttonBarButtonStyle"138                android:textSize="14sp"139                android:layout_height="wrap_content" />140        </LinearLayout>141     </LinearLayout>142</LinearLayout>

布局文件与AlertDialog.Builder内各方法对应


0 0
原创粉丝点击