安卓AlertDialog弹窗

article/2025/10/31 18:53:02

Android在开发中经常会遇到有弹框的需求。

经常使用的有Dialog 弹框,Window弹框(任意位置弹出除了外观样式和显示的位置的区别之外,他们之间最本质的区别是: dialog是非阻塞式对话框,popupwindow是阻塞式对话框。也就是说dialog弹出时 后台还可以进行很多的操作,而popupwindow弹出是 后台进程是阻塞的,要一直等待popupwindow 消失 才会进行操作。

),Activity伪弹框

Activity变成一个窗口

 

 

目录

AlertDialog的六种创建方式... 1

创建AlertDialog的步骤:... 1

最常用的AlertDialog. 2

一、setMessage:设置对话框内容为简单文本内容... 4

二、setItem:设置文本框内容为简单列表项... 4

三、setSingleChoiceItems()设置对话框内容为单选列表项     5

四、setMultiChoiceItems()设置对话框内容为多选项列表     6

五、setView ()设置图片内容为对话框项... 7

六、setView()设置对话框为自定义输入框View.. 8

七、ProgressDialog()圆形进度条对话框... 10

 

 

AlertDialog的六种创建方式

创建AlertDialog的步骤:

1、创建AlertDialog.Builder对象

2、调用Builder b有的)对象的setTitle方法设置标题,setIcon方法设置图标

3、调用Builder相关方法如setMessage(显示内容)方法、

setItems 方法、

setSingleChoiceItems方法、(色特行康崔赛腾)

setMultiChoiceItems方法、(色特某疼康崔赛腾)

setAdapter方法、(额达科特)

setView方法设置不同类型的对话框内容。

4、调用setPositiveButton(右边)、

setNegativeButton(中间)()色特兰格提兀巴腾、

setNeutralButton(左边)设置多个按钮(色特刘雀巴腾)

5、调用 方法创建AlertDialog对象

6、调用AlertDialog对象的show()方法将对话框显示出来

 

最常用的AlertDialog.

今天主要是讲他的用法。AlertDialog的用法也很简单

经常用的:

setTitle :为对话框设置标题

 setIcon :为对话框设置图标

 setMessage:为对话框设置内容

 setView : 给对话框设置自定义样式

 setItems :设置对话框要显示的一个list,一般用于显示几个命令时

 setMultiChoiceItems :用来设置对话框显示一系列的复选框

 setSingleChoiceItems :用来设置对话框显示一系列的单选框

 setNeutralButton    :普通按钮

 setPositiveButton   :给对话框添加"Yes"按钮

 setNegativeButton :对话框添加"No"按钮

 create : 创建对话框

 show :显示对话框

 

 

列表框list

单选框Radio

复选框check

图片Picture

输入entry

进度条 progress

 

  • 点击普通弹窗时弹出下面的界面:

涉及到知识点:setMessage设置对话框内容为简单文本内容

context

AlertDialog.Builder alertdialogbuilder = new AlertDialog.Builder(this);
alertdialogbuilder.setMessage(
"您确认要退出程序");
alertdialogbuilder.setPositiveButton(
"确定", null);
alertdialogbuilder.setNeutralButton(
"取消", null);
final AlertDialog alertdialog1 = alertdialogbuilder.create();
alertdialog1.show();

二、setItem:设置文本框内容为简单列表项

核心代码:

builder = new AlertDialog.Builder(this);builder.setTitle("列表框").setItems(new String[]{"列表项1", "列表项2", "列表项3"}, null).setNegativeButton("确定", null);builder.create().show();

三、setSingleChoiceItems()设置对话框内容为单选列表项

核心代码:

final String[] item = new String[]{"选项1", "选项2", "选项3", "选项4"};android.app.AlertDialog alertDialog = new android.app.AlertDialog.Builder(this).setTitle("请选择")//默认为0表示选中第一个项目.setSingleChoiceItems(item, 0, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this, "你单选了" + item[which], Toast.LENGTH_LONG).show();}}).setPositiveButton("确认", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {}}).setNegativeButton("取消", null).create();alertDialog.show();

四、setMultiChoiceItems()设置对话框内容为多选项列表

 
核心代码:
// 创建数据final String[] items = new String[]{"我是Item一", "我是Item二", "我是Item三", "我是Item四"};// 创建对话框构建器builder = new AlertDialog.Builder(this);builder.setIcon(R.drawable.ic_launcher_background).setTitle("提示").setMultiChoiceItems(items, //选项条new boolean[]{true, true, false, false, false},// //这个參数必须是boolean[]的,不能使Boolean[]的,有几个item就数组长度几个,true为勾选,false则相反new DialogInterface.OnMultiChoiceClickListener() {@Overridepublic void onClick(DialogInterface dialog,int which, boolean isChecked) {// TODO Auto-generated method stubif (isChecked) {Toast.makeText(MainActivity.this,items[which], Toast.LENGTH_SHORT).show();}}});builder.create().show();

