Android Dialog使用详解

article/2025/9/26 12:04:11

对话框是提示用户作出决定或输入额外信息的小窗口,通常不会填充整个屏幕,用于进行一些额外交互

Dialog 类是对话框的基类,但应该避免直接实例化 Dialog,而应使用其子类,比如

  • AlertDialog 
    此对话框可显示标题、提示信息、按钮、可选择项列表或自定义布局等
  • DatePickerDialog 或 TimePickerDialog 
    此类对话框带有允许用户选择日期或时间的预定义 UI

这些类定义了对话框的样式和结构,但开发者应该将 DialogFragment 用作对话框的容器。DialogFragment 类提供了创建对话框和管理其外观所需的所有控件,而不是调用 Dialog 对象上的方法 
使用 DialogFragment 管理对话框可确保它能正确处理生命周期事件,如用户按“返回”按钮或旋转屏幕时。 此外,DialogFragment 类还允许将对话框的 UI 作为嵌入式组件在较大 UI 中重复使用,就像传统 Fragment 一样

DialogFragment 类最初是通过 Android 3.0(API 11)添加的,如果想要使应用可以在运行 Android 1.6 或更高版本的设备上使用 DialogFragment 以及各种其他 API,可以使用支持库附带的 DialogFragment 类(android.support.v4.app.DialogFragment)。如果应用支持的最低版本是 API 级别 11 或更高版本,则可使用 DialogFragment 的框架版本(android.app.DialogFragment)

一、基础用法

通过扩展 DialogFragment 类并在 onCreateDialog() 回调方法中创建 AlertDialog

public class MyDialogFragment extends DialogFragment {@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());builder.setTitle("title").setMessage("message").setPositiveButton("确定", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int id) {}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int id) {}});return builder.create();}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

之后,当创建此类的实例并调用该对象上的 show() 方法时,就可以显示出对话框了 
这是 DialogFragment 最基础用法,之后会介绍几种不同的样式

二、包含两个按钮的对话框

在 ButtonDialogFragment 类中重载 show() 方法,传入所有需要的参数,并调用 DialogFragment 类本身的 show(FragmentManager manager, String tag) 方法,从而显示对话框。”tag” 参数是系统用于保存片段状态并在必要时进行恢复的唯一标记名称,该标记还允许通过调用 findFragmentByTag() 获取片段的句柄

/*** Created by 叶应是叶 on 2017/2/23.*/public class ButtonDialogFragment extends DialogFragment {private DialogInterface.OnClickListener positiveCallback;private DialogInterface.OnClickListener negativeCallback;private String title;private String message;public void show(String title, String message, DialogInterface.OnClickListener positiveCallback,DialogInterface.OnClickListener negativeCallback, FragmentManager fragmentManager) {this.title = title;this.message = message;this.positiveCallback = positiveCallback;this.negativeCallback = negativeCallback;show(fragmentManager, "ButtonDialogFragment");}@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());builder.setTitle(title);builder.setMessage(message);builder.setPositiveButton("确定", positiveCallback);builder.setNegativeButton("取消", negativeCallback);return builder.create();}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

之后,提供调用方法

public void showButtonDialogFragment(View view) {ButtonDialogFragment buttonDialogFragment = new ButtonDialogFragment();buttonDialogFragment.show("Hi,你好", "叶应是叶", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this, "点击了确定 " + which, Toast.LENGTH_SHORT).show();}}, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this, "点击了取消 " + which, Toast.LENGTH_SHORT).show();}}, getFragmentManager());}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

运行效果 
这里写图片描述

三、包含一个中性按钮的对话框

思路类似,对话框包含一个中性按钮

/*** Created by ZY on 2017/2/23.*/public class NeutralDialogFragment extends DialogFragment {private DialogInterface.OnClickListener neutralCallback;private String title;private String message;private String hint;public void show(String title, String message, String hint, DialogInterface.OnClickListener neutralCallback,FragmentManager fragmentManager) {this.title = title;this.message = message;this.hint = hint;this.neutralCallback = neutralCallback;show(fragmentManager, "NeutralDialogFragment");}@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());builder.setTitle(title);builder.setMessage(message);builder.setNeutralButton(hint, neutralCallback);return builder.create();}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

提供调用方法

    public void showNeutralDialogFragment(View view) {NeutralDialogFragment neutralDialogFragment = new NeutralDialogFragment();neutralDialogFragment.show("Hi,你好", "叶应是叶", "确定~", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this, "点击了按钮 " + which, Toast.LENGTH_SHORT).show();}}, getFragmentManager());}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

运行效果 
这里写图片描述

