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();
相关推荐
lichong95136 分钟前
【混合开发】vue+Android、iPhone、鸿蒙、win、macOS、Linux之video 的各种状态和生命周期调用说明
android·vue.js·macos
app出海创收老李1 小时前
海外独立创收日记(1)-我是如何从0到1在Google Play获得睡后被动收入的?
android·程序员
lang9998881 小时前
kodi在Android4.0.4安装播放歌曲显示歌词
android·kodi·歌词插件
yzx9910131 小时前
构建未来:深度学习、嵌入式与安卓开发的融合创新之路
android·人工智能·深度学习
前行的小黑炭2 小时前
Android :如何快速让布局适配手机和平板?
android·java·kotlin
Yang-Never5 小时前
Kotlin协程 -> Job.join() 完整流程图与核心源码分析
android·开发语言·kotlin·android studio
一笑的小酒馆11 小时前
Android性能优化之截屏时黑屏卡顿问题
android
懒人村杂货铺14 小时前
Android BLE 扫描完整实战
android
TeleostNaCl16 小时前
如何安装 Google 通用的驱动以便使用 ADB 和 Fastboot 调试(Bootloader)设备
android·经验分享·adb·android studio·android-studio·android runtime
fatiaozhang952717 小时前
中国移动浪潮云电脑CD1000-系统全分区备份包-可瑞芯微工具刷机-可救砖
android·网络·电脑·电视盒子·刷机固件·机顶盒刷机