继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Android中的AlertDialog

Amy_木婉清
关注TA
已关注
手记 28
粉丝 2
获赞 22

一.普通对话框
演示图片:
image.png
demo代码:

    class OnClick implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            switch (v.getId()){
               case R.id.btn_dialog1:
                   AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
                    builder.setTitle("请回答")
                           .setMessage("你觉得Android是一种什么样的语言")
                            .setIcon(R.mipmap.username)
                           .setPositiveButton("神奇的语言", new DialogInterface.OnClickListener() {
                               @Override
                               public void onClick(DialogInterface dialogInterface, int which) {
                                   ToastUtil.showMsg(DialogActivity.this,"很好呀");
                               }
                           }).setNeutralButton("还行", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            ToastUtil.showMsg(DialogActivity.this,"在思考一下~");
                        }
                    }).setNegativeButton("不神奇", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            ToastUtil.showMsg(DialogActivity.this,"怎么可能");
                        }
                    }).show();
                    break;
            }
        }
    }

二.单选对话框
运行图片
image.png
demo代码:

                case R.id.btn_dialog2:
                    final String[] array = new String[]{"男","女"};
                    AlertDialog.Builder builder1 = new AlertDialog.Builder(DialogActivity.this);
                    builder1.setTitle("选择性别")
                            .setItems(array, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    ToastUtil.showMsg(DialogActivity.this,array[i]);

                                }
                            }).show();

复选按钮形式展示:
image.png

demo代码展示:

            case R.id.btn_dialog3:
                    final String[] array3 = new String[]{"男","女"};
                    AlertDialog.Builder builder3 = new AlertDialog.Builder(DialogActivity.this);
                    builder3.setSingleChoiceItems(array3, 0, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            ToastUtil.showMsg(DialogActivity.this,array3[i]);

                        }
                    }).show();
                    break;

三.多选弹窗:
image.png
demo代码:

       case R.id.btn_dialog4:
                    final String[] array4 = new String[]{"唱歌","跳舞","写代码"};
                    final boolean[] isSelected = new boolean[]{false,false,true};
                    AlertDialog.Builder builder4 = new AlertDialog.Builder(DialogActivity.this);
                    builder4.setTitle("选择兴趣")
                            .setMultiChoiceItems(array4, isSelected, new DialogInterface.OnMultiChoiceClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i, boolean isChecked) {
                                    ToastUtil.showMsg(DialogActivity.this,array4[i]+":"+isChecked);
                                }
                            }).show();

四.自定义dialog
image.png
demo代码:

                case R.id.btn_dialog5:
                    AlertDialog.Builder builder5 = new AlertDialog.Builder(DialogActivity.this);
                    View view= LayoutInflater.from(DialogActivity.this).inflate(R.layout.layout_dialog,null);
                    EditText etUsername = view.findViewById(R.id.et_username);
                    EditText etPassword = view.findViewById(R.id.et_password);
                    Button btnLogin = view.findViewById(R.id.btn_login);
                    btnLogin.setOnClickListener(new View.OnClickListener() {
                            @Override
                        public void onClick(View view) {
                            //相应操作
                        }
                    });
                    builder5.setTitle("请先登录")
                            .setView(view).show();
                    break;
打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP