Most visited

Recently visited

AlertDialog

public class AlertDialog
extends AppCompatDialog implements DialogInterface

java.lang.Object
   ↳ android.app.Dialog
     ↳ android.support.v7.app.AppCompatDialog
       ↳ android.support.v7.app.AlertDialog
Known Direct Subclasses


对话框的一个子类,可以显示一个,两个或三个按钮。 如果您只想在此对话框中显示一个字符串,请使用setMessage()方法。 如果您想显示更复杂的视图,请查看名为“custom”的FrameLayout并将视图添加到它:

 FrameLayout fl = (FrameLayout) findViewById(android.R.id.custom);
 fl.addView(myView, new LayoutParams(MATCH_PARENT, WRAP_CONTENT));
 

该AlertDialog类需要自动设置护理WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM根据你是否在对话框返回从真正的任何意见View.onCheckIsTextEditor() 一般而言,您希望将此设置设置为不含文本编辑器的对话框,以便将其放置在当前输入法UI的顶部。 调用onCreate(Bundle)后,可以通过强制标志为所需模式来修改此行为。

Developer Guides

有关创建对话框的更多信息,请阅读 Dialogs开发人员指南。

Summary

Nested classes

class AlertDialog.Builder

 

Inherited constants

From interface android.content.DialogInterface

Protected constructors

AlertDialog(Context context)
AlertDialog(Context context, int themeResId)

构建一个使用明确主题的AlertDialog。

AlertDialog(Context context, boolean cancelable, DialogInterface.OnCancelListener cancelListener)

Public methods

Button getButton(int whichButton)

获取对话框中使用的按钮之一。

ListView getListView()

获取对话框中使用的列表视图。

boolean onKeyDown(int keyCode, KeyEvent event)

一键被按下。

boolean onKeyUp(int keyCode, KeyEvent event)

一个关键被释放。

void setButton(int whichButton, CharSequence text, DialogInterface.OnClickListener listener)

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

void setButton(int whichButton, CharSequence text, Message msg)

设置按下按钮时要发送的消息。

void setCustomTitle(View customTitleView)
void setIcon(Drawable icon)

设置标题中使用的 Drawable

void setIcon(int resId)

如果不需要图标,请将resId设置为0。

void setIconAttribute(int attrId)

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

void setMessage(CharSequence message)

设置要显示的消息。

void setTitle(CharSequence title)

为此对话框的窗口设置标题文本。

void setView(View view, int viewSpacingLeft, int viewSpacingTop, int viewSpacingRight, int viewSpacingBottom)

将视图设置为在对话框中显示,指定在该视图周围显示的间距。

void setView(View view)

设置视图以显示在对话框中。

Protected methods

void onCreate(Bundle savedInstanceState)

onCreate(Bundle)类似,您应该使用此方法初始化对话框,其中包括调用 setContentView(View)

Inherited methods

From class android.support.v7.app.AppCompatDialog
From class android.app.Dialog
From class java.lang.Object
From interface android.support.v7.app.AppCompatCallback
From interface android.content.DialogInterface
From interface android.view.Window.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.View.OnCreateContextMenuListener

Protected constructors

AlertDialog

AlertDialog (Context context)

Parameters
context Context

AlertDialog

AlertDialog (Context context, 
                int themeResId)