四、列表对话框

/*** Created by ZY on 2017/2/23.*/public class ItemsDialogFragment extends DialogFragment {private String title;private String[] items;private DialogInterface.OnClickListener onClickListener;public void show(String title, String[] items, DialogInterface.OnClickListener onClickListener,FragmentManager fragmentManager) {this.title = title;this.items = items;this.onClickListener = onClickListener;show(fragmentManager, "ItemsDialogFragment");}@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());builder.setTitle(title).setItems(items, onClickListener);return builder.create();}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

提供调用方法

public void showItemsDialogFragment(View view) {ItemsDialogFragment itemsDialogFragment = new ItemsDialogFragment();String[] items = {"Hi", "Hello", "叶"};itemsDialogFragment.show("Hi,你好", items, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this, "点击了第 " + (which + 1) + " 个选项", Toast.LENGTH_SHORT).show();}}, getFragmentManager());}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

运行效果 
这里写图片描述

五、多项选择对话框

/*** Created by ZY on 2017/2/23.*/public class MultiChoiceDialogFragment extends DialogFragment {private String title;private String[] items;private DialogInterface.OnMultiChoiceClickListener onMultiChoiceClickListener;private DialogInterface.OnClickListener positiveCallback;public void show(String title, String[] items, DialogInterface.OnMultiChoiceClickListener onMultiChoiceClickListener,DialogInterface.OnClickListener positiveCallback, FragmentManager fragmentManager) {this.title = title;this.items = items;this.onMultiChoiceClickListener = onMultiChoiceClickListener;this.positiveCallback = positiveCallback;show(fragmentManager, "MultiChoiceDialogFragment");}@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());builder.setTitle(title).setMultiChoiceItems(items, null, onMultiChoiceClickListener).setPositiveButton("确定", positiveCallback);return builder.create();}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

提供调用方法

public void showMultiChoiceDialogFragment(View view) {MultiChoiceDialogFragment multiChoiceDialogFragment = new MultiChoiceDialogFragment();final String[] items = {"Hi", "Hello", "叶"};final List<Integer> integerList = new ArrayList<>();multiChoiceDialogFragment.show("Hi,你好", items, new DialogInterface.OnMultiChoiceClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which, boolean isChecked) {if (isChecked) {integerList.add(which);} else {integerList.remove(which);}}}, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {String hint = "";for (int i = 0; i < integerList.size(); i++) {hint = items[integerList.get(i)] + hint;}Toast.makeText(MainActivity.this, hint, Toast.LENGTH_SHORT).show();}}, getFragmentManager());}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

运行效果 
这里写图片描述

六、单项选择对话框

/*** Created by ZY on 2017/2/23.*/public class SingleChoiceDialogFragment extends DialogFragment {private String title;private String[] items;private DialogInterface.OnClickListener onClickListener;private DialogInterface.OnClickListener positiveCallback;public void show(String title, String[] items, DialogInterface.OnClickListener onClickListener,DialogInterface.OnClickListener positiveCallback, FragmentManager fragmentManager) {this.title = title;this.items = items;this.onClickListener = onClickListener;this.positiveCallback = positiveCallback;show(fragmentManager, "SingleChoiceDialogFragment");}@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());builder.setTitle(title).setSingleChoiceItems(items, 0, onClickListener).setPositiveButton("确定", positiveCallback);return builder.create();}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

提供调用方法

  private int index;public void showSingleChoiceDialogFragment(View view) {SingleChoiceDialogFragment singleChoiceDialogFragment = new SingleChoiceDialogFragment();String[] items = {"Hi", "Hello", "叶"};singleChoiceDialogFragment.show("Hi,你好", items, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {index = which;}}, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this, "选择了第" + (index + 1) + "项", Toast.LENGTH_SHORT).show();}}, getFragmentManager());}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

运行效果 
这里写图片描述

七、自定义对话框

可以通过创建一个自定义布局,然后调用 AlertDialog.Builder 对象上的 setView() 方法将其添加到 AlertDialog 中,从而让对话框拥有自定义样式

首先自定义布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"><ImageView
        android:layout_width="match_parent"android:layout_height="64dp"android:background="#FFFFBB33"android:contentDescription="@string/app_name"android:scaleType="fitXY"android:src="@drawable/head" /><EditText
        android:id="@+id/username"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="4dp"android:layout_marginLeft="4dp"android:layout_marginRight="4dp"android:layout_marginTop="16dp"android:hint="用户名"android:inputType="textEmailAddress" /><EditText
        android:id="@+id/password"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="16dp"android:layout_marginLeft="4dp"android:layout_marginRight="4dp"android:layout_marginTop="4dp"android:fontFamily="sans-serif"android:hint="密码"android:inputType="textPassword" /></LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

