Android中常用Dialog的使用

Android开发中常会使用到Dialog对话框,如拦截返回键后问是否退出。

1.AlertDialog - 简单对话框

java 复制代码
//简单对话框
        AlertDialog alertDialog=new AlertDialog.Builder(context)
                .setIcon(R.drawable.~)
                .setTitle("标题")
                .setMessage("信息")
                .setNegativeButton("阴性按钮(左)名称", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //执行代码
                        alertDialog.dismiss();//销毁对话框
                    }
                })
                .setPositiveButton("阳性按钮(右)名称", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //执行代码
                        alertDialog.dismiss();//销毁对话框
                    }
                })
                .create();

        alertDialog.show();

2.AlertDialog - 列表对话框

注意 .setItems(列表项,监听器)

java 复制代码
//列表对话框
        String items[]=new String[]{"item1","item2","...."};

        AlertDialog alertDialog=new AlertDialog.Builder(context)
                .setIcon(R.drawable.~)
                .setTitle("标题")
                .setItems(items, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //   i 表示点击的指针,可使用 items[i]
                    }
                })
                .setNegativeButton("阴性按钮(左)名称", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //执行代码
                        alertDialog.dismiss();//销毁对话框
                    }
                })
                .setPositiveButton("阳性按钮(右)名称", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //执行代码
                        alertDialog.dismiss();//销毁对话框
                    }
                })
                .create();
        
        alertDialog.show();

3.单选列表对话框

注意 .setSingleChoiceItems(列表项,选中的列表项的指针,监听器)

java 复制代码
//单选列表对话框
        String items[]=new String[]{"item1","item2","...."};

        AlertDialog alertDialog=new AlertDialog.Builder(context)
                .setIcon(R.drawable.~)
                .setTitle("标题")
                .setSingleChoiceItems(items,0, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //   i 表示点击的指针,可使用 items[i]
                    }
                })
                .setNegativeButton("阴性按钮(左)名称", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //执行代码
                        alertDialog.dismiss();//销毁对话框
                    }
                })
                .setPositiveButton("阳性按钮(右)名称", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //执行代码
                        alertDialog.dismiss();//销毁对话框
                    }
                })
                .create();

        alertDialog.show();

4.AlertDialog - 多选列表对话框

注意 .setSingleChoiceItems(列表项,列表项是否选中的数组,监听器)

java 复制代码
//多选列表对话框
        String items[]=new String[]{"item1","item2","...."};

        AlertDialog alertDialog=new AlertDialog.Builder(context)
                .setIcon(R.drawable.~)
                .setTitle("标题")
                .setMultiChoiceItems(items, new boolean[]{false, false, ... }, new DialogInterface.OnMultiChoiceClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                        //   i 表示点击的指针,可使用 items[i] 
                    }
                })
                .setNegativeButton("阴性按钮(左)名称", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //执行代码
                        alertDialog.dismiss();//销毁对话框
                    }
                })
                .setPositiveButton("阳性按钮(右)名称", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //执行代码
                        alertDialog.dismiss();//销毁对话框
                    }
                })
                .create();

        alertDialog.show();

5.AlertDialog - 半自定义对话框

注意 .setView( View )

java 复制代码
//半自定义对话框
        View view = LayoutInflater.from(context).inflate(R.layout.view,null);

        AlertDialog alertDialog=new AlertDialog.Builder(context)
                .setIcon(R.drawable)
                .setTitle("标题")
                .setView(view)
                .setNegativeButton("阴性按钮(左)名称", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //执行代码
                        alertDialog.dismiss();//销毁对话框
                    }
                })
                .setPositiveButton("阳性按钮(右)名称", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //执行代码
                        alertDialog.dismiss();//销毁对话框
                    }
                })
                .create();
        alertDialog.show();

6.AlertDialog - 全自定义对话框

不常用

7.ProgressDialog - 圆形进度条对话框

java 复制代码
 //圆形进度条对话框
        ProgressDialog progressDialog=new ProgressDialog(context);
        progressDialog.setMessage("信息");
        progressDialog.show();

8.ProgressDialog - 圆形进度条对话框

.setProgressStyle( ProgressDialog.STYLE_HORIZONTAL ) 用于设置水平

.setMax(100) 设置最大值

.setProgress(10) 设置当前进度

java 复制代码
//圆形进度条对话框
        ProgressDialog progressDialog=new ProgressDialog(context);
        progressDialog.setMessage("信息");
        progressDialog.setProgressStyle( ProgressDialog.STYLE_HORIZONTAL );
        progressDialog.setMax(100);
        progressDialog.setProgress(10);
        progressDialog.show();
相关推荐
LittleLoveBoy28 分钟前
踩坑:uiautomatorviewer.bat 打不开
android
居然是阿宋1 小时前
Android核心系统服务:AMS、WMS、PMS 与 system_server 进程解析
android
CGG924 小时前
【单例模式】
android·java·单例模式
kp000004 小时前
PHP弱类型安全漏洞解析与防范指南
android·开发语言·安全·web安全·php·漏洞
编程乐学(Arfan开发工程师)9 小时前
06、基础入门-SpringBoot-依赖管理特性
android·spring boot·后端
androidwork9 小时前
使用 Kotlin 和 Jetpack Compose 开发 Wear OS 应用的完整指南
android·kotlin
繁依Fanyi11 小时前
Animaster:一次由 CodeBuddy 主导的 CSS 动画编辑器诞生记
android·前端·css·编辑器·codebuddy首席试玩官
奔跑吧 android13 小时前
【android bluetooth 框架分析 02】【Module详解 6】【StorageModule 模块介绍】
android·bluetooth·bt·aosp13·storagemodule
田一一一16 小时前
Android framework 中间件开发(三)
android·中间件·framework·jni
androidwork21 小时前
掌握 Kotlin Android 单元测试:MockK 框架深度实践指南
android·kotlin