AlertDialog6种使用方法

article/2025/10/31 19:05:56

AlertDialog

1.AlertDialog的6种创建模式

1.1setMessage

1)Java代码

//1.创建构造器AlertDialog.Builder builder=new AlertDialog.Builder(this);//2.设置参数
        builder.setTitle("弹窗提示").setIcon(R.mipmap.boy).setMessage("选择你的性别?").setPositiveButton("男",new DialogInterface.OnClickListener(){@Overridepublic void onClick(DialogInterface dialogInterface, int i) {Toast.makeText(DialogTest.this, "选中男", Toast.LENGTH_SHORT).show();}}).setNegativeButton("女",new DialogInterface.OnClickListener(){@Overridepublic void onClick(DialogInterface dialogInterface, int i) {Toast.makeText(DialogTest.this, "选中女", Toast.LENGTH_SHORT).show();}}).setNeutralButton("未知",new DialogInterface.OnClickListener(){@Overridepublic void onClick(DialogInterface dialogInterface, int i) {Toast.makeText(DialogTest.this, "选中未知", Toast.LENGTH_SHORT).show();}});

1.2setItem,设置列表项

1)Java代码

String[] citys={"济南","青岛","潍坊","日照","临沂","枣庄"};
        //1.创建构造器
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        //2.设置参数
        builder.setTitle("请选择城市地区")
                .setIcon(R.mipmap.city)
                .setItems(citys, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(DialogTest.this, ""+citys[i], Toast.LENGTH_SHORT).show();
                    }
                })
                .setPositiveButton("确认",new DialogInterface.OnClickListener(){                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {                    }
                })
                .setNegativeButton("取消",new DialogInterface.OnClickListener(){                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {                    }
                })
                .setNeutralButton("忽略",new DialogInterface.OnClickListener(){                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {                    }                });
        //3.创建对象,显示对象
        builder.create().show();

1.3setSingleChoiceItems,设置对话框内容为单选列表项

  • 可以传递数组或者是集合
String[] citys={"济南","青岛","潍坊","日照","临沂","枣庄"};//1.创建构造器AlertDialog.Builder builder=new AlertDialog.Builder(this);//2.设置参数
        builder.setTitle("请选择城市地区").setIcon(R.mipmap.city).setSingleChoiceItems(citys, 0,new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialogInterface, int i) {Toast.makeText(DialogTest.this, ""+citys[i], Toast.LENGTH_SHORT).show();}}).setPositiveButton("确认",new DialogInterface.OnClickListener(){@Overridepublic void onClick(DialogInterface dialogInterface, int i) {}}).setNegativeButton("取消",new DialogInterface.OnClickListener(){@Overridepublic void onClick(DialogInterface dialogInterface, int i) {}}).setNeutralButton("忽略",new DialogInterface.OnClickListener(){@Overridepublic void onClick(DialogInterface dialogInterface, int i) {}});//3.创建对象,显示对象
        builder.create().show();

1.4setMultiChoiceItems设置多选

 //                设置多选
        String[] citys={"济南","青岛","潍坊","日照","临沂","枣庄"};
        //1.创建构造器
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        //2.设置参数
        builder.setTitle("请选择城市地区")
                .setIcon(R.mipmap.city)
                .setMultiChoiceItems(citys, new boolean[]{false, false, false, false, false, false}, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                        Toast.makeText(DialogTest.this, ""+citys[i]+b, Toast.LENGTH_SHORT).show();
                    }
                })
                .setPositiveButton("确认",new DialogInterface.OnClickListener(){                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {                    }
                })
                .setNegativeButton("取消",new DialogInterface.OnClickListener(){                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {                    }
                })
                .setNeutralButton("忽略",new DialogInterface.OnClickListener(){                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {                    }                });
        //3.创建对象,显示对象
        builder.create().show();

1.5setAdapter设置自定义的样式

  • 需要传入一个自定义的布局

1)子布局样式

  • 文本框
  • 输入框
  • 多选框
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView
            android:id="@+id/s1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            />
        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/s2"
            android:layout_weight="1"
            />
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/s3"
            />
    </LinearLayout>
</LinearLayout>

