Android的各种Dialog,你都知道吗?

摘要:什么叫dialog?
  简单来说就是一个对话框和按钮为特定事件指定了功能:即弹出一个窗口,提示用户自己去选择,去提示,去分类的一些内容。

安卓自带的dialog有三种

  1. AlertDialog 普通的提示对话框
  2. ProgressDialog 进度条对话
  3. DatePickerDialog/TimePickerDialog 日期对话框/时间对话框

  所有的对话框,都是直接或间接继承自Dialog类,而AlterDialog直接继承自Dialog,其他的几个类均继承自AlterDialog。系统自带的dialog基本上用AlertDialog类,AlertDialog继承自Dialog类,对于Android内置的AlterDialog,它可以包含一个标题、一个内容消息或者一个选择列表、最多三个按钮。而创建AlterDialog推荐使用它的一个内部类AlterDialog.Builder创建。使用Builder对象,可以设置AlterDialog的各种属性,最后通过Builder.create()就可以得到AlterDialog对 象,如果只是还需要显示这个AlterDialog,一般可以直接使用Builder.show()方法,它会返回一个AlterDialog对象,并且显示它。

一般的dialog用法

  • 第一步:new个AlertDialog.Builder;
  • 第二步:设置dialog的图标,文字,提示信息;
  • 第三步:设置不同选择的点击事件;
  • 第四步:显示dialog。

下面将讲解常见的八种dialog

(1)普通Dialog(2)列表Dialog(3)单选Dialog(4)多选Dialog
(5)等待Dialog(6)进度条Dialog(7)编辑Dialog(8)自定义Dialog

具体代码如下:

第一种:普通Dialog:

在这里插入图片描述
效果图:
在这里插入图片描述

第二种:列表Dialog

  这个和普通的dialog差不多,只不过是从中数组选择一个确定点击事件。
在这里插入图片描述
效果图:
在这里插入图片描述

第三种:单选Dialog

  它和列表对话框区别是前面有选择的圆点,也是一样的。在这里插入图片描述
效果图:
在这里插入图片描述

第四种:多选Dialog

  在单选的基础上加了多选这个,就是选择的函数不一样。在这里插入图片描述
效果图:
在这里插入图片描述

第五种:等待Dialog

  等待Dialog具有屏蔽其他控件的交互能力,setCancelable 为使屏幕不可点击,设置为不可取消(false),下载等事件完成后,主动调用函数关闭该Dialog。
在这里插入图片描述
效果图:
在这里插入图片描述

第六种:进度条Dialog

在这里插入图片描述
效果图:
在这里插入图片描述

第七种:编辑Dialog

在这里插入图片描述
效果图:
在这里插入图片描述

第八种:自定义Dialog

略(看完以上七种,自定义就很容易了)

PS:为了方便大家学习,完整的源码已贴在文章后面(节省下载时间٩꒰▽ ꒱۶⁼³₌₃ 学习去咯)

完整演示:
在这里插入图片描述
完整代码:

package com.gx.car.ui.appoint;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.gx.car.R;

import java.util.ArrayList;

