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

来源:互联网 发布:皖南医学院校园网络 编辑:程序博客网 时间:2024/06/16 12:50

一、常用 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();}

错误疑问请提 catface.wyh@gmail.com

0 0
原创粉丝点击