Android 系统原生dialog使用

简介: Android 原生 Dialog 使用 相对详细。

先上图,不上图的程序员不是好程序员:
两个按钮的 dialog
三个按钮的 dialog
列表样式 dialog
单选 dialog
多选 dialog
转圈加载 dialog
带有加载进度的 dialog
带有输入框 dialog
引入布局的 dialog
看了效果下面该看代码了

1、MainActivity的xml内代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn_two"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="最普通的 dialog"
        android:textAllCaps="false" />

    <Button
        android:id="@+id/btn_three"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="三个按钮的 dialog"
        android:textAllCaps="false" />

    <Button
        android:id="@+id/btn_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="列表 dialog"
        android:textAllCaps="false" />

    <Button
        android:id="@+id/btn_multi_select"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="多选 dialog"
        android:textAllCaps="false" />

    <Button
        android:id="@+id/btn_single_select"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="单选 dialog"
        android:textAllCaps="false" />

    <Button
        android:id="@+id/btn_waiting"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="等待 dialog"
        android:textAllCaps="false" />

    <Button
        android:id="@+id/btn_loading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="加载 dialog"
        android:textAllCaps="false" />

    <Button
        android:id="@+id/btn_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="输入 dialog"
        android:textAllCaps="false" />

    <Button
        android:id="@+id/btn_my_style"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="自定义 dialog"
        android:textAllCaps="false" />

</LinearLayout>