五、setView ()设置图片内容为对话框项

 
核心代码:
ImageView img = new ImageView(this);img.setImageResource(R.mipmap.timg);builder = new AlertDialog.Builder(this);builder.setTitle("图片框").setView(img).show();
 
//—————升级添加按钮事件——————
ImageView img = new ImageView(this);img.setImageResource(R.mipmap.timg);builder = new AlertDialog.Builder(this);builder.setTitle("认识它吗?").setView(img).setPositiveButton("知道", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this,"哎呦,不错哦",Toast.LENGTH_SHORT).show();}}).setNegativeButton("不知道", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this,"一点也不老实",Toast.LENGTH_SHORT).show();}}).setNeutralButton("谁呀", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this,"快睁开眼瞅瞅",Toast.LENGTH_SHORT).show();}});builder.create() .show();

六、setView()设置对话框为自定义输入框View

核心代码点击事件写:
builder = new AlertDialog.Builder(this);// 获取布局View view2 = View.inflate(MainActivity.this, R.layout.login, null);// 获取布局中的控件final EditText username = (EditText) view2.findViewById(R.id.username);final EditText password = (EditText) view2.findViewById(R.id.password);final Button btn = (Button) view2.findViewById(R.id.btn_login);// 设置参数builder.setTitle("Login").setIcon(R.drawable.ic_launcher_background).setView(view2);// 创建对话框builder.create().show();btn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubString uname = username.getText().toString().trim();String psd = password.getText().toString().trim();if (uname.equals("123") && psd.equals("123")) {Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();}Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_SHORT).show();}});
LinearLayout还需要一个login.Xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:orientation="vertical"
   
android:gravity="center">
    <
LinearLayout
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:orientation="horizontal">
        <
TextView
           
android:layout_width="wrap_content"
           
android:layout_height="wrap_content"
           
android:text="姓名"
           
android:id="@+id/tv_name"/>
        <
EditText
           
android:layout_width="200dp"
           
android:layout_height="wrap_content"
           
android:id="@+id/username"
           
/>
    </
LinearLayout>
    <
LinearLayout
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:orientation="horizontal">
        <
TextView
           
android:layout_width="wrap_content"
           
android:layout_height="wrap_content"
           
android:text="姓名"
            
android:id="@+id/tv_pwd"/>
        <
EditText
           
android:layout_width="200dp"
           
android:layout_height="wrap_content"
           
android:id="@+id/password"
           
/>
    </
LinearLayout>
    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Login"
       
android:id="@+id/btn_login"/>
</
LinearLayout>

 

 

七、ProgressDialog()圆形进度条对话框


 

                //ProgressDialog.STYLE_SPINNER 环形精度条

                //ProgressDialog.STYLE_HORIZONTAL 水平样式的进度条

ProgressDialog:可以在当前界面弹出一个置顶于所有界面元素的对话框,同样具有屏蔽其他控件的交互能力,用于提示用户当前操作正在运行,让用户等待。

设置可否使用back键返回,这里设置若是为progressDialog.setCancelable(flase),则要在数据加载完成后调用ProgressDialogdismiss()来关闭对话框;

ProgressDialog dialog = new ProgressDialog(this); //1.创建一个ProgressDialog的实例

dialog.setMessage("正在加载中");dialog.show();//5.ProgessDialog显示出来
 

感兴趣同学可以思考:如何做出这种效果:(过时了)

 

//参考核心代码:

final ProgressDialog dialog = new ProgressDialog(this);

        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

        dialog.setMessage("正在加载中");

        dialog.setMax(100);

        final Timer timer = new Timer();

        timer.schedule(new TimerTask() {

            int progress = 0;

 

            @Override

            public void run() {

                dialog.setProgress(progress += 5);

                if (progress == 100) {

                    timer.cancel();

                }

            }

        }, 0, 1000);

        dialog.show();

 


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

相关文章

AlertDialog对话框的简单使用

目录 一、对话框的创建 二、单选的对话框 三、多选的对话框 一、对话框的创建 一般的对话框分为标题、内容、按钮三大部分。 常见的方法&#xff1a; 方法功能setTitle()设置对话框的标题setIcon()设置对话框的图标setMessage()设置对话框的提示信息setPositiveButton()设…

alertDialog使用详解

1、设置标题、内容、图标 2、设置按钮 3、使用列表、单选和多选。适配 4、设定弹窗大小 5、自定义view 6、设置点击周边灰色区域弹窗不消失 7、自定义view圆角消除周边白块 1、设置标题、内容、图标 AlertDialog alertDialog new AlertDialog.Builder(this) .setTit…

AlertDialog6种使用方法

AlertDialog 1.AlertDialog的6种创建模式 1.1setMessage 1&#xff09;Java代码 //1.创建构造器AlertDialog.Builder buildernew AlertDialog.Builder(this);//2.设置参数builder.setTitle("弹窗提示").setIcon(R.mipmap.boy).setMessage("选择你的性别&#xf…

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控件里面添加…