在 DialogFragment 的 onCreateDialog() 方法中加载自定义布局,并添加到 AlertDialog.Builder 中 
此外,自定义一个接口 Callback,用于获取用户名与密码

/*** Created by ZY on 2017/2/23.*/public class ViewDialogFragment extends DialogFragment {public interface Callback {void onClick(String userName, String password);}private Callback callback;public void show(FragmentManager fragmentManager) {show(fragmentManager, "ViewDialogFragment");}@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());LayoutInflater inflater = getActivity().getLayoutInflater();final View view = inflater.inflate(R.layout.dialog_signin, null);builder.setView(view).setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {if (callback != null) {EditText et_userName = (EditText) view.findViewById(R.id.username);EditText et_password = (EditText) view.findViewById(R.id.password);callback.onClick(et_userName.getText().toString(), et_password.getText().toString());}}});return builder.create();}@Overridepublic void onAttach(Context context) {super.onAttach(context);if (context instanceof Callback) {callback = (Callback) context;} else {throw new RuntimeException(context.toString() + " must implement Callback");}}@Overridepublic void onDestroy() {super.onDestroy();callback = null;}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

令 Activity 实现 Callbak 接口

public class MainActivity extends AppCompatActivity implements ViewDialogFragment.Callback {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void showViewDialogFragment(View view) {ViewDialogFragment viewDialogFragment = new ViewDialogFragment();viewDialogFragment.show(getFragmentManager());}@Overridepublic void onClick(String userName, String password) {Toast.makeText(MainActivity.this, "用户名: " + userName + " 密码: " + password, Toast.LENGTH_SHORT).show();}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

运行效果 
这里写图片描述


http://chatgpt.dhexx.cn/article/26yVeNYV.shtml

相关文章

Android 修改AlertDialog原生setPositiveButton的字体颜色背景颜色大小边距位置

看效果图&#xff1a; public void lanyaClick(View v) {//点击确定之后转向登陆框LayoutInflater factory LayoutInflater.from(Beforestart.this);//得到自定义对话框final View DialogView factory.inflate(R.layout.item_alert_dialog, null);//创建对话框android.app.Al…

setPositiveButton和setNegativeButton的区别

setPositiveButton和setNegativeButton的区别和setNeutralButton的区别 三者都是AlertDialog弹出框的按钮&#xff0c;都是封装好的button&#xff0c;只是显示的位置不同&#xff0c;项目中可根据情况选择使用&#xff0c;setNegativeButton一般用于确认&#xff0c;setNegat…

GPS(rinex格式)数据解析详细解读

RINEX格式现如今已成为GPS测量应用中的标准数据格式&#xff0c;目前应用最为广泛、最普遍的是RINEX格式的第2个版本&#xff0c;该版本能够用于包括静态和动态GPS测量在内的不同观测模式数据。在该版本中定义了6种不同类型的数据文件&#xff0c;分别用于存放不同类型的数据&a…

2020/10/23 GPS的数据格式学习

GPS的数据格式学习 一、在使用GPS的通过串口向电脑发送数据的时候&#xff0c;要注意GPS数据线的连接&#xff1b; 1.1 VCC接VCC&#xff1b;&#xff08;VCC表示接电源正极&#xff09; 1.2 GND接GND&#xff1b;&#xff08;GND表示接地或接电源负极&#xff09; 1.3 TX接RX…

GPS数据包格式+数据解析

GPS数据包格式数据解析 一、全球时区的划分&#xff1a; 每个时区跨15经度。以0经线为界向东向西各划出7.5经度&#xff0c;作为0时区。即0时区的经度范围是7.5W——7.5E。从7.5E与7.5W分别向东、向西每15经度划分为一个时区&#xff0c;直到东11区和西11区。东11区最东部的经度…

GPS研究---GPS 数据格式

GPS 数据处理时所采用的观测数据是来自观测的 GPS 接收机。由于接收机的型号很多&#xff0c;厂商设计的数据格式各不相同&#xff0c;国际上为了能统一使用不同接收机的数据&#xff0c; 设计了一种与接收机无关的 RINEX(The Receiver Independent Exchange Format)格式&#…

GPS数据格式的分析

文章目录 前言一、数据格式解析1、GPGGA2、GPRMC3、GPCHC4、Kitti数据集oxts数据 二、驱动1、功能包1.1 解析GPGGA1.2 华测GPCHC 2、ROS相关消息类型2.1 sensor_msgs::NavSatFix2.2 gps_common::GPSFix2.3 sensor_msgs::Imu 3、驱动思路 三、时间1、UTC时间2、时间戳 前言 GPS…

GPS GLONASS数据文件类型解析

GPS & GLONASS数据文件类型解析 一、GPS数据格式 RINEX格式现如今已成为GPS测量应用中的标准数据格式&#xff0c;目前应用最为广泛、最普遍的是RINEX格式的第2个版本&#xff0c;该版本能够用于包括静态和动态GPS测量在内的不同观测模式数据。在该版本中定义了6种不同类…

GPS的数据格式介绍

GPRMC&#xff08;建议使用最小GPS数据格式&#xff09; $GPRMC,<1>,<2>,<3>,<4>,<5>,<6>,<7>,<8>,<9>,<10>,<11><CR><LF> 1) 标准定位时间&#xff08;UTC time&#xff09;格式&#xff1a…

Android ExpandableListView

ExpandableListView可以显示一个视图垂直滚动显示两级列表中的条目&#xff0c;这不同于列表视图&#xff08;ListView&#xff09;。ExpandableListView允许有两个层次&#xff1a;一级列表中有二级列表。 比如在手机设置中&#xff0c;对于分类&#xff0c;有很好的效果。手机…

ExpandableListView用法

先上个效果图&#xff1a; 1&#xff0c;我用的fragment import java.util.ArrayList; import java.util.Collections; import java.util.List; import com.dami.student.ui.chatui.adapter.ContactsExpandableListAdapter; import com.dami.student.R; import android.conten…

android expandablelistview简单应用,android ExpandableListView简单例子

android中常常要用到ListView&#xff0c;有时也要用到ExpandableListView&#xff0c;如在手机设置中&#xff0c;对于分类有很好的效果&#xff0c;会用ListView的人一定会用ExpandableListView&#xff0c;因为 ExpandableListView extends ListView的&#xff0c;下面来看个…

android expandablelistview横向,完美实现ExpandableListView二级分栏效果

本文实例为大家分享了ExpandableListView二级分栏效果的具体代码&#xff0c;供大家参考&#xff0c;具体内容如下 对ExpandableListView控件进行封装(未自定义)直接上代码&#xff1a; 通用ViewHolder类&#xff0c;仅在setImageResource中添加代码 package com.svp.haoyan.ex…

android expandablelistview横向,expandableListView 总结

实现效果图&#xff1a; expandableListView groupIndicator 图片默认是在左边&#xff0c;而且比较难看&#xff0c;而我要的是实现groupIndicator 在右边自定义图片&#xff0c; 换图片 最简单的就是直接copy 系统 android:drawable/expander_group ?android:attr/expandab…

Android学习之ExpandableListView

什么是ExpandableListView ExpandableListView是扩展的ListView&#xff0c;继承自ListView&#xff1b;ExpandableListView可以实现点击展开列表&#xff0c;再点击收缩回去的效果。 ExpandableListView的使用 首先需要在主布局文件中声明ExpandableListView&#xff1b; …

ExpandableListView详解

文章目录 效果图ExpandableListView的简介与使用去掉ExpandableListView的箭头以及自定义Indicator解决setOnChildClickListener失效问题解决collapseGroup(i)崩溃问题解决group_item.xml中包含CheckBox、EditText等&#xff0c;点击不能展开的问题 1.效果图 2.ExpandableLi…

values_list()

转载&#xff1a;https://www.cnblogs.com/chenchao1990/p/5311531.html?utm_sourcetuicool&utm_mediumreferral

列表(lists)

Lists and the things you can do with them.Includes indexing(索引&#xff09;,slicing &#xff08;切片&#xff09;and mutating&#xff08;变异&#xff09;. 1.Python 中的列表表示有序的值序列。 以下是如何创建它们的示例&#xff1a; primes [2,3,5,7] #我们可以…

Android Preference API 用法--ListPreference(一)

一&#xff0e;ListPreference简介 我们都只知道SharedPreference非常适合于参数设置功能&#xff0c;在此处的preference 也是代表SharedPreference的意思&#xff0c;在SharedPreference中&#xff0c;我们可以迅速的将某些值保存进xml文件中&#xff0c;然后我们可以读取这…

android entries属性,ListPreference需要设置两个属性:android:entries和android:entryValues...

android:defaultValue"black" android:entries"array/setting_skintheme" android:entryValues"array/setting_skintheme_value" android:key"SkinTheme" android:summary"请选择您喜欢的软件皮肤颜色" android:title"…