2、MainActivity的java文件内代码即逻辑代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private AlertDialog.Builder builder;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnTwo = findViewById(R.id.btn_two);
        Button btnThree = findViewById(R.id.btn_three);
        Button btnList = findViewById(R.id.btn_list);
        Button btnMultiSelect = findViewById(R.id.btn_multi_select);
        Button btnSingleSelect = findViewById(R.id.btn_single_select);
        Button btnWaiting = findViewById(R.id.btn_waiting);
        Button btnLoading = findViewById(R.id.btn_loading);
        Button btnInput = findViewById(R.id.btn_input);
        Button btnMyStyle = findViewById(R.id.btn_my_style);


        btnTwo.setOnClickListener(this);
        btnThree.setOnClickListener(this);
        btnList.setOnClickListener(this);
        btnMultiSelect.setOnClickListener(this);
        btnSingleSelect.setOnClickListener(this);
        btnWaiting.setOnClickListener(this);
        btnLoading.setOnClickListener(this);
        btnInput.setOnClickListener(this);
        btnMyStyle.setOnClickListener(this);

    }


    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.btn_two://最普通dialog
                showTwo();
                break;
            case R.id.btn_three://三个按钮dialog
                showThree();
                break;
            case R.id.btn_list://列表样式dialog
                showList();
                break;
            case R.id.btn_multi_select://多选dialog
                showMultiSelect();
                break;
            case R.id.btn_single_select://单选dialog
                showSingSelect();
                break;
            case R.id.btn_waiting://等待dialog
                showWaiting();
                break;
            case R.id.btn_loading://加载进度dialog
                showLoading();
                break;
            case R.id.btn_input://输入框dialog
                showInput();
                break;
            case R.id.btn_my_style://自定义dialog
                showMyStyle();
                break;
            default:
        }
    }

    /**
     * 两个按钮的 dialog
     */
    private void showTwo() {

        builder = new AlertDialog.Builder(this).setIcon(R.mipmap.ic_launcher).setTitle("最普通dialog")
                .setMessage("我是最简单的dialog").setPositiveButton("确定(积极)", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //ToDo: 你想做的事情
                        Toast.makeText(MainActivity.this, "确定按钮", Toast.LENGTH_LONG).show();
                    }
                }).setNegativeButton("取消(消极)", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //ToDo: 你想做的事情
                        Toast.makeText(MainActivity.this, "关闭按钮", Toast.LENGTH_LONG).show();
                        dialogInterface.dismiss();
                    }
                });
        builder.create().show();
    }

    /**
     * 三个按钮的 dialog
     */
    private void showThree() {
        builder = new AlertDialog.Builder(this).setIcon(R.mipmap.ic_launcher).setTitle("三个按钮dialog")
                .setMessage("三个按钮dialog").setPositiveButton("确定(积极)", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //ToDo: 你想做的事情
                        Toast.makeText(MainActivity.this, "确定按钮", Toast.LENGTH_LONG).show();
                    }
                }).setNeutralButton("你猜(中立)", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this, "你猜按钮", Toast.LENGTH_LONG).show();
                    }
                }).setNegativeButton("取消(消极)", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //ToDo: 你想做的事情
                        Toast.makeText(MainActivity.this, "关闭按钮", Toast.LENGTH_LONG).show();
                        dialogInterface.dismiss();
                    }
                });
        builder.create().show();

    }

    /**
     * 列表 dialog
     */
    private void showList() {
        final String[] items = {"item 1", "item 2", "item 3", "item 4", "item 5", "item 6"};
        builder = new AlertDialog.Builder(this).setIcon(R.mipmap.ic_launcher)
                .setTitle("列表dialog")
                .setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this, "你点击的内容为: " + items[i], Toast.LENGTH_LONG).show();

                    }
                });
        builder.create().show();
    }

    /**
     * 多选 dialog
     */
    private void showMultiSelect() {
        final List<Integer> choice = new ArrayList<>();
        final String[] items = {"多选1", "多选2", "多选3", "多选4", "多选5", "多选6"};
        //默认都未选中
        boolean[] isSelect = {false, false, false, false, false, false};

        builder = new AlertDialog.Builder(this).setIcon(R.mipmap.ic_launcher)
                .setTitle("多选dialog")
                .setMultiChoiceItems(items, isSelect, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i, boolean b) {

                        if (b) {
                            choice.add(i);
                        } else {
                            choice.remove(i);
                        }

                    }
                }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        StringBuilder str = new StringBuilder();

                        for (int j = 0; j < choice.size(); j++) {
                            str.append(items[choice.get(j)]);
                        }
                        Toast.makeText(MainActivity.this, "你选择了" + str, Toast.LENGTH_LONG).show();
                    }
                });

        builder.create().show();

    }

    private int choice;

    /**
     * 单选 dialog
     */
    private void showSingSelect() {

        //默认选中第一个
        final String[] items = {"单选1", "单选2", "单选3", "单选4", "单选5", "单选6"};
        choice = -1;
        builder = new AlertDialog.Builder(this).setIcon(R.mipmap.ic_launcher).setTitle("单选列表")
                .setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        choice = i;
                    }
                }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if (choice != -1) {
                            Toast.makeText(MainActivity.this, "你选择了" + items[choice], Toast.LENGTH_LONG).show();
                        }
                    }
                });
        builder.create().show();
    }

    /**
     * 圆圈加载进度的 dialog
     */
    private void showWaiting() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setIcon(R.mipmap.ic_launcher);
        progressDialog.setTitle("加载dialog");
        progressDialog.setMessage("加载中...");
        progressDialog.setIndeterminate(true);// 是否形成一个加载动画  true表示不明确加载进度形成转圈动画  false 表示明确加载进度
        progressDialog.setCancelable(false);//点击返回键或者dialog四周是否关闭dialog  true表示可以关闭 false表示不可关闭
        progressDialog.show();

    }

    /**
     * 带有进度的 dialog
     */
    private void showLoading() {

        final int MAX_VALUE = 100;
        progressDialog = new ProgressDialog(this);
        progressDialog.setProgress(0);
        progressDialog.setTitle("带有加载进度dialog");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMax(MAX_VALUE);
        progressDialog.show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                int progress = 0;
                while (progress < MAX_VALUE) {
                    try {
                        Thread.sleep(100);
                        progress++;
                        progressDialog.setProgress(progress);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //加载完毕自动关闭dialog
                progressDialog.cancel();
            }
        }).start();

    }

    /**
     * 一个输入框的 dialog
     */
    private void showInput() {
        final EditText editText = new EditText(this);
        builder = new AlertDialog.Builder(this).setTitle("输入框dialog").setView(editText)
                .setPositiveButton("读取输入框内容", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this, "输入内容为:" + editText.getText().toString()
                                , Toast.LENGTH_LONG).show();
                    }
                });
        builder.create().show();
    }

    /**
     * 原生自定义 dialog
     */
    private void showMyStyle() {
        @SuppressLint("InflateParams") View view = LayoutInflater.from(this).inflate(R.layout.layout_test, null);
        final EditText etUsername = view.findViewById(R.id.et_username);
        final EditText etPassword = view.findViewById(R.id.et_password);

        builder = new AlertDialog.Builder(this).setView(view).setTitle("自定义dialog——登录").setIcon(R.mipmap.ic_launcher)
                .setPositiveButton("登录", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this, "账号: " + etUsername.getText().toString() + "  密码: " + etPassword.getText().toString()
                                , Toast.LENGTH_LONG).show();
                    }
                });

        builder.create().show();
    }
}