2)Java代码

        //                设置多选
        String[] citys={"济南","青岛","潍坊","日照","临沂","枣庄"};
        //1.创建构造器
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        //2.设置参数
        builder.setTitle("请选择城市地区")
                .setIcon(R.mipmap.city)
                .setAdapter(new ArrayAdapter<String>(DialogTest.this, R.layout.myselect,R.id.s1,citys), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {                            }
                        }
                )
                .setPositiveButton("确认",new DialogInterface.OnClickListener(){                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {                    }
                })
                .setNegativeButton("取消",new DialogInterface.OnClickListener(){                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {                    }
                })
                .setNeutralButton("忽略",new DialogInterface.OnClickListener(){                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {                    }                });
        //3.创建对象,显示对象
        builder.create().show();

1.6setView,指定对话框为自定义的View

1)布局代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="账号:"
            android:textSize="30dp"
            />
        <EditText            android:id="@+id/account"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="输入账号"
            android:singleLine="true"
            android:maxLength="16"
            android:layout_weight="1"
            android:textSize="30dp"
            />    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码:"
            android:textSize="30dp"
            android:inputType="textPassword"
            />
        <EditText
            android:id="@+id/password"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="输入密码"
            android:singleLine="true"
            android:maxLength="16"
            android:layout_weight="1"
            android:textSize="30dp"
            />    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >        <Button
            android:id="@+id/lbtn1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="登录"
            android:textSize="30dp"
            />
        <Button
            android:id="@+id/lbtn2"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="取消"
            android:textSize="30dp"
            />    </LinearLayout>
</LinearLayout>

2)Java代码

  • dismiss,可以设置消失
//        6.自定义View
        //1.获取布局
        View view= LayoutInflater.from(this).inflate(R.layout.login,null);
        //2.获取布局中的控件
        EditText account=view.findViewById(R.id.account);
        EditText password=view.findViewById(R.id.password);
        Button lbtn1=view.findViewById(R.id.lbtn1);
        Button lbtn2=view.findViewById(R.id.lbtn2);        //3.创建构造器
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        //4.设置参数
        builder.setTitle("输入指定的登录信息")
                .setIcon(R.mipmap.city)
                .setView(view)
                .setPositiveButton("确认",new DialogInterface.OnClickListener(){                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {                    }
                })
                .setNegativeButton("取消",new DialogInterface.OnClickListener(){                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {                    }
                })
                .setNeutralButton("忽略",new DialogInterface.OnClickListener(){                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {                    }                });
        //5.创建对象,
        AlertDialog alertDialog=builder.create();
        //6.单独设置事件监听器
        lbtn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (account.getText().toString().equals("1001")&&password.getText().toString().equals("123456")){
                    Toast.makeText(DialogTest.this, "登录成功!", Toast.LENGTH_SHORT).show();
                    //===设置对话框消失===
                    alertDialog.dismiss();
                }
            }
        });        lbtn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(DialogTest.this, "取消登录!", Toast.LENGTH_SHORT).show();
                //===设置对话框消失===
                alertDialog.dismiss();
            }
        });
        //7.显示对象
        alertDialog.show();
    }

3)改建

public void loginAlert(View view1) {
            //        6.自定义View
            //1.获取布局
            View view= LayoutInflater.from(this).inflate(R.layout.login,null);
            //2.获取布局中的控件
            EditText account=view.findViewById(R.id.account);
            EditText password=view.findViewById(R.id.password);
            Button lbtn1=view.findViewById(R.id.lbtn1);
            Button lbtn2=view.findViewById(R.id.lbtn2);            //3.创建构造器
            AlertDialog.Builder builder=new AlertDialog.Builder(this);
            //4.设置参数
            builder.setTitle("输入指定的登录信息")
                    .setIcon(R.mipmap.city)
                    .setView(view)
                    .setPositiveButton("确认",new DialogInterface.OnClickListener(){                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {                        }
                    })
                    .setNegativeButton("取消",new DialogInterface.OnClickListener(){                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {                        }
                    })
                    .setNeutralButton("忽略",new DialogInterface.OnClickListener(){                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {                        }                    });
            //5.创建对象,
            AlertDialog alertDialog=builder.create();
            //6.单独设置事件监听器
            lbtn1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (account.getText().toString().equals("1001")&&password.getText().toString().equals("123456")){
                        Toast.makeText(DialogTest.this, "登录成功!", Toast.LENGTH_SHORT).show();
                        //===设置对话框消失===
                        alertDialog.dismiss();
                    }
                }
            });            lbtn2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(DialogTest.this, "取消登录!", Toast.LENGTH_SHORT).show();
                    //===设置对话框消失===
                    alertDialog.dismiss();
                }
            });
            //7.显示对象
            alertDialog.show();
        }

点击弹出框之后的


http://chatgpt.dhexx.cn/article/xRVddsL3.shtml

相关文章

Android的AlertDialog详解(7种方式)

需要注意的两点&#xff1a; 1. 在setIcon时&#xff0c;需要使用setTitle方法&#xff0c;否则icon不会显示 2.如果同时调用setMessage 和 setItems(或者setSingleChoiceItems setMultiChoiceItems)函数会导致dialog没有显示内容 AlertDialog的构造方法全部是Protected的&am…

AlertDialog(对话框)详解

AlertDialog可以在当前的界面上显示一个对话框&#xff0c;这个对话框是置顶于所有界面元素之上的&#xff0c;能够屏蔽掉其他控件的交互能力&#xff0c;因此AlertDialog一般是用于提示一些非常重要的内容或者警告信息。 1.创建AlertDialog 首先&#xff0c;我们来了解一下Al…

andoid小游戏开发

apk下载&#xff1a;apk下载&#xff1a;http://download.csdn.net/detail/xiangqiao123/3805861 这是去年用android写的一个小游戏&#xff0c; 我感觉不错&#xff0c;是从事android小游戏开发入门不错的案例&#xff0c; 今天就把它拿出来和大家共享一下。 程序截图&a…

android 2D 游戏的开发的方法

最近学习了android 2D 应用的开发&#xff0c;拿来和大家分享一下&#xff0c;学习2D 开发前我们先了解一下SurfaceView的使用以及贴图技术的使用&#xff0c;最后呢&#xff0c;是一个简单的2的游戏的实现。 1.SurfaceView的一些用法 提供了一个专门的绘图渲染的图形嵌入在一个…

Android手机游戏开发入门教程

Android手机游戏开发入门教程 视频欣赏地址 来自 “ ITPUB博客 ” &#xff0c;链接&#xff1a;http://blog.itpub.net/29597077/viewspace-1139520/&#xff0c;如需转载&#xff0c;请注明出处&#xff0c;否则将追究法律责任。 转载于:http://blog.itpub.net/29597077/vie…

Android 游戏开发速递

作者 / Greg Hartrell, Head of Product Management, Games on Android & Google Play 在今年 3 月举行的 Google 游戏开发者峰会上&#xff0c;我们分享了 Google 为帮助游戏开发者而持续投入研发的数种新工具和服务。这些新工具和服务能够帮助游戏开发者更轻松地查看其 A…

游戏开发相关

游戏开发—图形图像篇 游戏开发--开篇  记得我第一次玩的PC game 是KKND(绝地风暴)&#xff0c;当时的游戏平台是DOS&#xff0c;我只是觉得很好玩&#xff0c;经常和几个小学同学一起厮杀到12点。可是现在回忆起来&#xff0c;KKND无论是从智能设计还是在游戏画面与操作上都…

android小游戏制作基础,View实现游戏布局和方法

在使用android的朋友们&#xff0c;相信大家对android的游戏不陌生吧&#xff0c;像愤怒的小鸟&#xff0c;植物大战僵尸等等优秀的游戏&#xff0c;给我们带来了很好的用户体验 下面我来教大家一点android游戏开发的一点基础&#xff0c;大家可以参照这个方法框架来设计一些像…

android游戏开发的架构

&#xfeff;&#xfeff; 在编写游戏代码之前&#xff0c;必须要仔细地理顺思路&#xff0c;清晰地构建出整个游戏的框架。有的开发者经常抱怨说&#xff0c;游戏开发到最后总是千头万绪&#xff0c;一旦出现bug就不知道该如何修改&#xff0c;身心疲惫甚至是痛不欲生。其实不…

用Unity3d开发Android游戏

Unity3d是个强大的游戏引擎&#xff0c;可以很轻松的将游戏发布到Android平台上&#xff0c;今天我就来讲讲如何用Android来开发Android游戏。 首先我们要下载Android SDK&#xff0c;可以在http://developer.android.com/sdk/index.html这里下载到&#xff0c;运行installer安…

Android游戏开发的入门实例

在Android系统上开发游戏是Android开发学习者所向往的&#xff0c;有成就感也有乐趣&#xff0c;还能取得经济上的报酬。那怎样开发Android游戏呢&#xff1f;下面介绍一个简单的入门实例。 一、创建新工程   首先&#xff0c;我们在Eclipse中新建一个名为Movement的工程&…

如何开发手机游戏?

当今社会&#xff0c;手机游戏无非是当下在旅途中打发时间的最便捷的方式。有关数据显示&#xff0c;62% 的智能手机用户在购买智能手机后的一周内安装了游戏。无论你是参加聚会还是度假&#xff0c;或者周末宅在家里&#xff0c;手机都是玩游戏最方便的设备。这也正是手机游戏…

【Android游戏开发详细过程1】Android平台飞机大战游戏APP设计与实现

【Android游戏】Android平台飞机大战游戏APP设计与实现 前言一、界面设计与功能实现1.1 主界面1.2 登录界面1.2 注册界面1.4 菜单界面1.5 设置界面1.6 商店界面1.7 换机界面1.8 游戏界面1.9 欢迎界面1.10 游戏图标 二、数据库设计与实现三、服务器设计与实现四、其他功能实现 前…

datagridview中使用DataGridViewComboBoxColumn

在datagridview中使用自带的DataGridViewComboBoxColumn&#xff0c;加载数据库中的数据&#xff0c;选中和保存所需要的数据 实现效果如图 加载数据库已保存的数 选择自己所需要的数据 具体代码如下 datagridview命名为&#xff1a;dgvDrugList 添加基本列及对应的设…

DataGridView怎样实现添加、删除、上移、下移一行

场景 在Winform中使用DataGridView实现添加一行、删除一行、上移一行、下移一行。 注&#xff1a; 博客主页&#xff1a;https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书、教程推送与免费下载。 实现 添加一行 private void Task…

DataGridview获取选中行数

DataGridview获取选中行数 代码&#xff1a; dataGridView1.CurrentRow.Index//获取选中行数使用Messbox.Show()弹窗&#xff1a;

DataGridview动态初始化

DataGridview动态初始化 this.dataGridView1.Rows.Add("参数"&#xff0c;"参数"...");Add&#xff08;&#xff09;方法里面可以根据列数而添加相应个数的参数 例如&#xff1a; 上图的列数有4个&#xff0c;所以如果要将DataGridview控件里面添加…

C# 解决datagridview控件显示大量数据拖拉卡顿问题

问题描述&#xff1a; 由于在使用SQL查询大量的数据并一次显示到dataGridView控件&#xff0c;出现拖拉的时候卡顿。 解决方法&#xff1a; 1.首先分页。 2.其次把显示控件设置双buffer。 解决过程如下&#xff1a; 1.设置dataGridView双buffer代码如下&#xff0c;需要引用…

DataGridView绑定数据库

背景 今天在做C#实验的时候&#xff0c;遇到了一个难题&#xff1a;需要将数据库中的数据在C#的窗体的DataGridView控件中显示出来。当然老师布置这个作业是在之前做了铺垫的&#xff0c;之前做省县区三级查询时&#xff0c;讲过了SampleData和LiteDB的使用&#xff0c;但是我…

Winform实现在DataGridView控件的单元格中添加多个控件

Winform实现在DataGridView控件的单元格中添加多个控件 背景实现思路关键代码完整代码下载 背景 DataGridView控件的列是支持TextBoxColumn、ComboBoxColumn等类型的&#xff0c;就是DataGridView的单元格进入编辑模式的时候就会出现对应的控件&#xff0c;但是有些业务场景是…