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();
相关推荐
bianshaopeng1 小时前
android 原生加载pdf
android·pdf
hhzz1 小时前
Linux Shell编程快速入门以及案例(Linux一键批量启动、停止、重启Jar包Shell脚本)
android·linux·jar
火红的小辣椒2 小时前
XSS基础
android·web安全
勿问东西4 小时前
【Android】设备操作
android
五味香4 小时前
C++学习,信号处理
android·c语言·开发语言·c++·学习·算法·信号处理
图王大胜6 小时前
Android Framework AMS(01)AMS启动及相关初始化1-4
android·framework·ams·systemserver
工程师老罗8 小时前
Android Button “No speakable text present” 问题解决
android
小雨cc5566ru9 小时前
hbuilderx+uniapp+Android健身房管理系统 微信小程序z488g
android·微信小程序·uni-app
小雨cc5566ru10 小时前
微信小程序hbuilderx+uniapp+Android 新农村综合风貌旅游展示平台
android·微信小程序·uni-app
小雨cc5566ru10 小时前
小程序 uniapp+Android+hbuilderx体育场地预约管理系统的设计与实现
android·小程序·uni-app