3、介绍一些好用的第三方dialog库

1、material-dialogs
2、FlycoDialog_Master

相关文章
|
存储 API Android开发
【02】完整的安卓二次商业实战-配置gradle-构建打包原生安卓项目-调试本地运行模拟器-优雅草伊凡
【02】完整的安卓二次商业实战-配置gradle-构建打包原生安卓项目-调试本地运行模拟器-优雅草伊凡
1196 4
【02】完整的安卓二次商业实战-配置gradle-构建打包原生安卓项目-调试本地运行模拟器-优雅草伊凡
|
Linux 测试技术 语音技术
【车载Android】模拟Android系统的高负载环境
本文介绍如何将Linux压力测试工具Stress移植到Android系统,用于模拟高负载环境下的CPU、内存、IO和磁盘压力,帮助开发者优化车载Android应用在多任务并发时的性能问题,提升系统稳定性与用户体验。
1274 6
基于Android的电子记账本系统
本项目研究开发一款基于Java与Android平台的开源电子记账系统,采用SQLite数据库和Gradle工具,实现高效、安全、便捷的个人财务管理,顺应数字化转型趋势。
|
Android开发 开发者
Android中Dialog位置+样式的设置
本文介绍了在Android开发中如何设置Dialog的位置和样式。通过自定义`MyDialog`类,可以灵活调整Dialog的显示位置,例如将其固定在屏幕底部,并设置宽度匹配父布局。同时,文章还展示了如何模仿Android原生Dialog样式,通过定义`MyDialogStyle`去除标题栏、设置背景透明度、添加阴影效果以及配置点击外部关闭等功能,从而实现更加美观和符合需求的Dialog效果。代码示例详细,便于开发者快速上手实现。
922 2
|
安全 搜索推荐 Android开发
Android系统SELinux安全机制详解
如此看来,SELinux对于大家来说,就像那位不眠不休,严阵以待的港口管理员,守护我们安卓系统的平安,维护这片海港的和谐生态。SELinux就这样,默默无闻,却卫士如山,给予Android系统一份厚重的安全保障。
676 18
|
监控 Java Android开发
深入探索Android系统的内存管理机制
本文旨在全面解析Android系统的内存管理机制,包括其工作原理、常见问题及其解决方案。通过对Android内存模型的深入分析,本文将帮助开发者更好地理解内存分配、回收以及优化策略,从而提高应用性能和用户体验。
1421 38
|
存储 安全 Android开发
探索Android系统的最新安全特性
在数字时代,智能手机已成为我们生活中不可或缺的一部分。随着技术的不断进步,手机操作系统的安全性也越来越受到重视。本文将深入探讨Android系统最新的安全特性,包括其设计理念、实施方式以及对用户的影响。通过分析这些安全措施如何保护用户免受恶意软件和网络攻击的威胁,我们希望为读者提供对Android安全性的全面了解。
|
安全 Android开发 iOS开发
深入探讨Android与iOS系统的差异及未来发展趋势
本文旨在深入分析Android和iOS两大移动操作系统的核心技术差异、用户体验以及各自的市场表现,进一步探讨它们在未来技术革新中可能的发展方向。通过对比两者的开放性、安全性、生态系统等方面,本文揭示了两大系统在移动设备市场中的竞争态势和潜在变革。
|
Java Android开发 Kotlin
Android 自定义加载动画Dialog弹窗
Android 自定义加载动画Dialog弹窗
867 0
Android 自定义加载动画Dialog弹窗
|
XML Android开发 数据格式
Android自定义Dialog以控制其位置和宽高
Android自定义Dialog以控制其位置和宽高
887 0

热门文章

最新文章