【Android Dialog】Dialog

article/2025/9/20 21:57:34

在这里插入图片描述
在这里插入图片描述

AlertDialog

Dialog类是所有弹窗的父类,官方建议我们不要直接实例化它,而是使用其子类来获取实例。AlertDialog是系统提供的一个直接子类,它能帮助我们快速构建出不同类型的弹窗。接下来就看下各种类型弹窗的使用。

1、普通对话框
/*** AlertDialog默认UI样式* */
private fun showNormalAlertDialog() {val builder = AlertDialog.Builder(this).setIcon(R.mipmap.ic_launcher).setTitle("Dialog Title").setMessage("Dialog Message").setPositiveButton("Sure", object : DialogInterface.OnClickListener {override fun onClick(dialog: DialogInterface?, which: Int) {Toast.makeText(applicationContext, "Sure", Toast.LENGTH_SHORT).show()}}).setNegativeButton("Cancel", object : DialogInterface.OnClickListener {override fun onClick(dialog: DialogInterface?, which: Int) {Toast.makeText(applicationContext, "Cancel", Toast.LENGTH_SHORT).show()}})//Neutral按钮,显示在dialog的最左面。.setNeutralButton("Neutral", object : DialogInterface.OnClickListener {override fun onClick(dialog: DialogInterface?, which: Int) {Toast.makeText(applicationContext, "Neutral", Toast.LENGTH_SHORT).show()}})val dialog = builder.create()dialog.show()
}

在这里插入图片描述

