Dialog - 常用类型 & 自定义布局

一、常用 Dialog 类型

依次介绍:通知、列表、单选、复选、进度对话框

  1. 通知对话框

    这里写图片描述

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setIcon(R.mipmap.ic_launcher).setTitle("通知对话框").setMessage("爱不爱我").setPositiveButton("爱", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(AActivity.this, "爱你", Toast.LENGTH_SHORT).show();
        }
    }).setNegativeButton("不爱", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(AActivity.this, "恨你", Toast.LENGTH_SHORT).show();
        }
    }).show();
    
  2. 列表对话框

    这里写图片描述

    final String[] names = { "zhangsan", "wangwu", "lisi", "zhaoliu" }; // 列表选项
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setIcon(R.mipmap.ic_launcher).setTitle("列表对话框").setItems(names, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(AActivity.this, names[which], Toast.LENGTH_SHORT).show();
        }
    }).show();
    
  3. 单选对话框

    这里写图片描述

    final String[] names = { "zhangsan", "wangwu", "lisi", "zhaoliu" }; // 单选选项
    final String[] name = new String[1]; // 记录选中项
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setIcon(R.mipmap.ic_launcher).setTitle("单选对话框").setSingleChoiceItems(names, 0, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            name[0] = names[which];
            Toast.makeText(AActivity.this, name[0], Toast.LENGTH_SHORT).show();
        }
    }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(AActivity.this, ("确定" + name[0]), Toast.LENGTH_SHORT).show();
        }
    }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(AActivity.this, "取消选择", Toast.LENGTH_SHORT).show();
        }
    }).show();
    
  4. 复选对话框

    这里写图片描述

    final String[] names = { "zhangsan", "wangwu", "lisi", "zhaoliu" }; // 复选选项
    final List<String> nameChosed = new ArrayList<>(); // 记录选中项
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setIcon(R.mipmap.ic_launcher).setTitle("多选对话框").setMultiChoiceItems(names, new boolean[]{false, false, false, false}, new DialogInterface.OnMultiChoiceClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which, boolean isChecked) {
            String name = names[which];
            if (isChecked) {
                nameChosed.add(name);
                Toast.makeText(AActivity.this, name + "OK", Toast.LENGTH_SHORT).show();
            } else {
                nameChosed.remove(name);
                Toast.makeText(AActivity.this, name + "NO", Toast.LENGTH_SHORT).show();
            }
        }
    }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(AActivity.this, "nameChosed.size():" + nameChosed.size() + "", Toast.LENGTH_SHORT).show();
        }
    }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(AActivity.this, "取消", Toast.LENGTH_SHORT).show();
        }
    }).show();
    
  5. 进度对话框

    这里写图片描述

    final ProgressDialog progressDialog = new ProgressDialog(this);
    progressDialog.setIcon(R.mipmap.ic_launcher);
    progressDialog.setTitle("进度对话框");
    progressDialog.setMessage("正在下载啊...");
    progressDialog.setProgressStyle(progressDialog.STYLE_HORIZONTAL);
    progressDialog.setMax(100);
    progressDialog.show();
    
    new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                SystemClock.sleep(100);
                progressDialog.incrementProgressBy(5);
                if (progressDialog.getProgress() == progressDialog.getMax()) {
                    runOnUiThread(new Runnable() { // 更新 UI
                        @Override
                        public void run() {
                            progressDialog.dismiss();
                            Toast.makeText(AActivity.this, "进度完成", Toast.LENGTH_SHORT).show();
                        }
                    });
                    break;
                }
            }
        }
    }).start();
    

二、自定义 Dialog 的布局

  • 效果

    这里写图片描述

  • xml布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#10f0"
            android:gravity="center"
            android:padding="5dp"
            android:text="设置密码"
            android:textSize="20sp" />
    
        <EditText
            android:id="@+id/et_pwd_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入密码"
            android:padding="5dp" />
    
        <EditText
            android:id="@+id/et_pwd_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请确认密码"
            android:padding="5dp" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="5dp">
    
            <Button
                android:id="@+id/bt_YES"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="确认" />
    
            <Button
                android:id="@+id/bt_NO"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
            android:text="取消" />
        </LinearLayout>
    
    </LinearLayout>
    
  • Java代码

    private EditText etPWD1, etPWD2;
    private String pwdStr1, pwdStr2;
    
    public void showDialog(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        View dialogView = View.inflate(this, R.layout.dialog_set_pwd, null);
        dialog.setView(dialogView, 0, 0, 0, 0); // 去边距,兼容2.X
    
        etPWD1 = (EditText) dialogView.findViewById(R.id.et_pwd_1); // 记录两次密码
        etPWD2 = (EditText) dialogView.findViewById(R.id.et_pwd_2);
    
        dialogView.findViewById(R.id.bt_YES).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pwdStr1 = etPWD1.getText().toString();
                pwdStr2 = etPWD2.getText().toString();
                if (TextUtils.isEmpty(pwdStr1) || TextUtils.isEmpty(pwdStr2)) {
                    Toast.makeText(getApplicationContext(), "密码不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    if (pwdStr1.equals(pwdStr2)) {
                        Toast.makeText(AActivity.this, "密码设置成功", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    } else {
                        Toast.makeText(getApplicationContext(), "密码不一致", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        dialogView.findViewById(R.id.bt_NO).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    
        dialog.show();
    }
    
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值