Most visited

Recently visited

Added in API level 1

AlertDialog.Builder

public static class AlertDialog.Builder
extends Object

java.lang.Object
   ↳ android.app.AlertDialog.Builder


Summary

Public constructors

AlertDialog.Builder(Context context)

为使用默认警报对话框主题的警报对话框创建构建器。

AlertDialog.Builder(Context context, int themeResId)

为使用明确主题资源的警报对话框创建构建器。

Public methods

AlertDialog create()

使用提供给此构建器的参数创建一个 AlertDialog

Context getContext()

返回 Context其中包含此Builder创建的对话框的相应主题。

AlertDialog.Builder setAdapter(ListAdapter adapter, DialogInterface.OnClickListener listener)

设置由给定 ListAdapter提供的项目列表,作为内容显示在对话框中,将通过提供的监听器向您通知所选项目。

AlertDialog.Builder setCancelable(boolean cancelable)

设置对话框是否可取消。

AlertDialog.Builder setCursor(Cursor cursor, DialogInterface.OnClickListener listener, String labelColumn)

设置由给定的 Cursor提供的项目列表,作为内容显示在对话框中,将通过提供的监听器向您通知所选项目。

AlertDialog.Builder setCustomTitle(View customTitleView)

使用自定义视图 customTitleView设置标题。

AlertDialog.Builder setIcon(Drawable icon)

设置标题中使用的 Drawable

AlertDialog.Builder setIcon(int iconId)

设置要在标题中使用的 Drawable的资源ID。

AlertDialog.Builder setIconAttribute(int attrId)

设置由主题属性提供的图标。

AlertDialog.Builder setInverseBackgroundForced(boolean useInverseBackground)

此方法在API级别23中已弃用。此标志仅用于前材质主题。 相反,使用警报对话框主题指定窗口背景。

AlertDialog.Builder setItems(int itemsId, DialogInterface.OnClickListener listener)

设置要在对话框中显示的项目列表作为内容,将通过提供的监听器向您通知所选项目。

AlertDialog.Builder setItems(CharSequence[] items, DialogInterface.OnClickListener listener)

设置要在对话框中显示的项目列表作为内容,将通过提供的监听器向您通知所选项目。

AlertDialog.Builder setMessage(int messageId)

将消息设置为使用给定的资源ID显示。

AlertDialog.Builder setMessage(CharSequence message)

设置要显示的消息。

AlertDialog.Builder setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, DialogInterface.OnMultiChoiceClickListener listener)

设置要在对话框中显示的项目列表作为内容,将通过提供的监听器向您通知所选项目。

AlertDialog.Builder setMultiChoiceItems(Cursor cursor, String isCheckedColumn, String labelColumn, DialogInterface.OnMultiChoiceClickListener listener)

设置要在对话框中显示的项目列表作为内容,将通过提供的监听器向您通知所选项目。

AlertDialog.Builder setMultiChoiceItems(int itemsId, boolean[] checkedItems, DialogInterface.OnMultiChoiceClickListener listener)

设置要在对话框中显示的项目列表作为内容,将通过提供的监听器向您通知所选项目。

AlertDialog.Builder setNegativeButton(CharSequence text, DialogInterface.OnClickListener listener)

当按下对话框的否定按钮时,设置一个侦听器来调用。

AlertDialog.Builder setNegativeButton(int textId, DialogInterface.OnClickListener listener)

当按下对话框的否定按钮时,设置一个侦听器来调用。

AlertDialog.Builder setNeutralButton(CharSequence text, DialogInterface.OnClickListener listener)

设置一个侦听器,当按下对话框的中性按钮时被调用。

AlertDialog.Builder setNeutralButton(int textId, DialogInterface.OnClickListener listener)

设置一个侦听器,当按下对话框的中性按钮时被调用。

AlertDialog.Builder setOnCancelListener(DialogInterface.OnCancelListener onCancelListener)

设置在对话框被取消时将被调用的回调。

AlertDialog.Builder setOnDismissListener(DialogInterface.OnDismissListener onDismissListener)

设置当对话由于任何原因被解散时将被调用的回调。

AlertDialog.Builder setOnItemSelectedListener(AdapterView.OnItemSelectedListener listener)

设置在选择列表中的项目时调用侦听器。

AlertDialog.Builder setOnKeyListener(DialogInterface.OnKeyListener onKeyListener)

设置将密钥分派到对话框时将要调用的回调。

AlertDialog.Builder setPositiveButton(int textId, DialogInterface.OnClickListener listener)

当按下对话框的正面按钮时,设置一个侦听器被调用。

AlertDialog.Builder setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener)

当按下对话框的正面按钮时,设置一个侦听器被调用。

AlertDialog.Builder setSingleChoiceItems(int itemsId, int checkedItem, DialogInterface.OnClickListener listener)

设置要在对话框中显示的项目列表作为内容,将通过提供的监听器向您通知所选项目。

AlertDialog.Builder setSingleChoiceItems(ListAdapter adapter, int checkedItem, DialogInterface.OnClickListener listener)

设置要在对话框中显示的项目列表作为内容,将通过提供的监听器向您通知所选项目。

AlertDialog.Builder setSingleChoiceItems(Cursor cursor, int checkedItem, String labelColumn, DialogInterface.OnClickListener listener)

设置要在对话框中显示的项目列表作为内容,将通过提供的监听器向您通知所选项目。

AlertDialog.Builder setSingleChoiceItems(CharSequence[] items, int checkedItem, DialogInterface.OnClickListener listener)

设置要在对话框中显示的项目列表作为内容,将通过提供的监听器向您通知所选项目。

AlertDialog.Builder setTitle(CharSequence title)

设置显示在 Dialog中的 Dialog

AlertDialog.Builder setTitle(int titleId)

使用给定的资源ID设置标题。

AlertDialog.Builder setView(int layoutResId)

将自定义视图资源设置为对话框的内容。

AlertDialog.Builder setView(View view)

将自定义视图设置为警报对话框的内容。

AlertDialog show()

使用提供给此构建器的参数创建一个 AlertDialog ,并立即显示该对话框。

Inherited methods

From class java.lang.Object

Public constructors

AlertDialog.Builder

Added in API level 1
AlertDialog.Builder (Context context)

为使用默认警报对话框主题的警报对话框创建构建器。

默认的警告对话框的主题是通过定义 alertDialogTheme在父 context的主题。

Parameters
context Context: the parent context

AlertDialog.Builder

Added in API level 11
AlertDialog.Builder (Context context, 
                int themeResId)

为使用明确主题资源的警报对话框创建构建器。