构建一个使用明确主题的AlertDialog。 AlertDialog使用的实际样式是私有实现,但是您可以在此处提供要从中获取对话框样式的主题中的属性名称(例如alertDialogTheme

Parameters
context Context
themeResId int

AlertDialog

AlertDialog (Context context, 
                boolean cancelable, 
                DialogInterface.OnCancelListener cancelListener)

Parameters
context Context
cancelable boolean
cancelListener DialogInterface.OnCancelListener

Public methods

getButton

Button getButton (int whichButton)

获取对话框中使用的按钮之一。 如果指定的按钮不存在或对话框尚未完全创建(例如,通过show()create() ),则返回null。

Parameters
whichButton int: The identifier of the button that should be returned. For example, this can be BUTTON_POSITIVE.
Returns
Button The button from the dialog, or null if a button does not exist.

getListView

ListView getListView ()

获取对话框中使用的列表视图。

Returns
ListView The ListView from the dialog.

onKeyDown

boolean onKeyDown (int keyCode, 
                KeyEvent event)

一键被按下。

如果重点视图不想要这个事件,则调用此方法。

缺省实现使用了KEYCODE_BACK,以便稍后在 onKeyUp(int, KeyEvent)处理它。

Parameters
keyCode int: The value in event.getKeyCode().
event KeyEvent: Description of the key event.
Returns
boolean If you handled the event, return true. If you want to allow the event to be handled by the next receiver, return false.

onKeyUp

boolean onKeyUp (int keyCode, 
                KeyEvent event)

一个关键被释放。

缺省实现处理KEYCODE_BACK以关闭对话框。

Parameters
keyCode int: The value in event.getKeyCode().
event KeyEvent: Description of the key event.
Returns
boolean If you handled the event, return true. If you want to allow the event to be handled by the next receiver, return false.

setButton

void setButton (int whichButton, 
                CharSequence text, 
                DialogInterface.OnClickListener listener)

当按下对话框的正面按钮时,设置要调用的侦听器。 如果在show()之后调用此方法,则此方法show()

Parameters
whichButton int: Which button to set the listener on, can be one of BUTTON_POSITIVE, BUTTON_NEGATIVE, or BUTTON_NEUTRAL
text CharSequence: The text to display in positive button.
listener DialogInterface.OnClickListener: The DialogInterface.OnClickListener to use.

setButton

void setButton (int whichButton, 
                CharSequence text, 
                Message msg)

设置按下按钮时要发送的消息。 如果在show()之后调用此方法,则此方法show()

Parameters
whichButton int: Which button to set the message for, can be one of BUTTON_POSITIVE, BUTTON_NEGATIVE, or BUTTON_NEUTRAL
text CharSequence: The text to display in positive button.
msg Message: The Message to be sent when clicked.

setCustomTitle

void setCustomTitle (View customTitleView)

Parameters
customTitleView View

也可以看看:

setIcon

void setIcon (Drawable icon)

设置标题中使用的 Drawable

Parameters
icon Drawable: Drawable to use as the icon or null if you don't want an icon.

setIcon

void setIcon (int resId)

如果不需要图标,请将resId设置为0。

Parameters
resId int: the resourceId of the drawable to use as the icon or 0 if you don't want an icon.

setIconAttribute

void setIconAttribute (int attrId)

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

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

setMessage

void setMessage (CharSequence message)

设置要显示的消息。

Parameters
message CharSequence: The message to display in the dialog.

setTitle

void setTitle (CharSequence title)

为此对话框的窗口设置标题文本。

Parameters
title CharSequence: The new text to display in the title.

setView

void setView (View view, 
                int viewSpacingLeft, 
                int viewSpacingTop, 
                int viewSpacingRight, 
                int viewSpacingBottom)

将视图设置为在对话框中显示,指定在该视图周围显示的间距。 如果在show()之后调用此方法,则此方法show()

Parameters
view View: The view to show in the content area of the dialog
viewSpacingLeft int: Extra space to appear to the left of view
viewSpacingTop int: Extra space to appear above view
viewSpacingRight int: Extra space to appear to the right of view
viewSpacingBottom int: Extra space to appear below view

setView

void setView (View view)

设置视图以显示在对话框中。 如果在show()之后调用此方法,则此方法show()

Parameters
view View

Protected methods

onCreate

void onCreate (Bundle savedInstanceState)

onCreate(Bundle)类似,您应该使用此方法初始化对话框,其中包括调用 setContentView(View)

Parameters
savedInstanceState Bundle: If this dialog is being reinitialized after a the hosting activity was previously shut down, holds the result from the most recent call to onSaveInstanceState(), or null if this is the first time.

Hooray!