@Override
publicvoidonClick(View v) {
switch(v.getId()){
case R.id.btn1://确定取消对话框
//1获取一个对话框的创建器
AlertDialog.Builder builder=newBuilder(MainActivity.this);
//2所有builder设置一些参数
builder.setTitle("对话框标题");
builder.setMessage("提示是否退出");
builder.setPositiveButton("确定", newDialogInterface.OnClickListener() {
publicvoid onClick(DialogInterface dialog, intwhich) {
Toast.makeText(MainActivity.this, "确定按钮被点击",1).show();
}
});
builder.setNeutralButton("取消", newDialogInterface.OnClickListener() {
publicvoid onClick(DialogInterface dialog, intwhich) {
Toast.makeText(MainActivity.this, "取消按钮被点击",1).show();
}
});
builder.create().show();
break;
case R.id.btn2://pick对话框
AlertDialog.Builder builder2=newBuilder(MainActivity.this);
builder2.setTitle("选择一个人");
final String[] arr=new String[]{"张三","李四","王五"};
builder2.setItems(arr, newDialogInterface.OnClickListener() {
publicvoid onClick(DialogInterface dialog, intwhich) {
Toast.makeText(MainActivity.this, arr[which], 1).show();
}
});
builder2.create().show();
break;
case R.id.btn3://带选择的单选按钮的对话框
AlertDialog.Builder builder3=newBuilder(MainActivity.this);
builder3.setTitle("选择一个颜色");
final String[] items=new String[]{"蓝色","黄色","红色"};
builder3.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() {
publicvoid onClick(DialogInterface dialog, intwhich) {
Toast.makeText(MainActivity.this, items[which], 1).show();
}
});
builder3.create().show();
break;
case R.id.btn4:
AlertDialog.Builder builder4=newBuilder(MainActivity.this);
builder4.setTitle("选择若干个颜色");
final String[] items4=new String[]{"蓝色","黄色","红色"};
builder4.setMultiChoiceItems(items4, newboolean[]{false,false,false}, new DialogInterface.OnMultiChoiceClickListener() {
publicvoid onClick(DialogInterface dialog, intwhich, booleanisChecked) {
Toast.makeText(MainActivity.this, items4[which]+"选择状态"+isChecked, 1).show();
}
});
builder4.setPositiveButton("确定",newDialogInterface.OnClickListener() {
publicvoid onClick(DialogInterface dialog, intwhich) {
}
});
builder4.create().show();
break;
case R.id.btn5://进度条的对话框
ProgressDialog pd=newProgressDialog(this);
pd.setTitle("提示");
pd.setMessage("正在加载中,请稍后...");
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置带进度条的
pd.setMax(100);
pd.show();
pd.setProgress(50);
break;
}
AlertDialog源码
定义一个简单的对话框,只有一个或几个按钮,显示一个字符串,可以使用setMessage()方法,其他复杂一点的视图可以添加一个定义的View给它.
* FrameLayout fl =(FrameLayout) findViewById(android.R.id.custom);
* fl.addView(myView, newLayoutParams(MATCH_PARENT, WRAP_CONTENT));
*
* <p>TheAlertDialog class takes care of automatically setting
* {@link WindowManager.LayoutParams#FLAG_ALT_FOCUSABLE_IM
*WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM} for you based on whether
* any views in thedialog return true from {@linkView#onCheckIsTextEditor()
*View.onCheckIsTextEditor()}. Generally you want this set for a Dialog
* without text editors, sothat it will be placed on top of the current
* input method UI. You can modify this behavior by forcing theflag to your
* desired modeafter calling {@link #onCreate}.
*
* <div class="specialreference">
* <h3>DeveloperGuides</h3>
* <p>For moreinformation about creating dialogs, read the
* <ahref="{@docRoot}guide/topics/ui/dialogs.html">Dialogs</a> developerguide.</p>
* </div>
*/
publicclassAlertDialog extends Dialog implements DialogInterface {
privateAlertController mAlert;
/**
* Specialtheme constant for {@link#AlertDialog(Context, int)}: use
* thetraditional (pre-Holo) alert dialog theme.
*/
publicstaticfinalintTHEME_TRADITIONAL = 1;
/**
* Specialtheme constant for {@link#AlertDialog(Context, int)}: use
* the holographic alerttheme with a dark background.
*/
publicstaticfinalintTHEME_HOLO_DARK = 2;
/**
* Specialtheme constant for {@link#AlertDialog(Context, int)}: use
* the holographic alerttheme with a light background.
*/
publicstaticfinalintTHEME_HOLO_LIGHT = 3;
/**
* Specialtheme constant for {@link#AlertDialog(Context, int)}: use
* the device's defaultalert theme with a dark background.
*/
publicstaticfinalintTHEME_DEVICE_DEFAULT_DARK = 4;
/**
* Specialtheme constant for {@link#AlertDialog(Context, int)}: use
* the device's defaultalert theme with a light background.
*/
publicstaticfinalintTHEME_DEVICE_DEFAULT_LIGHT = 5;
/**
* No layout hint.
* @hide
*/
publicstaticfinalintLAYOUT_HINT_NONE = 0;
/**
* Hint layout to theside.
* @hide
*/
publicstaticfinalintLAYOUT_HINT_SIDE = 1;
-------------------------------构造方法------------------------------------
protectedAlertDialog(Context context) {
this(context, resolveDialogTheme(context, 0), true);
}
/**
* Construct anAlertDialog that uses an explicit theme. The actual style
* that an AlertDialoguses is a private implementation, however you can
* here supply either thename of an attribute in the theme from which
* to get thedialog's style (such as {@linkandroid.R.attr#alertDialogTheme}
* or one ofthe constants {@link #THEME_TRADITIONAL},
* {@link #THEME_HOLO_DARK},or {@link #THEME_HOLO_LIGHT}.
*/
protectedAlertDialog(Context context, inttheme) {
this(context, theme, true);
}
AlertDialog(Context context, inttheme, booleancreateThemeContextWrapper) {
super(context, resolveDialogTheme(context, theme), createThemeContextWrapper);
mWindow.alwaysReadCloseOnTouchAttr();
mAlert = newAlertController(getContext(), this, getWindow());
}
protectedAlertDialog(Context context, booleancancelable, OnCancelListener cancelListener) {
super(context, resolveDialogTheme(context, 0));
mWindow.alwaysReadCloseOnTouchAttr();
setCancelable(cancelable);
setOnCancelListener(cancelListener);
mAlert = newAlertController(context, this, getWindow());
}
-----------------------静态方法返回对话框主题------------------------------
staticintresolveDialogTheme(Context context, intresid) {
if (resid == THEME_TRADITIONAL) {
returncom.android.internal.R.style.Theme_Dialog_Alert;
} elseif (resid == THEME_HOLO_DARK) {
returncom.android.internal.R.style.Theme_Holo_Dialog_Alert;
} elseif (resid == THEME_HOLO_LIGHT) {
returncom.android.internal.R.style.Theme_Holo_Light_Dialog_Alert;
} elseif (resid == THEME_DEVICE_DEFAULT_DARK) {
returncom.android.internal.R.style.Theme_DeviceDefault_Dialog_Alert;
} elseif (resid == THEME_DEVICE_DEFAULT_LIGHT) {
returncom.android.internal.R.style.Theme_DeviceDefault_Light_Dialog_Alert;
} elseif (resid >= 0x01000000){ //start of real resource IDs.
returnresid;
} else {
TypedValue outValue = new TypedValue();
context.getTheme().resolveAttribute(com.android.internal.R.attr.alertDialogTheme,
outValue, true);
returnoutValue.resourceId;
}
}
-------------------获取对话框中指定的一个按钮------------------------
/**
* Gets one of the buttonsused in the dialog. Returns null if the specified
* button does not existor the dialog has not yet been fully created (for
* example, via{@link #show()} or {@link #create()}).
*
* @paramwhichButton The identifier of the button that should be returned.
* For example, this can be
* {@linkDialogInterface#BUTTON_POSITIVE}.
* @returnThe button from the dialog, or null if a button does not exist.
*/
publicButton getButton(intwhichButton) {
returnmAlert.getButton(whichButton);
}
-------------------获取对话框中使用的ListView------------------------
/**
* Gets the list view usedin the dialog.
*
* @returnThe {@link ListView} from the dialog.
*/
publicListView getListView() {
returnmAlert.getListView();
}
-------------------设置标题------------------------
@Override
publicvoidsetTitle(CharSequence title) {
super.setTitle(title);
mAlert.setTitle(title);
}
-------------------设置传统标题------------------------
/**
* @seeBuilder#setCustomTitle(View)
*/
publicvoidsetCustomTitle(View customTitleView) {
mAlert.setCustomTitle(customTitleView);
}
-------------------设置信息字符串------------------------
publicvoidsetMessage(CharSequence message) {
mAlert.setMessage(message);
}
-------------------设置View------------------------
/**
* Set the view to displayin that dialog.
*/
publicvoidsetView(View view) {
mAlert.setView(view);
}
-------------------设置View(其它参数 左 上 右 下的距离)------------------------
/**
* Set the view to displayin that dialog, specifying the spacing to appear around that
* view.
*
* @paramview The view to show in the content area of the dialog
* @paramviewSpacingLeft Extra space to appear to the left of {@code view}
* @paramviewSpacingTop Extra space to appear above {@code view}
* @paramviewSpacingRight Extra space to appear to the right of {@code view}
* @paramviewSpacingBottom Extra space to appear below {@code view}
*/
publicvoidsetView(View view, intviewSpacingLeft, intviewSpacingTop, intviewSpacingRight,intviewSpacingBottom) {
mAlert.setView(view, viewSpacingLeft, viewSpacingTop, viewSpacingRight, viewSpacingBottom);
}
-------------------设置button面板------------------------
/**
* Internal api to allowhinting for the best button panel layout.
* @hide
*/
voidsetButtonPanelLayoutHint(intlayoutHint) {
mAlert.setButtonPanelLayoutHint(layoutHint);
}
-------------------设置指定的按钮被点击发送一个消息------------
/**
* Set a message to besent when a button is pressed.
*
* @paramwhichButton Which button to set the message for, can be one of
* {@linkDialogInterface#BUTTON_POSITIVE},
* {@linkDialogInterface#BUTTON_NEGATIVE}, or
* {@linkDialogInterface#BUTTON_NEUTRAL}
* @paramtext The text to display in positive button.
* @parammsg The {@link Message} to be sent when clicked.
*/
publicvoidsetButton(intwhichButton, CharSequence text, Message msg) {
mAlert.setButton(whichButton, text, null, msg);
}
-------------------为对话框的按钮添加监听器------------------------
/**
* Set a listener to beinvoked when the positive button of the dialog is pressed.
*
* @paramwhichButton Which button to set the listener on, can be one of
* {@linkDialogInterface#BUTTON_POSITIVE},
* {@linkDialogInterface#BUTTON_NEGATIVE}, or
* {@linkDialogInterface#BUTTON_NEUTRAL}
* @paramtext The text to display in positive button.
* @paramlistener The {@linkDialogInterface.OnClickListener} to use.
*/
publicvoidsetButton(intwhichButton, CharSequence text, OnClickListener listener) {
mAlert.setButton(whichButton, text, listener, null);
}
-------------------------------------------
/**
* @deprecated Use {@link#setButton(int, CharSequence, Message)} with
* {@linkDialogInterface#BUTTON_POSITIVE}.
*/
@Deprecated
publicvoidsetButton(CharSequence text, Message msg) {
setButton(BUTTON_POSITIVE, text, msg);
}
/**
* @deprecated Use {@link#setButton(int, CharSequence, Message)} with
* {@linkDialogInterface#BUTTON_NEGATIVE}.
*/
@Deprecated
publicvoidsetButton2(CharSequence text, Message msg) {
setButton(BUTTON_NEGATIVE, text, msg);
}
/**
* @deprecated Use {@link#setButton(int, CharSequence, Message)} with
* {@linkDialogInterface#BUTTON_NEUTRAL}.
*/
@Deprecated
publicvoidsetButton3(CharSequence text, Message msg) {
setButton(BUTTON_NEUTRAL, text, msg);
}
/**
* Set a listener to beinvoked when button 1 of the dialog is pressed.
*
* @paramtext The text to display in button 1.
* @paramlistener The {@linkDialogInterface.OnClickListener} to use.
* @deprecated Use
* {@link#setButton(int, CharSequence, android.content.DialogInterface.OnClickListener)}
* with {@link DialogInterface#BUTTON_POSITIVE}
*/
@Deprecated
publicvoidsetButton(CharSequence text, final OnClickListener listener) {
setButton(BUTTON_POSITIVE, text, listener);
}
/**
* Set a listener to beinvoked when button 2 of the dialog is pressed.
* @paramtext The text to display in button 2.
* @paramlistener The {@linkDialogInterface.OnClickListener} to use.
* @deprecated Use
* {@link#setButton(int, CharSequence, android.content.DialogInterface.OnClickListener)}
* with {@link DialogInterface#BUTTON_NEGATIVE}
*/
@Deprecated
publicvoidsetButton2(CharSequence text, final OnClickListener listener) {
setButton(BUTTON_NEGATIVE, text, listener);
}
/**
* Set a listener to beinvoked when button 3 of the dialog is pressed.
* @paramtext The text to display in button 3.
* @paramlistener The {@linkDialogInterface.OnClickListener} to use.
* @deprecated Use
* {@link#setButton(int, CharSequence, android.content.DialogInterface.OnClickListener)}
* with {@link DialogInterface#BUTTON_POSITIVE}
*/
@Deprecated
publicvoidsetButton3(CharSequence text, final OnClickListener listener) {
setButton(BUTTON_NEUTRAL, text, listener);
}
-------------------设置对话框的图标(通过资源ID)------------------------
/**
* Set resId to 0 if youdon't want an icon.
* @paramresId the resourceId of the drawable to use as the icon or 0
* if you don't want anicon.
*/
publicvoidsetIcon(intresId) {
mAlert.setIcon(resId);
}
-------------------设置对话框的图标(传入Drawable)------------------------
publicvoidsetIcon(Drawable icon) {
mAlert.setIcon(icon);
}
-------------------设置对话框的图标属性(传入主题资源ID)------------------------
/**
* Set an icon as suppliedby a theme attribute. e.g. android.R.attr.alertDialogIcon
*
* @paramattrId ID of a theme attribute that points to a drawable resource.
*/
publicvoidsetIconAttribute(intattrId) {
TypedValue out = newTypedValue();
mContext.getTheme().resolveAttribute(attrId, out, true);
mAlert.setIcon(out.resourceId);
}
publicvoidsetInverseBackgroundForced(booleanforceInverseBackground) {
mAlert.setInverseBackgroundForced(forceInverseBackground);
}
-------------------重写父类的方法------------------------
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAlert.installContent();
}
@Override
publicbooleanonKeyDown(intkeyCode, KeyEvent event) {
if (mAlert.onKeyDown(keyCode, event)) returntrue;
returnsuper.onKeyDown(keyCode, event);
}
@Override
publicbooleanonKeyUp(intkeyCode, KeyEvent event) {
if (mAlert.onKeyUp(keyCode, event)) returntrue;
returnsuper.onKeyUp(keyCode, event);
}
-------------------静态内部类(AlertDialog的创建器)------------------------
Builder 里面的setXXX()方法不是直接操作AlertDialog,而是把参数先放到 AlertController.AlertParams 对象里,在create()的时候再把AlertController.AlertParams对象传给创建的AlertDialog对象,再调用AlertDialog的一系列的初始化方法。
publicstaticclassBuilder {
privatefinal AlertController.AlertParams P;
privateintmTheme;
/**
*Constructor using a context for this builder and the {@link AlertDialog} itcreates.
*/
publicBuilder(Context context) {
this(context, resolveDialogTheme(context, 0));
}
/**
* Constructor using acontext and theme for this builder and
* the {@link AlertDialog} itcreates. The actual theme
* that an AlertDialoguses is a private implementation, however you can
* here supply eitherthe name of an attribute in the theme from which
* to getthe dialog's style (such as {@linkandroid.R.attr#alertDialogTheme}
* or one of theconstants
* {@link AlertDialog#THEME_TRADITIONALAlertDialog.THEME_TRADITIONAL},
* {@link AlertDialog#THEME_HOLO_DARKAlertDialog.THEME_HOLO_DARK}, or
* {@link AlertDialog#THEME_HOLO_LIGHTAlertDialog.THEME_HOLO_LIGHT}.
*/
publicBuilder(Context context, inttheme) {
P = newAlertController.AlertParams(new ContextThemeWrapper(
context, resolveDialogTheme(context, theme)));
mTheme = theme;
}
/**
* Returnsa {@link Context} with the appropriate theme for dialogs created by thisBuilder.
* Applications shoulduse this Context for obtaining LayoutInflaters for inflating views
* that will be usedin the resulting dialogs, as it will cause views to be inflated with
* the correct theme.
*
* @returnA Context for built Dialogs.
*/
publicContext getContext() {
returnP.mContext;
}
/**
* Set the title usingthe given resource id.
* 设置标题,通过资源ID
* @returnThis Builder object to allow for chaining of calls to set methods
*/
publicBuilder setTitle(inttitleId) {
P.mTitle = P.mContext.getText(titleId);
returnthis;
}
/**
* Set thetitle displayed in the {@link Dialog}.
* 设置标题,通过传入的字符串
* @returnThis Builder object to allow for chaining of calls to set methods
*/
publicBuilder setTitle(CharSequence title) {
P.mTitle = title;
returnthis;
}
/**
* Set the title usingthe custom view {@code customTitleView}. The
* methods {@link #setTitle(int)}and {@link #setIcon(int)} should be
* sufficient for mosttitles, but this is provided if the title needs
* more customization.Using this will replace the title and icon set
* via the othermethods.
* 设置订制标题,setTitle(int)和setIcon(int)方法设置的是一般的标题和图标,对于需要自己订制的标题可以使用这个方法,传入的是设计的标题View.
* @paramcustomTitleView The custom view to use as the title.
*
* @returnThis Builder object to allow for chaining of calls to set methods
*/
publicBuilder setCustomTitle(View customTitleView) {
P.mCustomTitleView = customTitleView;
returnthis;
}
/**
* Set the message todisplay using the given resource id.
* 设置对话框要显示的字符串(传入资源ID)
* @returnThis Builder object to allow for chaining of calls to set methods
*/
publicBuilder setMessage(intmessageId) {
P.mMessage = P.mContext.getText(messageId);
returnthis;
}
/**
* Set the message todisplay.
* 设置对话框要显示的字符串(传入字符串)
* @returnThis Builder object to allow for chaining of calls to set methods
*/
publicBuilder setMessage(CharSequence message) {
P.mMessage = message;
returnthis;
}
/**
* Set theresource id of the {@link Drawable} to be used in the title.
* 设置图标(资源ID)
* @returnThis Builder object to allow for chaining of calls to set methods
*/
publicBuilder setIcon(inticonId) {
P.mIconId = iconId;
returnthis;
}
/**
* Set the {@link Drawable} to beused in the title.
*设置图标(Drawable 对象)
* @returnThis Builder object to allow for chaining of calls to set methods
*/
publicBuilder setIcon(Drawable icon) {
P.mIcon = icon;
returnthis;
}
/**
* Set an icon assupplied by a theme attribute. e.g. android.R.attr.alertDialogIcon
*
* @paramattrId ID of a theme attribute that points to a drawable resource.
*/
publicBuilder setIconAttribute(intattrId) {
TypedValue out = new TypedValue();
P.mContext.getTheme().resolveAttribute(attrId, out, true);
P.mIconId = out.resourceId;
returnthis;
}
-------------------------------------------设置确定按钮-------------------------------------------------
/**
* Set a listener tobe invoked when the positive button of the dialog is pressed.
* @paramtextId The resource id of the text to display in the positive button
* @paramlistener The {@linkDialogInterface.OnClickListener} to use.
*
* @returnThis Builder object to allow for chaining of calls to set methods
*/
publicBuilder setPositiveButton(inttextId, final OnClickListener listener) {
P.mPositiveButtonText = P.mContext.getText(textId);
P.mPositiveButtonListener= listener;
returnthis;
}
-------------------------------------------设置确定按钮-------------------------------------------------
/**
* Set a listener tobe invoked when the positive button of the dialog is pressed.
* @paramtext The text to display in the positive button
* @paramlistener The {@linkDialogInterface.OnClickListener} to use.
*
* @returnThis Builder object to allow for chaining of calls to set methods
*/
publicBuilder setPositiveButton(CharSequence text, final OnClickListener listener) {
P.mPositiveButtonText = text;
P.mPositiveButtonListener= listener;
returnthis;
}
-------------------------------------------设置取消按钮-------------------------------------------------
/**
* Set a listener tobe invoked when the negative button of the dialog is pressed.
* @paramtextId The resource id of the text to display in the negative button
* @paramlistener The {@linkDialogInterface.OnClickListener} to use.
*
* @returnThis Builder object to allow for chaining of calls to set methods
*/
publicBuilder setNegativeButton(inttextId, final OnClickListener listener) {
P.mNegativeButtonText = P.mContext.getText(textId);
P.mNegativeButtonListener= listener;
returnthis;
}
-------------------------------------------设置取消按钮-------------------------------------------------
/**
* Set a listener tobe invoked when the negative button of the dialog is pressed.
* @paramtext The text to display in the negative button
* @paramlistener The {@linkDialogInterface.OnClickListener} to use.
*
* @returnThis Builder object to allow for chaining of calls to set methods
*/
publicBuilder setNegativeButton(CharSequence text, final OnClickListener listener) {
P.mNegativeButtonText = text;
P.mNegativeButtonListener= listener;
returnthis;
}
-------------------------------------------设置无效按钮(资源 ID)-------------------------------------------------
/**
* Set a listener tobe invoked when the neutral button of the dialog is pressed.
* @paramtextId The resource id of the text to display in the neutral button
* @paramlistener The {@linkDialogInterface.OnClickListener} to use.
*
* @returnThis Builder object to allow for chaining of calls to set methods
*/
publicBuilder setNeutralButton(inttextId,final OnClickListener listener) {
P.mNeutralButtonText = P.mContext.getText(textId);
P.mNeutralButtonListener= listener;
returnthis;
}
-------------------------------------------设置无效按钮(字符串)-------------------------------------------------
/**
* Set a listener tobe invoked when the neutral button of the dialog is pressed.
* @paramtext The text to display in the neutral button
* @paramlistener The {@linkDialogInterface.OnClickListener} to use.
*
* @returnThis Builder object to allow for chaining of calls to set methods
*/
publicBuilder setNeutralButton(CharSequencetext, final OnClickListener listener) {
P.mNeutralButtonText = text;
P.mNeutralButtonListener= listener;
returnthis;
}
-------------------------------------------设置对话框可否被取消-------------------------------------------------
/**
* Sets whether thedialog is cancelable or not. Default istrue.
*
* @returnThis Builder object to allow for chaining of calls to set methods
*/
publicBuilder setCancelable(booleancancelable) {
P.mCancelable = cancelable;
returnthis;
}
-------------------------------------------设置取消监听器-------------------------------------------------
/**
* Sets the callbackthat will be called if the dialog is canceled.
*
* <p>Even in acancelable dialog, the dialog may be dismissed for reasons other than
* being canceled orone of the supplied choices being selected.
* If you areinterested in listening for all cases where the dialog is dismissed
* and not just whenit is canceled, see
* {@link#setOnDismissListener(android.content.DialogInterface.OnDismissListener)setOnDismissListener}.</p>
* @see#setCancelable(boolean)
* @see#setOnDismissListener(android.content.DialogInterface.OnDismissListener)
*
* @returnThis Builder object to allow for chaining of calls to set methods
*/
publicBuilder setOnCancelListener(OnCancelListener onCancelListener) {
P.mOnCancelListener = onCancelListener;
returnthis;
}
-------------------------------------------设置对话框消失监听器-------------------------------------------------
/**
* Sets the callbackthat will be called when the dialog is dismissed for any reason.
*
* @returnThis Builder object to allow for chaining of calls to set methods
*/
publicBuilder setOnDismissListener(OnDismissListener onDismissListener) {
P.mOnDismissListener = onDismissListener;
returnthis;
}
-------------------------------------------设置XXX-------------------------------------------------
/**
* Sets the callbackthat will be called if a key is dispatched to the dialog.
*
* @returnThis Builder object to allow for chaining of calls to set methods
*/
publicBuilder setOnKeyListener(OnKeyListener onKeyListener) {
P.mOnKeyListener = onKeyListener;
returnthis;
}
/**
* Set a list of itemsto be displayed in the dialog as the content, you will be notified of the
* selected item viathe supplied listener. This should be an array type i.e. R.array.foo
*
* @returnThis Builder object to allow for chaining of calls to set methods
*/
publicBuilder setItems(intitemsId, final OnClickListener listener) {
P.mItems = P.mContext.getResources().getTextArray(itemsId);
P.mOnClickListener = listener;
returnthis;
}
/**
* Set a list of itemsto be displayed in the dialog as the content, you will be notified of the
* selected item viathe supplied listener.
*
* @returnThis Builder object to allow for chaining of calls to set methods
*/
publicBuilder setItems(CharSequence[] items, final OnClickListener listener) {
P.mItems = items;
P.mOnClickListener = listener;
returnthis;
}
/**
* Set alist of items, which are supplied by the given {@linkListAdapter}, to be
* displayed in thedialog as the content, you will be notified of the
* selected item viathe supplied listener.
*
* @paramadapter The {@link ListAdapter} to supply the list of items
* @paramlistener The listener that will be called when an item is clicked.
*
* @returnThis Builder object to allow for chaining of calls to set methods
*/
publicBuilder setAdapter(final ListAdapter adapter, final OnClickListener listener) {
P.mAdapter = adapter;
P.mOnClickListener = listener;
returnthis;
}
/**
* Set alist of items, which are supplied by the given {@linkCursor}, to be
* displayed in thedialog as the content, you will be notified of the
* selected item viathe supplied listener.
*
* @paramcursor The {@link Cursor} to supply the list of items
* @paramlistener The listener that will be called when an item is clicked.
* @paramlabelColumn The column name on the cursor containing the string to display
* in the label.
*
* @returnThis Builder object to allow for chaining of calls to set methods
*/
publicBuilder setCursor(final Cursor cursor, final OnClickListener listener,
String labelColumn) {
P.mCursor = cursor;
P.mLabelColumn = labelColumn;
P.mOnClickListener = listener;
returnthis;
}
/**
* Set a list of itemsto be displayed in the dialog as the content,
* you will benotified of the selected item via the supplied listener.
* This should be anarray type, e.g. R.array.foo. The list will have
* a check markdisplayed to the right of the text for each checked
* item. Clicking onan item in the list will not dismiss the dialog.
* Clicking on abutton will dismiss the dialog.
*
* @paramitemsId the resource id of an array i.e. R.array.foo
* @paramcheckedItems specifies which items are checked. It should be null in which caseno
* items are checked. If non null it mustbe exactly the same length as the array of
* items.
* @paramlistener notified when an item on the list is clicked. The dialog will not be
* dismissed when an item is clicked. Itwill only be dismissed if clicked on a
* button, if no buttons are supplied it'sup to the user to dismiss the dialog.
*
* @returnThis Builder object to allow for chaining of calls to set methods
*/
publicBuilder setMultiChoiceItems(intitemsId, boolean[] checkedItems,
finalOnMultiChoiceClickListener listener) {
P.mItems = P.mContext.getResources().getTextArray(itemsId);
P.mOnCheckboxClickListener= listener;
P.mCheckedItems = checkedItems;
P.mIsMultiChoice = true;
returnthis;
}
/**
* Set a list of itemsto be displayed in the dialog as the content,
* you will benotified of the selected item via the supplied listener.
* The list will havea check mark displayed to the right of the text
* for each checkeditem. Clicking on an item in the list will not
* dismiss the dialog.Clicking on a button will dismiss the dialog.
*
* @paramitems the text of the items to be displayed in the list.
* @paramcheckedItems specifies which items are checked. It should be null in which caseno
* items are checked. If non null it mustbe exactly the same length as the array of
* items.
* @paramlistener notified when an item on the list is clicked. The dialog will not be
* dismissed when an item is clicked. Itwill only be dismissed if clicked on a
* button, if no buttons are supplied it'sup to the user to dismiss the dialog.
*
* @returnThis Builder object to allow for chaining of calls to set methods
*/
publicBuilder setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems,
finalOnMultiChoiceClickListener listener) {
P.mItems = items;
P.mOnCheckboxClickListener= listener;
P.mCheckedItems = checkedItems;
P.mIsMultiChoice = true;
returnthis;
}
/**
* Set a list of itemsto be displayed in the dialog as the content,
* you will benotified of the selected item via the supplied listener.
* The list will havea check mark displayed to the right of the text
* for each checkeditem. Clicking on an item in the list will not
* dismiss the dialog.Clicking on a button will dismiss the dialog.
*
* @paramcursor the cursor used to provide the items.
* @paramisCheckedColumn specifies the column name on the cursor to use to determine
* whether a checkbox is checked or not.It must return an integer value where 1
* means checked and 0 means unchecked.
* @paramlabelColumn The column name on the cursor containing the string to display inthe
* label.
* @paramlistener notified when an item on the list is clicked. The dialog will not be
* dismissed when an item is clicked. Itwill only be dismissed if clicked on a
* button, if no buttons are supplied it'sup to the user to dismiss the dialog.
*
* @returnThis Builder object to allow for chaining of calls to set methods
*/
publicBuilder setMultiChoiceItems(Cursor cursor, String isCheckedColumn, String labelColumn,
finalOnMultiChoiceClickListener listener) {
P.mCursor = cursor;
P.mOnCheckboxClickListener= listener;
P.mIsCheckedColumn = isCheckedColumn;
P.mLabelColumn = labelColumn;
P.mIsMultiChoice = true;
returnthis;
}
/**
* Set a list of itemsto be displayed in the dialog as the content, you will be notified of
* the selected itemvia the supplied listener. This should be an array type i.e.
* R.array.foo Thelist will have a check mark displayed to the right of the text for the
* checked item.Clicking on an item in the list will not dismiss the dialog. Clicking on a
* button will dismissthe dialog.
*
* @paramitemsId the resource id of an array i.e. R.array.foo
* @paramcheckedItem specifies which item is checked. If -1 no items are checked.
* @paramlistener notified when an item on the list is clicked. The dialog will not be
* dismissed when an item is clicked. Itwill only be dismissed if clicked on a
* button, if no buttons are supplied it'sup to the user to dismiss the dialog.
*
* @returnThis Builder object to allow for chaining of calls to set methods
*/
publicBuilder setSingleChoiceItems(intitemsId, intcheckedItem,
finalOnClickListener listener) {
P.mItems = P.mContext.getResources().getTextArray(itemsId);
P.mOnClickListener = listener;
P.mCheckedItem = checkedItem;
P.mIsSingleChoice = true;
returnthis;
}
/**
* Set a list of itemsto be displayed in the dialog as the content, you will be notified of
* the selected itemvia the supplied listener. The list will have a check mark displayed to
* the right of thetext for the checked item. Clicking on an item in the list will not
* dismiss the dialog.Clicking on a button will dismiss the dialog.
*
* @paramcursor the cursor to retrieve the items from.
* @paramcheckedItem specifies which item is checked. If -1 no items are checked.
* @paramlabelColumn The column name on the cursor containing the string to display inthe
* label.
* @paramlistener notified when an item on the list is clicked. The dialog will not be
* dismissed when an item is clicked. Itwill only be dismissed if clicked on a
* button, if no buttons are supplied it'sup to the user to dismiss the dialog.
*
* @returnThis Builder object to allow for chaining of calls to set methods
*/
publicBuilder setSingleChoiceItems(Cursor cursor, intcheckedItem, String labelColumn,
finalOnClickListener listener) {
P.mCursor = cursor;
P.mOnClickListener = listener;
P.mCheckedItem = checkedItem;
P.mLabelColumn = labelColumn;
P.mIsSingleChoice = true;
returnthis;
}
/**
* Set a list of itemsto be displayed in the dialog as the content, you will be notified of
* the selected itemvia the supplied listener. The list will have a check mark displayed to
* the right of thetext for the checked item. Clicking on an item in the list will not
* dismiss the dialog.Clicking on a button will dismiss the dialog.
*
* @paramitems the items to be displayed.
* @paramcheckedItem specifies which item is checked. If -1 no items are checked.
* @paramlistener notified when an item on the list is clicked. The dialog will not be
* dismissed when an item is clicked. Itwill only be dismissed if clicked on a
* button, if no buttons are supplied it'sup to the user to dismiss the dialog.
*
* @returnThis Builder object to allow for chaining of calls to set methods
*/
publicBuilder setSingleChoiceItems(CharSequence[] items, intcheckedItem, final OnClickListener listener) {
P.mItems = items;
P.mOnClickListener = listener;
P.mCheckedItem = checkedItem;
P.mIsSingleChoice = true;
returnthis;
}
/**
* Set a list of itemsto be displayed in the dialog as the content, you will be notified of
* the selected itemvia the supplied listener. The list will have a check mark displayed to
* the right of thetext for the checked item. Clicking on an item in the list will not
* dismiss the dialog.Clicking on a button will dismiss the dialog.
*
* @paramadapter The {@link ListAdapter} to supply the list of items
* @paramcheckedItem specifies which item is checked. If -1 no items are checked.
* @paramlistener notified when an item on the list is clicked. The dialog will not be
* dismissed when an item is clicked. Itwill only be dismissed if clicked on a
* button, if no buttons are supplied it'sup to the user to dismiss the dialog.
*
* @returnThis Builder object to allow for chaining of calls to set methods
*/
publicBuilder setSingleChoiceItems(ListAdapter adapter, intcheckedItem, final OnClickListener listener) {
P.mAdapter = adapter;
P.mOnClickListener = listener;
P.mCheckedItem = checkedItem;
P.mIsSingleChoice = true;
returnthis;
}
/**
* Sets a listener tobe invoked when an item in the list is selected.
*
* @paramlistener The listener to be invoked.
* @seeAdapterView#setOnItemSelectedListener(android.widget.AdapterView.OnItemSelectedListener)
*
* @returnThis Builder object to allow for chaining of calls to set methods
*/
publicBuilder setOnItemSelectedListener(finalAdapterView.OnItemSelectedListener listener) {
P.mOnItemSelectedListener= listener;
returnthis;
}
/**
* Set a custom viewresource to be the contents of the Dialog. The
* resourcewill be inflated, adding all top-level views to the screen.
*
* @paramlayoutResId Resource ID to be inflated.
* @returnThis Builder object to allow for chaining of calls to set
* methods
*/
publicBuilder setView(intlayoutResId) {
P.mView = null;
P.mViewLayoutResId = layoutResId;
P.mViewSpacingSpecified= false;
returnthis;
}
/**
* Set a custom viewto be the contents of the Dialog. If the supplied view is an instance
* of a {@link ListView} thelight background will be used.
*
* @paramview The view to use as the contents of the Dialog.
*
* @returnThis Builder object to allow for chaining of calls to set methods
*/
publicBuilder setView(View view) {
P.mView = view;
P.mViewLayoutResId = 0;
P.mViewSpacingSpecified= false;
returnthis;
}
/**
* Set a custom viewto be the contents of the Dialog, specifying the
* spacing to appeararound that view. If the supplied view is an
* instanceof a {@link ListView} the light background will be used.
*
* @paramview The view to use as the contents of the Dialog.
* @paramviewSpacingLeft Spacing between the left edge of the view and
* the dialog frame
* @paramviewSpacingTop Spacing between the top edge of the view and
* the dialog frame
* @paramviewSpacingRight Spacing between the right edge of the view
* and the dialog frame
* @paramviewSpacingBottom Spacing between the bottom edge of the view
* and the dialog frame
* @returnThis Builder object to allow for chaining of calls to set
* methods
*
*
* This is currentlyhidden because it seems like people should just
* be able to putpadding around the view.
* @hide
*/
publicBuilder setView(View view, intviewSpacingLeft, intviewSpacingTop,
intviewSpacingRight, intviewSpacingBottom) {
P.mView = view;
P.mViewLayoutResId = 0;
P.mViewSpacingSpecified= true;
P.mViewSpacingLeft = viewSpacingLeft;
P.mViewSpacingTop = viewSpacingTop;
P.mViewSpacingRight = viewSpacingRight;
P.mViewSpacingBottom = viewSpacingBottom;
returnthis;
}
/**
* Sets the Dialog touse the inverse background, regardless of what the
* contents is.
*
* @paramuseInverseBackground Whether to use the inverse background
*
* @returnThis Builder object to allow for chaining of calls to set methods
*/
publicBuilder setInverseBackgroundForced(booleanuseInverseBackground) {
P.mForceInverseBackground= useInverseBackground;
returnthis;
}
/**
* @hide
*/
publicBuilder setRecycleOnMeasureEnabled(booleanenabled) {
P.mRecycleOnMeasure = enabled;
returnthis;
}
/**
* Createsa {@link AlertDialog} with the arguments supplied to this builder. It does not
* {@link Dialog#show()}the dialog. This allows the user to do any extra processing
* beforedisplaying the dialog. Use {@link #show()} if you don't have any other processing
* to do and want thisto be created and displayed.
*/
publicAlertDialog create() {
finalAlertDialog dialog = new AlertDialog(P.mContext, mTheme, 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);
}
returndialog;
}
/**
* Createsa {@link AlertDialog} with the arguments supplied to this builder and
* {@link Dialog#show()}'sthe dialog.
*/
publicAlertDialog show() {
AlertDialog dialog = create();
dialog.show();
returndialog;
}
}
}