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();
相关推荐
安卓开发者3 小时前
Android RxJava 组合操作符实战:优雅处理多数据源
android·rxjava
阿华的代码王国3 小时前
【Android】RecyclerView复用CheckBox的异常状态
android·xml·java·前端·后端
一条上岸小咸鱼3 小时前
Kotlin 基本数据类型(三):Booleans、Characters
android·前端·kotlin
Jerry说前后端4 小时前
RecyclerView 性能优化:从原理到实践的深度优化方案
android·前端·性能优化
alexhilton4 小时前
深入浅出着色器:极坐标系与炫酷环形进度条
android·kotlin·android jetpack
一条上岸小咸鱼10 小时前
Kotlin 基本数据类型(一):Numbers
android·前端·kotlin
Huntto11 小时前
最小二乘法计算触摸事件速度
android·最小二乘法·触摸事件·速度估计
一笑的小酒馆11 小时前
Android中使用Compose实现各种样式Dialog
android
红橙Darren11 小时前
手写操作系统 - 编译链接与运行
android·ios·客户端
鹏多多.15 小时前
flutter-使用device_info_plus获取手机设备信息完整指南
android·前端·flutter·ios·数据分析·前端框架