Dialog
Dialog
implements DialogInterface KeyEvent.Callback View.OnCreateContextMenuListener Window.Callback
java.lang.Object | |
↳ | android.app.Dialog |
Known Direct Subclasses |
Known Indirect Subclasses |
Summary
[Expand]
Inherited Constants
| |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From interface android.content.DialogInterface
|
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Dialog(
Context context)
Create a Dialog window that uses the default dialog frame style.
| |||||||||||
Dialog(
Context context, int theme)
Create a Dialog window that uses a custom dialog style.
|
Protected Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Dialog( Context context, boolean cancelable, DialogInterface.OnCancelListener cancelListener) |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
void |
addContentView(
View view,
ViewGroup.LayoutParams params)
Add an additional content view to the screen.
| ||||||||||
void |
cancel()
Cancel the dialog.
| ||||||||||
void | closeOptionsMenu() | ||||||||||
void |
dismiss()
Dismiss this dialog, removing it from the screen.
| ||||||||||
boolean |
dispatchGenericMotionEvent(
MotionEvent ev)
Called to process generic motion events.
| ||||||||||
boolean |
dispatchKeyEvent(
KeyEvent event)
Called to process key events.
| ||||||||||
boolean |
dispatchKeyShortcutEvent(
KeyEvent event)
Called to process a key shortcut event.
| ||||||||||
boolean |
dispatchPopulateAccessibilityEvent(
AccessibilityEvent event)
Called to process population of
AccessibilityEvent s.
| ||||||||||
boolean |
dispatchTouchEvent(
MotionEvent ev)
Called to process touch screen events.
| ||||||||||
boolean |
dispatchTrackballEvent(
MotionEvent ev)
Called to process trackball events.
| ||||||||||
View |
findViewById(int id)
Finds a view that was identified by the id attribute from the XML that was processed in
onStart() .
| ||||||||||
ActionBar |
getActionBar()
Retrieve the
ActionBar attached to this dialog, if present.
| ||||||||||
final Context |
getContext()
Retrieve the Context this Dialog is running in.
| ||||||||||
View |
getCurrentFocus()
Call
getCurrentFocus() on the Window if this Activity to return the currently focused view.
| ||||||||||
LayoutInflater | getLayoutInflater() | ||||||||||
final Activity |
getOwnerActivity()
Returns the Activity that owns this Dialog.
| ||||||||||
final int | getVolumeControlStream() | ||||||||||
Window |
getWindow()
Retrieve the current Window for the activity.
| ||||||||||
void |
hide()
Hide the dialog, but do not dismiss it.
| ||||||||||
void | invalidateOptionsMenu() | ||||||||||
boolean | isShowing() | ||||||||||
void |
onActionModeFinished(
ActionMode mode)
Called when an action mode has been finished. Note that if you override this method you should always call through to the superclass implementation by calling super.onActionModeFinished(mode).
| ||||||||||
void |
onActionModeStarted(
ActionMode mode)
Called when an action mode has been started. Note that if you override this method you should always call through to the superclass implementation by calling super.onActionModeStarted(mode).
| ||||||||||
void |
onAttachedToWindow()
Called when the window has been attached to the window manager.
| ||||||||||
void |
onBackPressed()
Called when the dialog has detected the user's press of the back key.
| ||||||||||
void |
onContentChanged()
This hook is called whenever the content view of the screen changes (due to a call to
Window.setContentView or
Window.addContentView ).
| ||||||||||
boolean | onContextItemSelected( MenuItem item) | ||||||||||
void | onContextMenuClosed( Menu menu) | ||||||||||
void |
onCreateContextMenu(
ContextMenu menu,
View v,
ContextMenu.ContextMenuInfo menuInfo)
Called when the context menu for this view is being built.
| ||||||||||
boolean |
onCreateOptionsMenu(
Menu menu)
It is usually safe to proxy this call to the owner activity's
onCreateOptionsMenu(Menu) if the client desires the same menu for this Dialog.
| ||||||||||
boolean |
onCreatePanelMenu(int featureId,
Menu menu)
Initialize the contents of the menu for panel 'featureId'.
| ||||||||||
View |
onCreatePanelView(int featureId)
Instantiate the view to display in the panel for 'featureId'.
| ||||||||||
void |
onDetachedFromWindow()
Called when the window has been attached to the window manager.
| ||||||||||
boolean |
onGenericMotionEvent(
MotionEvent event)
Called when a generic motion event was not handled by any of the views inside of the dialog.
| ||||||||||
boolean |
onKeyDown(int keyCode,
KeyEvent event)
A key was pressed down.
| ||||||||||
boolean |
onKeyLongPress(int keyCode,
KeyEvent event)
Default implementation of
KeyEvent.Callback.onKeyLongPress() : always returns false (doesn't handle the event).
| ||||||||||
boolean |
onKeyMultiple(int keyCode, int repeatCount,
KeyEvent event)
Default implementation of
KeyEvent.Callback.onKeyMultiple() : always returns false (doesn't handle the event).
| ||||||||||
boolean |
onKeyShortcut(int keyCode,
KeyEvent event)
Called when a key shortcut event is not handled by any of the views in the Dialog.
| ||||||||||
boolean |
onKeyUp(int keyCode,
KeyEvent event)
A key was released.
| ||||||||||
boolean |
onMenuItemSelected(int featureId,
MenuItem item)
Called when a panel's menu item has been selected by the user.
| ||||||||||
boolean |
onMenuOpened(int featureId,
Menu menu)
Called when a panel's menu is opened by the user.
| ||||||||||
boolean | onOptionsItemSelected( MenuItem item) | ||||||||||
void | onOptionsMenuClosed( Menu menu) | ||||||||||
void |
onPanelClosed(int featureId,
Menu menu)
Called when a panel is being closed.
| ||||||||||
boolean |
onPrepareOptionsMenu(
Menu menu)
It is usually safe to proxy this call to the owner activity's
onPrepareOptionsMenu(Menu) if the client desires the same menu for this Dialog.
| ||||||||||
boolean |
onPreparePanel(int featureId,
View view,
Menu menu)
Prepare a panel to be displayed.
| ||||||||||
void |
onRestoreInstanceState(
Bundle savedInstanceState)
Restore the state of the dialog from a previously saved bundle.
| ||||||||||
Bundle |
onSaveInstanceState()
Saves the state of the dialog into a bundle.
| ||||||||||
boolean |
onSearchRequested()
This hook is called when the user signals the desire to start a search.
| ||||||||||
boolean |
onTouchEvent(
MotionEvent event)
Called when a touch screen event was not handled by any of the views under it.
| ||||||||||
boolean |
onTrackballEvent(
MotionEvent event)
Called when the trackball was moved and not handled by any of the views inside of the activity.
| ||||||||||
void |
onWindowAttributesChanged(
WindowManager.LayoutParams params)
This is called whenever the current window attributes change.
| ||||||||||
void |
onWindowFocusChanged(boolean hasFocus)
This hook is called whenever the window focus changes.
| ||||||||||
ActionMode |
onWindowStartingActionMode(
ActionMode.Callback callback)
Called when an action mode is being started for this window.
| ||||||||||
void | openContextMenu( View view) | ||||||||||
void | openOptionsMenu() | ||||||||||
void | registerForContextMenu( View view) | ||||||||||
final boolean |
requestWindowFeature(int featureId)
Enable extended window features.
| ||||||||||
void |
setCancelMessage(
Message msg)
Set a message to be sent when the dialog is canceled.
| ||||||||||
void |
setCancelable(boolean flag)
Sets whether this dialog is cancelable with the
BACK key.
| ||||||||||
void |
setCanceledOnTouchOutside(boolean cancel)
Sets whether this dialog is canceled when touched outside the window's bounds.
| ||||||||||
void |
setContentView(
View view)
Set the screen content to an explicit view.
| ||||||||||
void |
setContentView(int layoutResID)
Set the screen content from a layout resource.
| ||||||||||
void |
setContentView(
View view,
ViewGroup.LayoutParams params)
Set the screen content to an explicit view.
| ||||||||||
void |
setDismissMessage(
Message msg)
Set a message to be sent when the dialog is dismissed.
| ||||||||||
final void |
setFeatureDrawable(int featureId,
Drawable drawable)
Convenience for calling
setFeatureDrawable(int, Drawable) .
| ||||||||||
final void |
setFeatureDrawableAlpha(int featureId, int alpha)
Convenience for calling
setFeatureDrawableAlpha(int, int) .
| ||||||||||
final void |
setFeatureDrawableResource(int featureId, int resId)
Convenience for calling
setFeatureDrawableResource(int, int) .
| ||||||||||
final void |
setFeatureDrawableUri(int featureId,
Uri uri)
Convenience for calling
setFeatureDrawableUri(int, Uri) .
| ||||||||||
void |
setOnCancelListener(
DialogInterface.OnCancelListener listener)
Set a listener to be invoked when the dialog is canceled.
| ||||||||||
void |
setOnDismissListener(
DialogInterface.OnDismissListener listener)
Set a listener to be invoked when the dialog is dismissed.
| ||||||||||
void |
setOnKeyListener(
DialogInterface.OnKeyListener onKeyListener)
Sets the callback that will be called if a key is dispatched to the dialog.
| ||||||||||
void |
setOnShowListener(
DialogInterface.OnShowListener listener)
Sets a listener to be invoked when the dialog is shown.
| ||||||||||
final void |
setOwnerActivity(
Activity activity)
Sets the Activity that owns this dialog.
| ||||||||||
void |
setTitle(int titleId)
Set the title text for this dialog's window.
| ||||||||||
void |
setTitle(
CharSequence title)
Set the title text for this dialog's window.
| ||||||||||
final void |
setVolumeControlStream(int streamType)
By default, this will use the owner Activity's suggested stream type.
| ||||||||||
void |
show()
Start the dialog and display it on screen.
| ||||||||||
void |
takeKeyEvents(boolean get)
Request that key events come to this dialog.
| ||||||||||
void | unregisterForContextMenu( View view) |
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
void |
onCreate(
Bundle savedInstanceState)
Similar to
onCreate(Bundle) , you should initialize your dialog in this method, including calling
setContentView(View) .
| ||||||||||
void |
onStart()
Called when the dialog is starting.
| ||||||||||
void |
onStop()
Called to tell you that you're stopping.
|
简单的对话框
|
AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
.setTitle("对话框标题")
.setIcon(R.drawable.ic_launcher)
.setMessage("对话框内容")
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
Toast.makeText(MainActivity.this, "呵呵",
Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消", null)
.setNeutralButton("中间", null)
.show();
|
使用
Builder内部类创建对话框
setTitle() :为对话框设置标题 //不设置,没有标题
setIcon
()
:为对话框设置图标
setMessage
()
:为对话框设置内容
setNeutralButton
()
:普通按钮 //中间
setPositiveButton
()
:给对话框添加"Yes"按钮 //右边
setNegativeButton
()
:对话框添加"No"按钮 //左边
show
()
:显示对话框,在显示之前,会首先调用
create()来出
创建对话框
不设置 title时,不显示标题
|
进度对话框
|
ProgressDialog pd=new ProgressDialog(this);
//pd.setTitle("进度对话框");
//pd.setIcon(R.drawable.ic_launcher);
pd.setMessage("loading...");
pd.show();
| 显示无进度的对话框(圆圈圈) 在加载完成后使用隐藏 dismiss() cancel() |
进度对话框
|
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 设置为水平进度对话框
pd.setMax(1000);
pd
.show();
pd
.setProgress(10);
| 显示具有进度的对话框(ProbressBar进度条) setProgressStyle() 设置显示的样式 setMax() 设置进度的最大值 setProgress() 设置当前的进度(在对话框show只后有效) 当进度为100%后,通过代码隐藏 |
时间选择器对话框
|
Calendar calendar=Calendar.getInstance();
TimePickerDialog timeDialog = new TimePickerDialog(
this,
new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
System.out.println("onTimeSet" + hourOfDay + "时"
+ minute + "分");
}
},
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE)
,
true
);
timeDialog.setButton(
DialogInterface.BUTTON_POSITIVE,
"确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.out.println("onDialogClick");
}
});
timeDialog.setTitle("设置时间");
timeDialog.show();
|
构造器参数:
|
日期选择器对话框
|
DatePickerDialog dpd=new DatePickerDialog(
this,
new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear,
int dayOfMonth) {
Toast.makeText(MainActivity.this, monthOfYear+"",
Toast.LENGTH_SHORT).show();
}
},
c.get(Calendar.YEAR),
c.get(Calendar.MONTH),
c.get(Calendar.DAY_OF_MONTH));
dpd.setMessage("对话框内容");
dpd.setButton(
DialogInterface.BUTTON_POSITIVE,
"确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dpd.show();
|
构造器参数:
Context context,
OnDateSetListener callBack,
int year,
int monthOfYear,
int dayOfMonth
onDateSet()
设置时间,
点击确认按钮后调用
|
列表对话框
|
new AlertDialog.Builder(this)
.setTitle("列表对话框")
.setItems(R.array.dialog_items,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
Toast.makeText(MainActivity.this,
array[which], Toast.LENGTH_SHORT)
.show();
}
})
.show();
|
setItems()
列表对话框,设置每一行显示的内容
itemsId String[]/ArrayResuseId
String数组或数组资源ID
OnClickListener listener
回调方法
onClick()
DialogInterface dialog 触发事件的对话框
int which 哪一行被点击
点击某一行,对话框自动隐藏
|
单选对话框
| new AlertDialog.Builder(this) .setTitle("单选对话框") .setSingleChoiceItems( R.array.dialog_items, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText( MainActivity.this, getResources().getStringArray( R.array.dialog_items)[which], Toast.LENGTH_SHORT).show(); } }) .setPositiveButton("确定", null) .show(); |
setSingleChoiceItems
设置单选对话框每一行显示的内容
itemsId
String数组或数组资源ID
int checkedItem 默认选中的行
OnClickListener listener
回调方法
点击确认按钮隐藏
|
多选对话框
| final boolean[] checkedItems=new boolean[4]; new AlertDialog.Builder(this) .setTitle("多选对话框") .setMultiChoiceItems( R.array.dialog_items, checkedItems, new OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText( MainActivity.this, getResources().getStringArray( R.array.dialog_items)[which] + "isChecked" + isChecked, Toast.LENGTH_SHORT).show(); } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, Arrays.toString(checkedItems), Toast.LENGTH_SHORT).show(); } }) .show(); |
setMultiChoiceItems
设置单选对话框每一行显示的内容
itemsId
String数组或数组资源ID
checkedItems
默认选则情况 boolean[]
OnClickListener listener
回调方法
选择的情况保存在
boolean[]中
点击确认按钮隐藏
|
自定义对话框
|
View layout = getLayoutInflater().inflate(R.layout.dialog_view,
null);
final EditText editText = (EditText) layout
.findViewById(R.id.editText1);
Button btn = (Button) layout.findViewById(R.id.button1);
final AlertDialog show = new AlertDialog.Builder(this).setView(
layout).show();
show.setCancelable(false);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,
editText.getText().toString(), Toast.LENGTH_SHORT)
.show();
show.dismiss();
}
});
|
setView() 给对话框设置自定义显示界面
setCancelable(
false
) 不能自动隐藏,必须用过代码隐藏
getLayoutInflater().
inflate
()
将一个XML资源文件解析成View对象
使用
View.findViewById()根据ID找到该对象中的其他组件
resource:布局Id,R.layout.XXX
root: 生成的层次结构的根视图 LinearLayout
return 填充的层次结构的根视图。如果参数root提供了,那么root就是根视图;否则填充的XML文件的根就是根视图。
|