2、单选对话框
    private fun showSingleChoiceAlertDialog(){val subj = arrayOf("android", "linux", "java", "ios", "c", "html")val builder = AlertDialog.Builder(this@AlertDialogActivity).setIcon(R.mipmap.ic_launcher).setTitle("单选对话框")// 第二个参数为默认选中项 在这里设为第一项.setSingleChoiceItems(subj, 0) { dialog, which ->Toast.makeText(this@AlertDialogActivity, "我选择" + subj[which] + "这门课", Toast.LENGTH_SHORT).show()}.setPositiveButton("提交") { dialog, which ->Toast.makeText(this@AlertDialogActivity, "您已经提交您的选择", Toast.LENGTH_SHORT).show()}.setNeutralButton("取消"){dialog, which ->Toast.makeText(this@AlertDialogActivity, "您点击了取消按钮", Toast.LENGTH_SHORT).show()}val dialog = builder.create()dialog.show()}

在这里插入图片描述

3、多选对话框
    private fun showMultipleChoiceAlertDialog() {val subj = arrayOf("android", "linux", "java", "ios", "c")val builder = AlertDialog.Builder(this).setIcon(R.mipmap.ic_launcher).setTitle("多选对话框").setMultiChoiceItems(subj, null) { dialog, which, isChecked ->if (isChecked) {Toast.makeText(this@AlertDialogActivity,"我喜欢" + subj[which],Toast.LENGTH_SHORT).show()} else {Toast.makeText(this@AlertDialogActivity,"我不喜欢" + subj[which],Toast.LENGTH_SHORT).show()}}.setPositiveButton("提交") { dialog, which ->Toast.makeText(this@AlertDialogActivity, "您已经提交您的选择", Toast.LENGTH_SHORT).show()}.setNeutralButton("取消") { dialog, which ->Toast.makeText(this@AlertDialogActivity, "您关闭了弹窗", Toast.LENGTH_SHORT).show()}val dialog = builder.create()dialog.show()}

在这里插入图片描述

4、列表对话框
    /*** 列表对话框,内容多了就可滚动。这个dialog感觉没啥用。* ps:当单选、多选对话框的内容多了也可滚动。* */private fun showListAlertDialog() {val subj = arrayOf("android", "linux", "java", "ios", "c","kotlin")val builder = AlertDialog.Builder(this).setIcon(R.mipmap.ic_launcher).setTitle("列表对话框").setItems(subj) { dialog, which ->Toast.makeText(this@AlertDialogActivity, "您选择了" + subj[which], Toast.LENGTH_SHORT).show()}val dialog = builder.create()dialog.show()}

在这里插入图片描述

5、自定义对话框

AlertDialog的UI我们不满意时也是可以稍加定制的。

    /*** 基于AlertDialog进行自定义对话框* 标题、title的UI格式已经被定制过了,我们可以定义其下方区域的ui* */private fun showCustomAlertDialog() {val builder = AlertDialog.Builder(this)builder.setIcon(R.mipmap.ic_launcher)builder.setTitle("Custom AlertDialog")val inflater = LayoutInflater.from(this)val v: View = inflater.inflate(R.layout.layout_custom_alert_dialog, null)builder.setView(v)val dialog = builder.create()dialog.show()}

在这里插入图片描述

6、小结

可以看出AlertDialog的UI是系统帮我们定制过的:

(1)上方区域:左上角icon、iocn旁边是标题。

(2)下方区域:不同类型的AlertDialog下方区域各自实现。

系统还提供了api可自定义下方区域的UI。

其他对话框

1、DatePickerDialog

系统提供的一个默认的日期选择对话框

/**context – the parent contextthemeResId – the resource ID of the theme against which to inflate this dialog,or 0to use the parent context's default alert dialog themelistener – the listener to call when the user sets the dateyear – the initially selected yearmonthOfYear – the initially selected month of the year(0-11 for compatibility with Calendar.MONTH)dayOfMonth – the initially selected day of month(1-31,depending on month)*/
public DatePickerDialog (@NonNull Context context,@StyleRes int themeResId,@Nullable OnDateSetListener listener,int year,int monthOfYear,int dayOfMonth)
private fun datePickerDialog() {DatePickerDialog(this, object : DatePickerDialog.OnDateSetListener {override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {Toast.makeText(applicationContext, "$year-$month-$dayOfMonth", Toast.LENGTH_SHORT).show()}}, 2023, 3, 15).show()
}

在这里插入图片描述

上面是默认的UI,然而它还有另外一种UI,只需构造中传递一个UI参数AlertDialog.THEME_HOLO_LIGHT控制下即可:

在这里插入图片描述

2、TimePickerDialog

系统提供的一个默认的时间选择对话框

```java
/*** context – the parent context** themeResId – the resource ID of the theme to apply to this dialog** listener – the listener to call when the time is set** hourOfDay – the initial hour** minute – the initial minute** is24HourView – Whether this is a 24 hour view, or AM/PM.** */
public TimePickerDialog(Context context, int themeResId, OnTimeSetListener listener,int hourOfDay, int minute, boolean, is24HourView)
    private fun timePickDialog() {TimePickerDialog(this, object : TimePickerDialog.OnTimeSetListener {override fun onTimeSet(view: TimePicker?, hourOfDay: Int, minute: Int) {// todo}},22,56,true).show()}

在这里插入图片描述

上面是默认的UI,然而它还有另外一种UI,只需构造中传递一个UI参数AlertDialog.THEME_HOLO_LIGHT控制下即可:
在这里插入图片描述

3、ProgressDialog

已经弃用,官方建议使用ProgressBar代替。

4、DialogFragment

DialogFragment本质上是一个Fragment,也就具有Fragment所拥有的生命周期。在使用时,更容易通过生命周期回调来管理弹窗。对于复杂样式的弹窗,使用DialogFragment更加方便和高效。

自定义Dialog

系统提供的AlertDialog还是有一定的局限性的,开发中不一定会满足我们的需求,此时我们就需要自定义Dialog了。

最近在开发购物车流程中看到UI设计了好多弹窗,这个些弹窗有相似点:

  • 底部弹窗
  • 顶部两个角是圆角
  • 各个弹窗各自内容自定义

在这里插入图片描述

接下来就以这个简单的需求来实现下自定义Dialog

1、核心代码
/*** Create by SunnyDay /04/23 18:06:24*/
class BottomDialog @JvmOverloads constructor(@UiContext mContext: Context,dialogStyle: Int = R.style.BottomDialog,@LayoutRes layoutId: Int
) : Dialog(mContext, dialogStyle) {init {setContentView(layoutId)makeDialogBottom()}private fun makeDialogBottom() {window?.apply {setGravity(Gravity.BOTTOM)attributes.apply {width = ViewGroup.LayoutParams.MATCH_PARENTheight = ViewGroup.LayoutParams.WRAP_CONTENT}}}fun <T : View> getViewById(resId: Int): T {return findViewById<View>(resId) as T}
}
2、BottomDialog#style

稍微处理下dialog的样式

    <!--自定义dialog的样式--><style name="BottomDialog" parent="@style/AlertDialog.AppCompat"><!--无边框--><item name="android:windowFrame">@null</item><!--浮现在activity之上--><item name="android:windowIsFloating">true</item><!--半透明--><item name="android:windowIsTranslucent">false</item><!--无标题--><item name="android:windowNoTitle">true</item><!--提示框背景--><item name="android:windowBackground">@android:color/transparent</item><!-- 触摸dialog以外的地方可关闭dialog--><item name="android:windowCloseOnTouchOutside">true</item></style>
3、BottomDialog#bg

白色背景&上面两个角是圆角

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><solid android:color="@color/white" /><cornersandroid:topLeftRadius="20dp"android:topRightRadius="20dp" />
</shape>
4、食用
    private fun showCustomDialog() {//注意这里直接使用第三个参数时需要写参数名val bottomDialog = BottomDialog(this, layoutId = R.layout.dialog_permission)bottomDialog.show()bottomDialog.getViewById<View>(R.id.sure).setOnClickListener {Toast.makeText(applicationContext,"got permission !",Toast.LENGTH_SHORT).show()bottomDialog.dismiss()}bottomDialog.getViewById<View>(R.id.cancel).setOnClickListener {Toast.makeText(applicationContext,"no permission !",Toast.LENGTH_SHORT).show()bottomDialog.dismiss()}}

小结

1、本章能够了解到
  • AlertDialog 基本用法
  • Dialog的关系图
  • 如何自定义dialog
  • @JvmOverloads关键字作用
  • kotlin具名参数注意点
2、感悟

有没有发现一个问题,dialog需要的context我们传递非activity的context(如service的或者Application的)会怎样?dialog深究起来也是一块知识点,以后再总结喽。完事,溜了~

官方文档


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

相关文章

DIALOG

[SAP]屏幕Dynpro 原文链接&#xff1a;http://www.cnblogs.com/jiangzhengjun/p/4292250.html 对话屏幕Dynpro(SE51). 11 屏幕元素... 11 屏幕属性... 11 PAI事件的触发、屏幕元素Function Code设置... 12 屏幕流逻辑Screen Flow Logic. 12 对话屏幕中的字段命名大小写问题...…

HTML基础之表单提交

表单提交 在网页上&#xff0c;一般都会有让你输入信息进行校验或者检索的地方&#xff0c;比如搜索栏或者输入账号密码进行登录的地方&#xff0c;当我们输入数据之后&#xff0c;单击确定或者搜索等按键的时候&#xff0c;网页就会把我们输入到输入框中的信息发送到与前端网…

JavaScript之Ajax-axios表单提交

目录 一.表单概念 二.表单提交 三.FormData语法 四.头像上传模板 五.请求体类型 六.图书管理(增删改查) 七.axios语法优化写法 优化1: axios全局配置 优化2: 默认的method 优化3: axios的快捷方法 一.表单概念 form标签&#xff08;表单&#xff09;是用来收集用户…

表单提交后跳转指定链接

表单的作用可以根据场景的变化而变化&#xff0c;可以是问卷调查、故障报修、自助下单等多种业务场景。如需提交后进行下一步操作&#xff0c;如成功提交咨询申请后自动跳转到咨询详情页&#xff0c;可以通过设置提交扩展&#xff0c;实现表单提交后跳转指定链接。 当跳转开启…

jquery form表单提交

使用jquery 进行form表单提交 文章目录 使用jquery 进行form表单提交先上结果&#xff1a;代码html代码 ---index.htmljs代码---index.js代码用到的jquery.js、select2的css和js在我项目里面有 下载地址 我这里使用了多选框&#xff08;传输到后端时&#xff0c;用对象接收&…

表单提交和超链接请求传递参数的几种方式

表单提交和超链接请求传递参数的几种方式 这段时间在使用easy-ui的datagrid&#xff0c;他有自己提交表单的方式&#xff0c;所以就整理整理页面对参数的提交方式&#xff1a; 注&#xff1a;下面代码都已经过测试。 1. HTML提交表单 HTML提交表单简单易操作&#xff0c;依靠…

模拟html post表单提交

一、打开自动提交访问 设置好表单账号密码&#xff0c;然后访问指定地址。就能自动提交登录并跳转到首页 1、填写访问表单信息 设置好表单账号密码 <input name"account" <input id"password" name"password" 然后填上访问地址action …

02 Ajax表单提交

目录 一、表单概念 1.表单 2.提交的两种方式 二、form-serize与FormData 1.form-serize 2.FormData 三、文件上传案例分析 以下的既为本文的核心概括 一、表单概念 1.表单 form标签&#xff08;表单&#xff09;是用来收集用户输入的信息。 表单构成&#xff1a; 表单标签&a…

Form表单提交

Form表单提交 form表单提交&#xff0c;表单提交分两种&#xff1a;自动提交 和 手动提交&#xff0c; form表单提交又分&#xff1a;post提交和get提交&#xff0c;以下用的都是post提交 一.自动提交表单&#xff1a; 自动提交表单&#xff0c;我们可以通过框架引用&#…

html 提交form表单提交数据格式,form表单提交数据

form表单提交的几种方法 HTML表单提交的几种方式方式一:通过submit按钮提交方式二:通过一般按钮button提交1/3javascript">functionsubmit1(){varform1=document.getElementById("form1");form1.action="bjpowernode.html";form1.submit();方式三…

JavaScript表单提交

表单提交在前端编程阶段中也是一个重点。它是由页面数据保存到后台数据库的重要枢纽&#xff0c;通过表单提交的方式将数据上传到数据库进行保存。同时这些数据也包含用户信息、统计信息、日志信息等等。 数据的信息不同&#xff0c;上传的方式也不同。在JavaScript中有四种种表…

表单提交的四种方式

表单提交的四种方式开发工具与关键技术&#xff1a;VS &#xff0c;JavaScript &#xff0c;Ajax 作者&#xff1a;刘任锋 撰写时间&#xff1a;2021年5月7日内容&#xff1a;将from表单里的内容提交到控制器上。 HTML布局 JS&#xff1a;先获取姓名&#xff0c;性别&#xff0…

表单的提交方式

表单有两个较特殊的属性&#xff0c;一个是method&#xff0c;是规定用于发送表单的HTTP方法&#xff08;提交表单的方式&#xff09;&#xff1b;还有一个是action&#xff0c;是规定当提交时向何处发送表单的数据&#xff08;要提交表单的地址&#xff09;。Form表单有一个自…

InputStream的read()读取机制

public void readArr() {// 明确文件File file new File("D:/net.txt");// 构建流的对象InputStream inputStream null;try {inputStream new FileInputStream(file);// 声名缓冲数组int i;byte[] bytes new byte[5];while ((i inputStream.read(bytes)) ! -1) …

java中的InputStream,OutputStream,Read,Writer

Java 中定义了两种类型的流&#xff1a;字节型&#xff0c;和字符型。 字节流&#xff1a;处理字节的输入和输出。包括读写二进制数据等方面的内容。 字符流&#xff1a;处理字符的输入和输出。他采用的是 Unicode 编码&#xff0c;可以实现国际化。使用字符流的另外一个好处…

InputStreamReader和OutputStreamWriter 的区别和用法

一、InputStreamReader 用于将一个字节流中的字节解码成字符 &#xff0c; 用法如下 Testpublic void Test19() throws Exception {InputStream in new FileInputStream("C:/hello.txt");// 读取文件的数据,注意文件编码为UTF-8,防止读取乱码// 将输入的字节流 ---…

InputStream.read() 和 OutputStream.write()方法

InputStream.read() 和 OutputStream.write()方法组合使用可以完成文件的复制功能。 先贴出代码 InputStream inputStream new FileInputStream(file);OutputStream os response.getOutputStream();byte[] b new byte[2048];int size;while ((size inputStream.read(b)) &…

InputStream 、 InputStreamReader和BufferedReader

在Java中&#xff0c;上述三个类经常用于处理数据流&#xff0c;下面介绍一下三个类的不同之处以及各自的用法。 InputStream &#xff1a; 是所有字节输入流的超类&#xff0c;一般使用它的子类&#xff1a;FileInputStream等&#xff0c;它能输出字节流&#xff1b;InputStre…

Java转换流(InputStreamReader/OutputStreamWriter)

文章目录 概述为什么会有转换流&#xff1f;InputStreamReaderOutputStreamWriter 概述 转换流是字节流到字符流的桥梁&#xff0c;在转换的过程中&#xff0c;可以指定编码。转换流也是一种处理流&#xff0c;它提供了字节流和字符流之间的转换。 转换流的两个类 InputStrea…

InputStream read()方法详解

在Java7中&#xff0c;InputStream被定义为一个抽象类&#xff0c;相应的&#xff0c;该类下的read()方法也是一个抽象方法&#xff0c;这也就意味着必须有一个类继承InputStream并且实现这个read方法。   查阅Java7 API&#xff0c;我们可以看到&#xff0c;在InputStream中…