public class MainDialogActivity extends AppCompatActivity implements View.OnClickListener {
    private int yourChoice;
    private ArrayList<Integer> yourChoices = new ArrayList<>();
    private Button btDialogOne;
    private Button btDialogTwo;
    private Button btDialogThree;
    private Button btDialogFour;
    private Button btDialogFive;
    private Button btDialogSix;
    private Button btDialogSeven;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_dialog);
        //获取控件
        btDialogOne = findViewById(R.id.btn_dialog_one);
        btDialogTwo = findViewById(R.id.btn_dialog_two);
        btDialogThree = findViewById(R.id.btn_dialog_three);
        btDialogFour = findViewById(R.id.btn_dialog_four);
        btDialogFive = findViewById(R.id.btn_dialog_five);
        btDialogSix = findViewById(R.id.btn_dialog_six);
        btDialogSeven = findViewById(R.id.btn_dialog_seven);

        //监听事件
        btDialogOne.setOnClickListener(this);
        btDialogTwo.setOnClickListener(this);
        btDialogThree.setOnClickListener(this);
        btDialogFour.setOnClickListener(this);
        btDialogFive.setOnClickListener(this);
        btDialogSix.setOnClickListener(this);
        btDialogSeven.setOnClickListener(this);
    }

    //提示框方法
    private void showToastShort(String str) {
        //提示框居中显示
//        Toast toast = Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT);
//        toast.setGravity(Gravity.CENTER, 0, 0);
//        toast.show();
        //提示框默认显示
        Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
    }

    /**
     * 普通dialog
     *
     * @setIcon 设置对话框图标
     * @setTitle 设置对话框标题
     * @setMessage 设置对话框消息提示
     * setXXX方法返回Dialog对象,因此可以链式设置属性
     */
    public void DialogOne() {
        AlertDialog.Builder normalDialog = new AlertDialog.Builder(MainDialogActivity.this);
        normalDialog.setIcon(R.mipmap.ic_launcher);
        normalDialog.setTitle("这是一个普通Dialog");
        normalDialog.setMessage("你要点击哪一个按钮呢?");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        showToastShort("你点击了确定");
                    }
                });
        normalDialog.setNegativeButton("关闭",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        showToastShort("你点击了关闭");
                    }
                });
        normalDialog.show();// 显示
    }

    /**
     * 列表Dialog
     */
    public void DialogTwo() {
        final String[] items = {"这是1", "这是2", "这是3", "这是4"};
        AlertDialog.Builder listDialog =
                new AlertDialog.Builder(MainDialogActivity.this);
        listDialog.setTitle("这是一个列表Dialog");
        listDialog.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                showToastShort("你点击了" + items[which]);
            }
        });
        listDialog.show();
    }

    /**
     * 单选Dialog
     */
    public void DialogThree() {
        final String[] items = {"这是1", "这是2", "这是3", "这是4"};
        yourChoice = -1;
        AlertDialog.Builder singleChoiceDialog =
                new AlertDialog.Builder(MainDialogActivity.this);
        singleChoiceDialog.setTitle("这是一个单选Dialog");
        // 第二个参数是默认选项,此处设置为0
        singleChoiceDialog.setSingleChoiceItems(items, 0,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        yourChoice = which;
                    }
                });
        singleChoiceDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (yourChoice != -1) {
                            showToastShort("你选择了" + items[yourChoice]);
                        }
                    }
                });
        singleChoiceDialog.show();
    }

    /**
     * 多选Dialog
     */
    public void DialogFour() {
        final String[] items = {"这是1", "这是2", "这是3", "这是4"};
        // 设置默认选中的选项,全为false默认均未选中
        final boolean initChoiceSets[] = {false, false, false, false};
        yourChoices.clear();
        AlertDialog.Builder multiChoiceDialog =
                new AlertDialog.Builder(MainDialogActivity.this);
        multiChoiceDialog.setTitle("这是一个多选Dialog");
        multiChoiceDialog.setMultiChoiceItems(items, initChoiceSets,
                new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which,
                                        boolean isChecked) {
                        if (isChecked) {
                            yourChoices.add(which);
                        } else {
                            yourChoices.remove(which);
                        }
                    }
                });
        multiChoiceDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        int size = yourChoices.size();
                        String str = "";
                        for (int i = 0; i < size; i++) {
                            str += items[yourChoices.get(i)] + " ";
                        }
                        showToastShort("你选中了" + str);
                    }
                });
        multiChoiceDialog.show();
    }

    /**
     * 等待Dialog
     */
    public void DialogFive() {
        ProgressDialog waitingDialog = new ProgressDialog(MainDialogActivity.this);
        waitingDialog.setTitle("这是一个等待Dialog");
        waitingDialog.setMessage("请等待...");
        waitingDialog.setIndeterminate(true);
        waitingDialog.setCancelable(true);//为false时屏幕不可点击,设置为不可取消
        waitingDialog.show();
    }

    /**
     * 进度条dailog
     *
     * @setProgress 设置初始进度
     * @setProgressStyle 设置样式(水平进度条)
     * @setMax 设置进度最大值
     */
    public void DialogSix() {
        final int MAX_PROGRESS = 100;
        final ProgressDialog progressDialog =
                new ProgressDialog(MainDialogActivity.this);
        progressDialog.setProgress(0);
        progressDialog.setTitle("这是一个进度条Dialog");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMax(MAX_PROGRESS);
        progressDialog.show();
        /* 模拟进度增加的过程,新开一个线程,每个100ms,进度增加1*/
        new Thread(new Runnable() {
            @Override
            public void run() {
                int progress = 0;
                while (progress < MAX_PROGRESS) {
                    try {
                        Thread.sleep(100);
                        progress++;
                        progressDialog.setProgress(progress);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                // 进度达到最大值后,窗口消失
                progressDialog.cancel();
            }
        }).start();
    }

    /**
     * 编辑dialog
     */
    public void DialogSeven() {
        final EditText editText = new EditText(MainDialogActivity.this);
        AlertDialog.Builder inputDialog = new AlertDialog.Builder(MainDialogActivity.this);
        inputDialog.setTitle("我是一个输入Dialog").setView(editText);
        inputDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        showToastShort(editText.getText().toString());
                    }
                }).show();
    }

    /**
     * 点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_dialog_one:
                DialogOne();//显示dialog
                break;
            case R.id.btn_dialog_two:
                DialogTwo();
                break;
            case R.id.btn_dialog_three:
                DialogThree();
                break;
            case R.id.btn_dialog_four:
                DialogFour();
                break;
            case R.id.btn_dialog_five:
                DialogFive();
                break;
            case R.id.btn_dialog_six:
                DialogSix();
                break;
            case R.id.btn_dialog_seven:
                DialogSeven();
                break;
        }
    }
}

完整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">

    <com.gx.car.widget.MyActionBar
        android:id="@id/myActionBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:gravity="center">

        <Button
            android:id="@+id/btn_dialog_one"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textColor="@color/colorWhite"
            android:textSize="20dp"
            android:text="普通Dialog"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:gravity="center">

        <Button
            android:id="@+id/btn_dialog_two"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textColor="@color/colorWhite"
            android:textSize="20dp"
            android:text="列表Dialog"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:gravity="center">

        <Button
            android:id="@+id/btn_dialog_three"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textColor="@color/colorWhite"
            android:textSize="20dp"
            android:text="单选Dialog"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:gravity="center">

        <Button
            android:id="@+id/btn_dialog_four"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textColor="@color/colorWhite"
            android:textSize="20dp"
            android:text="多选Dialog"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:gravity="center">

        <Button
            android:id="@+id/btn_dialog_five"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textColor="@color/colorWhite"
            android:textSize="20dp"
            android:text="等待Dialog"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:gravity="center">

        <Button
            android:id="@+id/btn_dialog_six"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textColor="@color/colorWhite"
            android:textSize="20dp"
            android:text="进度条dailog"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:gravity="center">

        <Button
            android:id="@+id/btn_dialog_seven"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textColor="@color/colorWhite"
            android:textSize="20dp"
            android:text="编辑dialog"/>
    </LinearLayout>

</LinearLayout>

以上就是Dialog的使用案例啦,希望对您有帮助,如有描述不当之处,欢迎评论指正。

猜你喜欢

转载自blog.csdn.net/weixin_44563573/article/details/106839523