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();
相关推荐
IAM四十二9 天前
Google 端侧 AI 框架 LiteRT 初探
android·深度学习·tensorflow
CYRUS_STUDIO9 天前
手把手教你用 Chrome 断点调试 Frida 脚本,JS 调试不再是黑盒
android·app·逆向
Just丶Single9 天前
安卓NDK初识
android
编程乐学9 天前
网络资源模板--基于Android Studio 实现的咖啡点餐App
android·android studio·大作业·奶茶点餐·安卓移动开发·咖啡点餐
二流小码农9 天前
鸿蒙开发:基于node脚本实现组件化运行
android·ios·harmonyos
Wgllss9 天前
Kotlin+协程+FLow+Channel+Compose 实现一个直播多个弹幕效果
android·架构·android jetpack
顾林海9 天前
Android WebView内存释放全解析:从泄漏检测到彻底释放的实战指南
android·面试·性能优化
用户20187928316710 天前
🍕 披萨工厂奇遇记:Android APK打包之旅
android
程序猿陌名!10 天前
Android开发 原生设置-apps-里面隐藏应用信息
android
张风捷特烈10 天前
每日一题 Flutter#13 | build 回调的 BuildContext 是什么
android·flutter·面试