Android 入门宝典 - Dialog 对话框

  1. 创建 Dialog 实例
  2. 设置相关参数(标题、消息内容、按键)及属性
  3. 调用展示方法

常用方法:
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):设置要在对话框中显示的项目列表作为内容,使用提供的侦听器在选则项目时进行通知。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值