- 创建 Dialog 实例
- 设置相关参数(标题、消息内容、按键)及属性
- 调用展示方法
常用方法:
setTitle(“Dialog”):设置标题
setMessage("Loading … "):设置消息内容
setCancelable(false):设置是否可以点击屏幕取消,设置为 false 用于一些必须立即处理的操作
setPositiveButton(CharSequence text, OnClickListener listener):设置表示赞同的(positive)按钮,第一个参数用来设置按钮名称,第二个参数是用来监听按钮点击事件的监听器,是 DialogInterface.OnClickListener 接口类型的
源码:
public Builder setPositiveButton(CharSequence text, final OnClickListener listener) {
P.mPositiveButtonText = text;
P.mPositiveButtonListener = listener;
return this;
}
public CharSequence mButtonPositiveText;
public DialogInterface.OnClickListener mPositiveButtonListener;
if (mPositiveButtonText != null) {
dialog.setButton(DialogInterface.BUTTON_POSITIVE, mPositiveButtonText,
mPositiveButtonListener, null);
}
public void setButton(int whichButton, CharSequence text,
DialogInterface.OnClickListener listener, Message msg) {
if (msg == null && listener != null) {
msg = mHandler.obtainMessage(whichButton, listener);
}
switch (whichButton) {
case DialogInterface.BUTTON_POSITIVE:
mButtonPositiveText = text;
mButtonPositiveMessage = msg;
break;
case DialogInterface.BUTTON_NEGATIVE:
mButtonNegativeText = text;
mButtonNegativeMessage = msg;
break;
case DialogInterface.BUTTON_NEUTRAL:
mButtonNeutralText = text;
mButtonNeutralMessage = msg;
break;
default:
throw new IllegalArgumentException("Button does not exist");
}
}
DialogInterface 是一个接口,DialogInterface.OnClickListener 也是接口,里面有 onClick(DialogInterface dialog, int which) 方法接收是哪个 dialog 的点击事件,以及 which 按钮类型
public interface DialogInterface {
interface OnClickListener {
/**
* This method will be invoked when a button in the dialog is clicked.
*
* @param dialog The dialog that received the click.
* @param which The button that was clicked (e.g.
* {@link DialogInterface#BUTTON1}) or the position
* of the item clicked.
*/
/* TODO: Change to use BUTTON_POSITIVE after API council */
public void onClick(DialogInterface dialog, int which);
}
}
实例:
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Dialog");
builder.setMessage("Loading ... ");
builder.setCancelable(false);
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNeutralButton("???", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
setXXXButtom 里使用 new DialogInterface.OnClickListener() {} 对 OnClick() 进行实现,使按钮接收点击事件并作出相应响应,与 View.OnClickListener() 的 OnClick(View v) 区别在于参数的不同。
进度对话框 ProgressDialog
与警告对话框基本一致,实例:
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("Dialog");
progressDialog.setMessage("loading ... ");
progressDialog.setCancelable(true);
progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
progressDialog.show();
警报对话框 AlertDialog
https://developer.android.com/reference/android/app/AlertDialog
java.lang.Object
↳ android.app.Dialog
↳ android.app.AlertDialog
Dialog的子类,可以显示一个,两个或三个按钮。如果只想在此对话框中显示 String,请使用 setMessage 方法。如果要显示更复杂的视图,请查找资源名为“custom”的 FrameLayout 并向其中添加视图:
FrameLayout fl = findViewById(android.R.id.custom);
fl.addView(myView, new LayoutParams(MATCH_PARENT, WRAP_CONTENT));
当检测对话框中有任何视图从 View#onCheckIsTextEditor 返回 true,AlertDialog 将负责为您自动设置 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM。通常,您希望此设置用于没有文本编辑器的Dialog,以便将其放置在当前输入法UI的顶部。可以在 onCreate 之后通过将标志强制设为你所需的模式来修改此行为。
FLAG_ALT_FOCUSABLE_IM:设置后,输入法不能与可聚焦窗口交互,可以设置来覆盖输入法,以使用更多空间。注意:与 FLAG_NOT_FOCUSABLE 标志结合使用时,此标志不生效,因为输入法在设置了 FLAG_NOT_FOCUSABLE 标志的窗口不显示。
AlertDialog.Builder
AlertDialog.Builder setItems (CharSequence[] items, DialogInterface.OnClickListener listener)
:设置要在对话框中显示的项目列表作为内容,使用提供的侦听器在选则项目时进行通知。