指定的主题资源( themeResId )应用于父级context的主题context 它可以被指定为包含完全填充的主题(如Theme_Material_Dialog的样式资源,以替换父级context的主题(包括主色和重点色)中的所有属性。

要保留诸如主要和重点颜色之themeResId属性, themeResId可以改为指定为覆盖主题,例如ThemeOverlay_Material_Dialog 这将仅覆盖将警报窗口设置为对话框所需的窗口属性。

可替代地, themeResId可被指定为 0使用父 context解析值的为 alertDialogTheme

Parameters
context Context: the parent context
themeResId int: the resource ID of the theme against which to inflate this dialog, or 0 to use the parent context's default alert dialog theme

Public methods

create

Added in API level 1
AlertDialog create ()

使用提供给此构建器的参数创建一个 AlertDialog

调用此方法不会显示对话框。 如果不需要额外的处理,则可以调用show()来创建和显示对话框。

Returns
AlertDialog

getContext

Added in API level 11
Context getContext ()

返回Context其中包含此Builder创建的对话框的相应主题。 应用程序应该使用此上下文来获取LayoutInflaters,以便对将在结果对话框中使用的视图进行充气,因为这会使视图充满正确的主题。

Returns
Context A Context for built Dialogs.

setAdapter

Added in API level 1
AlertDialog.Builder setAdapter (ListAdapter adapter, 
                DialogInterface.OnClickListener listener)

设置由给定的 ListAdapter提供的项目列表,作为内容显示在对话框中,将通过提供的监听器向您通知所选项目。

Parameters
adapter ListAdapter: The ListAdapter to supply the list of items
listener DialogInterface.OnClickListener: The listener that will be called when an item is clicked.
Returns
AlertDialog.Builder This Builder object to allow for chaining of calls to set methods

setCancelable

Added in API level 1
AlertDialog.Builder setCancelable (boolean cancelable)

设置对话框是否可取消。 默认值是true。

Parameters
cancelable boolean
Returns
AlertDialog.Builder This Builder object to allow for chaining of calls to set methods

setCursor

Added in API level 1
AlertDialog.Builder setCursor (Cursor cursor, 
                DialogInterface.OnClickListener listener, 
                String labelColumn)

设置由给定的 Cursor提供的项目列表,作为内容显示在对话框中,您将通过提供的监听器通知所选项目。

Parameters
cursor Cursor: The Cursor to supply the list of items
listener DialogInterface.OnClickListener: The listener that will be called when an item is clicked.
labelColumn String: The column name on the cursor containing the string to display in the label.
Returns
AlertDialog.Builder This Builder object to allow for chaining of calls to set methods

setCustomTitle

Added in API level 1
AlertDialog.Builder setCustomTitle (View customTitleView)

使用自定义视图 customTitleView设置标题。

方法setTitle(int)setIcon(int)应该足以满足大多数标题,但如果标题需要更多定制,则会提供此方法。 使用这将通过其他方法替换标题和图标集。

注意:为确保一致的样式,自定义视图应使用通过 getContext()获取的警报对话框的主题上下文进行膨胀或构造。

Parameters
customTitleView View: the custom view to use as the title
Returns
AlertDialog.Builder this Builder object to allow for chaining of calls to set methods

setIcon

Added in API level 1
AlertDialog.Builder setIcon (Drawable icon)

设置标题中使用的 Drawable

注意:为确保一致的样式,应使用通过 getContext()获取的警报对话框的主题上下文来 getContext()

Parameters
icon Drawable
Returns
AlertDialog.Builder this Builder object to allow for chaining of calls to set methods

setIcon

Added in API level 1
AlertDialog.Builder setIcon (int iconId)

设置要在标题中使用的 Drawable的资源ID。

优先于使用 setIcon(Drawable)设置的值。

Parameters
iconId int
Returns
AlertDialog.Builder This Builder object to allow for chaining of calls to set methods

setIconAttribute

Added in API level 11
AlertDialog.Builder setIconAttribute (int attrId)

设置由主题属性提供的图标。 例如alertDialogIcon

优先于使用 setIcon(int)setIcon(Drawable)设置的值。

Parameters
attrId int: ID of a theme attribute that points to a drawable resource.
Returns
AlertDialog.Builder

setInverseBackgroundForced

Added in API level 1
AlertDialog.Builder setInverseBackgroundForced (boolean useInverseBackground)

此方法在API级别23中已弃用。
此标志仅用于前材质主题。 相反,使用警报对话框主题指定窗口背景。

无论内容是什么,都将警报对话框设置为使用逆背景。

Parameters
useInverseBackground boolean: whether to use the inverse background
Returns
AlertDialog.Builder this Builder object to allow for chaining of calls to set methods

setItems

Added in API level 1
AlertDialog.Builder setItems (int itemsId, 
                DialogInterface.OnClickListener listener)

设置要在对话框中显示的项目列表作为内容,将通过提供的监听器向您通知所选项目。 这应该是一个数组类型,即R.array.foo

Parameters
itemsId int
listener DialogInterface.OnClickListener
Returns
AlertDialog.Builder This Builder object to allow for chaining of calls to set methods

setItems

Added in API level 1
AlertDialog.Builder setItems (CharSequence[] items, 
                DialogInterface.OnClickListener listener)

设置要在对话框中显示的项目列表作为内容,将通过提供的监听器向您通知所选项目。

Parameters
items CharSequence
listener DialogInterface.OnClickListener
Returns
AlertDialog.Builder This Builder object to allow for chaining of calls to set methods

setMessage

Added in API level 1
AlertDialog.Builder setMessage (int messageId)

将消息设置为使用给定的资源ID显示。

Parameters
messageId int
Returns
AlertDialog.Builder This Builder object to allow for chaining of calls to set methods

setMessage

Added in API level 1
AlertDialog.Builder setMessage (CharSequence message)

设置要显示的消息。

Parameters
message CharSequence
Returns
AlertDialog.Builder This Builder object to allow for chaining of calls to set methods

setMultiChoiceItems

Added in API level 1
AlertDialog.Builder setMultiChoiceItems (CharSequence[] items, 
                boolean[] checkedItems, 
                DialogInterface.OnMultiChoiceClickListener listener)

设置要在对话框中显示的项目列表作为内容,将通过提供的监听器向您通知所选项目。 该列表将在每个检查项目的文本右侧显示一个复选标记。 点击列表中的项目不会消除对话框。 点击一个按钮将关闭对话框。

Parameters
items CharSequence: the text of the items to be displayed in the list.
checkedItems boolean: specifies which items are checked. It should be null in which case no items are checked. If non null it must be exactly the same length as the array of items.
listener DialogInterface.OnMultiChoiceClickListener: notified when an item on the list is clicked. The dialog will not be dismissed when an item is clicked. It will only be dismissed if clicked on a button, if no buttons are supplied it's up to the user to dismiss the dialog.
Returns
AlertDialog.Builder This Builder object to allow for chaining of calls to set methods

setMultiChoiceItems

Added in API level 1
AlertDialog.Builder setMultiChoiceItems (Cursor cursor, 
                String isCheckedColumn, 
                String labelColumn, 
                DialogInterface.OnMultiChoiceClickListener listener)

设置要在对话框中显示的项目列表作为内容,将通过提供的监听器向您通知所选项目。 该列表将在每个检查项目的文本右侧显示一个复选标记。 点击列表中的项目不会消除对话框。 点击一个按钮将关闭对话框。

Parameters
cursor Cursor: the cursor used to provide the items.
isCheckedColumn String: 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.
labelColumn String: The column name on the cursor containing the string to display in the label.
listener DialogInterface.OnMultiChoiceClickListener: notified when an item on the list is clicked. The dialog will not be dismissed when an item is clicked. It will only be dismissed if clicked on a button, if no buttons are supplied it's up to the user to dismiss the dialog.
Returns
AlertDialog.Builder This Builder object to allow for chaining of calls to set methods

setMultiChoiceItems

Added in API level 1
AlertDialog.Builder setMultiChoiceItems (int itemsId, 
                boolean[] checkedItems, 
                DialogInterface.OnMultiChoiceClickListener listener)

设置要在对话框中显示的项目列表作为内容,将通过提供的监听器向您通知所选项目。 这应该是一个数组类型,例如R.array.foo。 该列表将在每个检查项目的文本右侧显示一个复选标记。 点击列表中的项目不会消除对话框。 点击一个按钮将关闭对话框。

Parameters
itemsId int: the resource id of an array i.e. R.array.foo
checkedItems boolean: specifies which items are checked. It should be null in which case no items are checked. If non null it must be exactly the same length as the array of items.
listener DialogInterface.OnMultiChoiceClickListener: notified when an item on the list is clicked. The dialog will not be dismissed when an item is clicked. It will only be dismissed if clicked on a button, if no buttons are supplied it's up to the user to dismiss the dialog.
Returns
AlertDialog.Builder This Builder object to allow for chaining of calls to set methods

setNegativeButton

Added in API level 1
AlertDialog.Builder setNegativeButton (CharSequence text, 
                DialogInterface.OnClickListener listener)

当按下对话框的否定按钮时,设置一个侦听器来调用。

Parameters
text CharSequence: The text to display in the negative button
listener DialogInterface.OnClickListener: The DialogInterface.OnClickListener to use.
Returns
AlertDialog.Builder This Builder object to allow for chaining of calls to set methods

setNegativeButton

Added in API level 1
AlertDialog.Builder setNegativeButton (int textId, 
                DialogInterface.OnClickListener listener)

当按下对话框的否定按钮时,设置一个侦听器来调用。

Parameters
textId int: The resource id of the text to display in the negative button
listener DialogInterface.OnClickListener: The DialogInterface.OnClickListener to use.
Returns
AlertDialog.Builder This Builder object to allow for chaining of calls to set methods

setNeutralButton

Added in API level 1
AlertDialog.Builder setNeutralButton (CharSequence text, 
                DialogInterface.OnClickListener listener)

设置一个侦听器,当按下对话框的中性按钮时被调用。

Parameters
text CharSequence: The text to display in the neutral button
listener DialogInterface.OnClickListener: The DialogInterface.OnClickListener to use.
Returns
AlertDialog.Builder This Builder object to allow for chaining of calls to set methods

setNeutralButton

Added in API level 1
AlertDialog.Builder setNeutralButton (int textId, 
                DialogInterface.OnClickListener listener)

设置一个侦听器,当按下对话框的中性按钮时被调用。

Parameters
textId int: The resource id of the text to display in the neutral button
listener DialogInterface.OnClickListener: The DialogInterface.OnClickListener to use.
Returns
AlertDialog.Builder This Builder object to allow for chaining of calls to set methods

setOnCancelListener

Added in API level 1
AlertDialog.Builder setOnCancelListener (DialogInterface.OnCancelListener onCancelListener)

设置在对话框被取消时将被调用的回调。

即使在可撤销的对话框中,对话也可能因为除被取消之外的原因或选择的提供的选项之一而被解除。 如果您有兴趣听取对话被解散的所有情况,而不是仅在取消对话时setOnDismissListener ,请参阅setOnDismissListener

Parameters
onCancelListener DialogInterface.OnCancelListener
Returns
AlertDialog.Builder This Builder object to allow for chaining of calls to set methods

也可以看看:

setOnDismissListener

Added in API level 17
AlertDialog.Builder setOnDismissListener (DialogInterface.OnDismissListener onDismissListener)

设置当对话由于任何原因被解散时将被调用的回调。

Parameters
onDismissListener DialogInterface.OnDismissListener
Returns
AlertDialog.Builder This Builder object to allow for chaining of calls to set methods

setOnItemSelectedListener

Added in API level 1
AlertDialog.Builder setOnItemSelectedListener (AdapterView.OnItemSelectedListener listener)

设置在选择列表中的项目时调用侦听器。

Parameters
listener AdapterView.OnItemSelectedListener: the listener to be invoked
Returns
AlertDialog.Builder this Builder object to allow for chaining of calls to set methods

也可以看看:

setOnKeyListener

Added in API level 1
AlertDialog.Builder setOnKeyListener (DialogInterface.OnKeyListener onKeyListener)

设置将密钥分派到对话框时将要调用的回调。

Parameters
onKeyListener DialogInterface.OnKeyListener
Returns
AlertDialog.Builder This Builder object to allow for chaining of calls to set methods

setPositiveButton

Added in API level 1
AlertDialog.Builder setPositiveButton (int textId, 
                DialogInterface.OnClickListener listener)

当按下对话框的正面按钮时,设置一个侦听器被调用。

Parameters
textId int: The resource id of the text to display in the positive button
listener DialogInterface.OnClickListener: The DialogInterface.OnClickListener to use.
Returns
AlertDialog.Builder This Builder object to allow for chaining of calls to set methods

setPositiveButton

Added in API level 1
AlertDialog.Builder setPositiveButton (CharSequence text, 
                DialogInterface.OnClickListener listener)

当按下对话框的正面按钮时,设置一个侦听器被调用。

Parameters
text CharSequence: The text to display in the positive button
listener DialogInterface.OnClickListener: The DialogInterface.OnClickListener to use.
Returns
AlertDialog.Builder This Builder object to allow for chaining of calls to set methods

setSingleChoiceItems

Added in API level 1
AlertDialog.Builder setSingleChoiceItems (int itemsId, 
                int checkedItem, 
                DialogInterface.OnClickListener listener)

设置要在对话框中显示的项目列表作为内容,将通过提供的监听器向您通知所选项目。 这应该是一个数组类型,即R.array.foo该列表将在选中项目的文本右侧显示一个复选标记。 点击列表中的项目不会消除对话框。 点击一个按钮将关闭对话框。

Parameters
itemsId int: the resource id of an array i.e. R.array.foo
checkedItem int: specifies which item is checked. If -1 no items are checked.
listener DialogInterface.OnClickListener: notified when an item on the list is clicked. The dialog will not be dismissed when an item is clicked. It will only be dismissed if clicked on a button, if no buttons are supplied it's up to the user to dismiss the dialog.
Returns
AlertDialog.Builder This Builder object to allow for chaining of calls to set methods

setSingleChoiceItems

Added in API level 1
AlertDialog.Builder setSingleChoiceItems (ListAdapter adapter, 
                int checkedItem, 
                DialogInterface.OnClickListener listener)

设置要在对话框中显示的项目列表作为内容,将通过提供的监听器向您通知所选项目。 该列表将在选中项目的文本右侧显示一个复选标记。 点击列表中的项目不会消除对话框。 点击一个按钮将关闭对话框。

Parameters
adapter ListAdapter: The ListAdapter to supply the list of items
checkedItem int: specifies which item is checked. If -1 no items are checked.
listener DialogInterface.OnClickListener: notified when an item on the list is clicked. The dialog will not be dismissed when an item is clicked. It will only be dismissed if clicked on a button, if no buttons are supplied it's up to the user to dismiss the dialog.
Returns
AlertDialog.Builder This Builder object to allow for chaining of calls to set methods

setSingleChoiceItems

Added in API level 1
AlertDialog.Builder setSingleChoiceItems (Cursor cursor, 
                int checkedItem, 
                String labelColumn, 
                DialogInterface.OnClickListener listener)

设置要在对话框中显示的项目列表作为内容,将通过提供的监听器向您通知所选项目。 该列表将在选中项目的文本右侧显示一个复选标记。 点击列表中的项目不会消除对话框。 点击一个按钮将关闭对话框。

Parameters
cursor Cursor: the cursor to retrieve the items from.
checkedItem int: specifies which item is checked. If -1 no items are checked.
labelColumn String: The column name on the cursor containing the string to display in the label.
listener DialogInterface.OnClickListener: notified when an item on the list is clicked. The dialog will not be dismissed when an item is clicked. It will only be dismissed if clicked on a button, if no buttons are supplied it's up to the user to dismiss the dialog.
Returns
AlertDialog.Builder This Builder object to allow for chaining of calls to set methods

setSingleChoiceItems

Added in API level 1
AlertDialog.Builder setSingleChoiceItems (CharSequence[] items, 
                int checkedItem, 
                DialogInterface.OnClickListener listener)

设置要在对话框中显示的项目列表作为内容,将通过提供的监听器向您通知所选项目。 该列表将在选中项目的文本右侧显示一个复选标记。 点击列表中的项目不会消除对话框。 点击一个按钮将关闭对话框。

Parameters
items CharSequence: the items to be displayed.
checkedItem int: specifies which item is checked. If -1 no items are checked.
listener DialogInterface.OnClickListener: notified when an item on the list is clicked. The dialog will not be dismissed when an item is clicked. It will only be dismissed if clicked on a button, if no buttons are supplied it's up to the user to dismiss the dialog.
Returns
AlertDialog.Builder This Builder object to allow for chaining of calls to set methods

setTitle

Added in API level 1
AlertDialog.Builder setTitle (CharSequence title)

设置显示在 Dialog中的 Dialog

Parameters
title CharSequence
Returns
AlertDialog.Builder This Builder object to allow for chaining of calls to set methods

setTitle

Added in API level 1
AlertDialog.Builder setTitle (int titleId)

使用给定的资源ID设置标题。

Parameters
titleId int
Returns
AlertDialog.Builder This Builder object to allow for chaining of calls to set methods

setView

Added in API level 21
AlertDialog.Builder setView (int layoutResId)

将自定义视图资源设置为对话框的内容。 资源将被夸大,将所有顶级视图添加到屏幕上。

Parameters
layoutResId int: Resource ID to be inflated.
Returns
AlertDialog.Builder this Builder object to allow for chaining of calls to set methods

setView

Added in API level 1
AlertDialog.Builder setView (View view)

将自定义视图设置为警报对话框的内容。

在使用Holo主题前,如果提供的视图是 ListView的实例,则将使用浅色背景。

注意:为确保一致的样式,自定义视图应使用通过 getContext()获取的警报对话框的主题上下文进行膨胀或构造。

Parameters
view View: the view to use as the contents of the alert dialog
Returns
AlertDialog.Builder this Builder object to allow for chaining of calls to set methods

show

Added in API level 1
AlertDialog show ()

使用提供给此构建器的参数创建 AlertDialog ,并立即显示对话框。

调用此方法在功能上与以下内容相同:

     AlertDialog dialog = builder.create();
     dialog.show();
 

Returns
AlertDialog

Hooray!