Android———最详细的系统对话框(AlertDialog)详解

在实际应用开发中,用到系统对话框中的情况几乎是没有的。按开发流程来说,UI工程师都会给出每一个弹窗的样式,故而在实际开发中都是自定义弹窗的。
即使用到的地方不多,但是我们也是需要了解并且能熟练的运用它,下面为大家奉上各种系统对话框的实现。
目录

系统对话框的几种类型与实现
在项目的实际开发中,用到的系统对话框几乎是没有的。原因大概包含以下几点:
- 样式过于单一,不能满足大部分实际项目中的需求。
- 对话框的样式会根据手机系统版本的不同而变化。不能达到统一的样式。
- 能实现的功能过于简单。
在这里先附上下面代码中出现文本的string.xml文件。
<string name="dialog_normal_content">我是普通dialog</string>
<string name="dialog_normal_more_button_content">我是普通多按钮dialog</string>
<string name="dialog_btn_confirm_text">确定</string>
<string name="dialog_btn_cancel_text">取消</string>
<string name="dialog_btn_neutral_text">忽略</string>
<string name="dialog_btn_confirm_hint_text">您点击了确定按钮</string>
<string name="dialog_btn_cancel_hint_text">您点击了取消按钮</string>
<string name="dialog_btn_neutral_hint_text">您点击了忽略按钮</string>
1、普通对话框
在实际项目开发中,此类型对话框中用到的地方要比其他类型的对话框多一些。但是考虑UI统一问题,也会很少用。
运行截图:

代码:
private void showNormalDialog(){
//创建dialog构造器
AlertDialog.Builder normalDialog = new AlertDialog.Builder(this);
//设置title
normalDialog.setTitle(getString(R.string.dialog_normal_text));
//设置icon
normalDialog.setIcon(R.mipmap.ic_launcher_round);
//设置内容
normalDialog.setMessage(getString(R.string.dialog_normal_content));
//设置按钮
normalDialog.setPositiveButton(getString(R.string.dialog_btn_confirm_text)
, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this,getString(R.string.dialog_btn_confirm_hint_text)
,Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
//创建并显示
normalDialog.create().show();
}
系统对话框都是支持链式调用的,举例:
new AlertDialog.Builder(this)
.setTitle(getString(R.string.dialog_normal_text))
.setIcon(R.mipmap.ic_launcher_round)
.setMessage(getString(R.string.dialog_normal_content))
.setPositiveButton(getString(R.string.dialog_btn_confirm_text)
, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this,getString(R.string.dialog_btn_confirm_hint_text)
,Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
})
.create()
.show();
下面的代码都是可以用链式调用的,这里就不展示了。
2、普通对话框(多按钮)
在系统对话框中最多出现三个按钮,即PositiveButton(确定)、NegativeButton(取消)、NeutralButton(忽略)。
运行截图:

代码:
private void showNormalMoreButtonDialog(){
AlertDialog.Builder normalMoreButtonDialog = new AlertDialog.Builder(this);
normalMoreButtonDialog.setTitle(getString(R.string.dialog_normal_more_button_text));
normalMoreButtonDialog.setIcon(R.mipmap.ic_launcher_round);
normalMoreButtonDialog.setMessage(getString(R.string.dialog_normal_more_button_content));
//设置按钮
normalMoreButtonDialog.setPositiveButton(getString(R.string.dialog_btn_confirm_text)
, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this
,getString(R.string.dialog_btn_confirm_hint_text),Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
normalMoreButtonDialog.setNegativeButton(getString(R.string.dialog_btn_cancel_text)
, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this,
getString(R.string.dialog_btn_cancel_hint_text),Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
normalMoreButtonDialog.setNeutralButton(getString(R.string.dialog_btn_neutral_text)
, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this,
getString(R.string.dialog_btn_neutral_hint_text),Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
normalMoreButtonDialog.create().show();
}
也可以用下面的实现方式,和上面的代码效果是一样的。
private void showNormalMoreButtonDialog(){
DialogInterface.OnClickListener setListener = null;
AlertDialog.Builder normalMoreButtonDialog = new AlertDialog.Builder(this);
normalMoreButtonDialog.setTitle(getString(R.string.dialog_normal_more_button_text));
normalMoreButtonDialog.setIcon(R.mipmap.ic_launcher_round);
normalMoreButtonDialog.setMessage(getString(R.string.dialog_normal_more_button_content));
setListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
Toast.makeText(DialogActivity.this,
getString(R.string.dialog_btn_confirm_hint_text),Toast.LENGTH_SHORT).show();
dialog.dismiss();
break;
case DialogInterface.BUTTON_NEUTRAL:
Toast.makeText(DialogActivity.this
,getString(R.string.dialog_btn_neutral_hint_text),Toast.LENGTH_SHORT).show();
dialog.dismiss();
break;
case DialogInterface.BUTTON_NEGATIVE:
Toast.makeText(DialogActivity.this
,getString(R.string.dialog_btn_cancel_hint_text),Toast.LENGTH_SHORT).show();
dialog.dismiss();
break;
}
}
};
normalMoreButtonDialog.setPositiveButton(getString(R.string.dialog_btn_confirm_text),setListener);
normalMoreButtonDialog.setNegativeButton(getString(R.string.dialog_btn_cancel_text),setListener);
normalMoreButtonDialog.setNeutralButton(getString(R.string.dialog_btn_neutral_text),setListener);
normalMoreButtonDialog.create().show();
}
3、普通列表对话框
此种类型的对话框能实现简单的列表。
运行截图:

代码:
/**
* 普通列表dialog
*/
private void showListDialog(){
final String listItems[] = new String[]{"listItems1","listItems2","listItems3",
"listItems4","listItems5","listItems6"};
AlertDialog.Builder listDialog = new AlertDialog.Builder(this);
listDialog.setTitle(getString(R.string.dialog_list_text));
listDialog.setIcon(R.mipmap.ic_launcher_round);
/*
设置item 不能用setMessage()
用setItems
items : listItems[] -> 列表项数组
listener -> 回调接口
*/
listDialog.setItems(listItems,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this,listItems[which],Toast.LENGTH_SHORT).show();
}
});
//设置按钮
listDialog.setPositiveButton(getString(R.string.dialog_btn_confirm_text)
, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
listDialog.create().show();
}
4、单选对话框
运行截图:

代码:
private void showRadioDialog(){
final String radioItems[] = new String[]{"radioItem1","radioItem1","radioItem1","radioItem1"};
AlertDialog.Builder radioDialog = new AlertDialog.Builder(this);
radioDialog.setTitle(getString(R.string.dialog_radio_text));
radioDialog.setIcon(R.mipmap.ic_launcher_round);
/*
设置item 不能用setMessage()
用setSingleChoiceItems
items : radioItems[] -> 单选选项数组
checkItem : 0 -> 默认选中的item
listener -> 回调接口
*/
radioDialog.setSingleChoiceItems(radioItems, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this,radioItems[which],Toast.LENGTH_SHORT).show();
}
});
//设置按钮
radioDialog.setPositiveButton(getString(R.string.dialog_btn_confirm_text)
, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
radioDialog.create().show();
}
5、多选对话框
运行截图:

代码:
private void showCheckBoxDialog(){
final String checkBoxItems[] = new String[]{"checkBoxItems1","checkBoxItems2",
"checkBoxItems3","checkBoxItems4"};
final boolean isCheck[] = new boolean[]{false,true,true,false};
AlertDialog.Builder checkBoxDialog = new AlertDialog.Builder(this);
checkBoxDialog.setTitle(getString(R.string.dialog_check_box_text));
checkBoxDialog.setIcon(R.mipmap.ic_launcher_round);
/*
设置item 不能用setMessage()
用setMultiChoiceItems
items : radioItems[] -> 多选选项数组
checkItems : isCheck[] -> 是否选中数组
listener -> 回调接口
*/
checkBoxDialog.setMultiChoiceItems(checkBoxItems, isCheck
, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked){
Toast.makeText(DialogActivity.this,
checkBoxItems[which] + " 选中",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(DialogActivity.this,
checkBoxItems[which] + " 未选中",Toast.LENGTH_SHORT).show();
}
}
});
//设置按钮
checkBoxDialog.setPositiveButton(getString(R.string.dialog_btn_confirm_text)
, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
checkBoxDialog.create().show();
}
6、带输入框的弹窗
运行截图:

代码:
private void showEditDialog(){
final EditText edit = new EditText(this);
AlertDialog.Builder editDialog = new AlertDialog.Builder(this);
editDialog.setTitle(getString(R.string.dialog_edit_text));
editDialog.setIcon(R.mipmap.ic_launcher_round);
//设置dialog布局
editDialog.setView(edit);
//设置按钮
editDialog.setPositiveButton(getString(R.string.dialog_btn_confirm_text)
, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this,
edit.getText().toString().trim(),Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
editDialog.create().show();
}
7、自定义布局的对话框
此类型的对话框在实际项目开发中用到的地方比提示对话框用到的地方要多一些,不过在项目几乎上都是自定义的对话框...
运行截图:

布局文件:custom_dialog_layout.xml
<TextView
android:id="@+id/dialog_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="@color/colorPrimary"
android:gravity="center"
android:padding="12dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
<Button
android:id="@+id/dialog_btn_confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorAccent"
android:textSize="15sp"
android:text="@string/dialog_btn_confirm_text"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/dialog_btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorAccent"
android:textSize="15sp"
android:text="@string/dialog_btn_cancel_text"
android:layout_centerHorizontal="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</LinearLayout>
代码:
private void showLayoutDialog() {
//加载布局并初始化组件
View dialogView = LayoutInflater.from(this).inflate(R.layout.custom_dialog_layout,null);
TextView dialogText = (TextView) dialogView.findViewById(R.id.dialog_text);
Button dialogBtnConfirm = (Button) dialogView.findViewById(R.id.dialog_btn_confirm);
Button dialogBtnCancel = (Button) dialogView.findViewById(R.id.dialog_btn_cancel);
final AlertDialog.Builder layoutDialog = new AlertDialog.Builder(this);
layoutDialog.setTitle(getString(R.string.dialog_custom_layout_text));
layoutDialog.setIcon(R.mipmap.ic_launcher_round);
layoutDialog.setView(dialogView);
//设置组件
dialogText.setText("我是自定义layout的弹窗!!");
dialogBtnConfirm .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(DialogActivity.this,"我是自定义layout的弹窗!!",Toast.LENGTH_SHORT).show();
}
});
dialogBtnConfirm .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
layoutDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
dialog.dismiss();
}
});
}
});
layoutDialog.create().show();
}
以上就是Android系统弹窗的几种实现方式,几乎涵盖了能解决各种简单需求。其中自定义布局的方式奠定了自定义弹窗的基本实现。
尾语
可以看出上面的几种实现方式都是通过AlertDialog类实现的。有兴趣的可以看Android源码中的AlertDialog类实现。
我的个人博客:Jetictors
我的掘金:Jetictors
Github:Jetictors
欢迎各位大佬进群共同研究、探索
QQ群号:497071402
Android———最详细的系统对话框(AlertDialog)详解的更多相关文章
- Android———最详细的系统对话框使用
在实际应用开发中,用到系统对话框中的情况几乎是没有的.按开发流程来说,UI工程师都会给出每一个弹窗的样式,故而在实际开发中都是自定义弹窗的. 即使用到的地方不多,但是我们也是需要了解并且能熟练的运用它 ...
- 【转】【Android UI设计与开发】之详解ActionBar的使用,androidactionbar
原文网址:http://www.bkjia.com/Androidjc/895966.html [Android UI设计与开发]之详解ActionBar的使用,androidactionbar 详解 ...
- 分享我开发的网络电话Android手机APP正式版,图文详解及下载
分享我开发的网络电话Android手机APP正式版,图文详解及下载 分享我开发的网络电话Android手机APP正式版 实时语音通讯,可广域网实时通讯,音质清晰流畅! 安装之后的运行效果: 第一次安装 ...
- Android 颜色渲染(九) PorterDuff及Xfermode详解
版权声明:本文为博主原创文章,未经博主允许不得转载. Android 颜色渲染(九) PorterDuff及Xfermode详解 之前已经讲过了除ComposeShader之外Shader的全部子类 ...
- Android ADB命令教程二——ADB命令详解
Android ADB命令教程二——ADB命令详解 转载▼ 原文链接:http://www.tbk.ren/article/249.html 我们使用 adb -h 来看看,adb命令里面 ...
- Android 中各种权限深入体验及详解
Android 中各种权限深入体验及详解 分类: Android2012-07-15 19:27 2822人阅读 评论(0) 收藏 举报 androidpermissionsinstallersyst ...
- Android低功耗蓝牙(BLE)使用详解
代码地址如下:http://www.demodashi.com/demo/13390.html 与普通蓝牙相比,低功耗蓝牙显著降低了能量消耗,允许Android应用程序与具有更严格电源要求的BLE设备 ...
- Android逆向之旅---SO(ELF)文件格式详解(转)
第一.前言 从今天开始我们正式开始Android的逆向之旅,关于逆向的相关知识,想必大家都不陌生了,逆向领域是一个充满挑战和神秘的领域.作为一名Android开发者,每个人都想去探索这个领域,因为一旦 ...
- Android SDK中的Support兼容包详解
这篇文章主要介绍了Android SDK中的Support兼容包详解,本文详细区分了Support Library的版本区别.各种Theme的概念和使用注意事项等内容,需要的朋友可以参考下 背景 来自 ...
随机推荐
- docker实战——构建Jekyll
构建第一个应用 要构建的第一个应用是Jekyll框架的自定义网站.我们会构建一下两个镜像. 一个镜像安装Jekyll以及其他用于构建Jekyll网站的必要的软件包. 一个镜像通过Apache来让Jek ...
- 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-点动面板的每个按钮含义
参考下面的图示 更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i.youku.com/acetaohai123 我的在线论坛: http://csrobot.g ...
- 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-报错0X4655,18005错误怎么办
首先确认驱动器没有报错(如果驱动器报错,请先解决绝对值编码器的清除多圈数据问题) 报错一般上使能就会报错,没法测试运转,而且不管是用贝福自带的NC功能还是自己写的都会一样的效果 请删除在贝福的Et ...
- Django——如何处理请求(URL配置和视图)
URLconfig—— 为了绑定视图函数和URL,我们使用URLconf. URLconf 就像是 Django 所支撑网站的目录. 它的本质是 URL 模式以及要为该 URL 模式调用的视图函数之间 ...
- Property with 'retain (or strong)' attribute must be of object type
AFNetworking 2.0 当Deployment Target 低于6.0时,AFURLConnectionOperation.h,AFURLSessionManager.h @propert ...
- 【Python】help与dir的用法
当你给dir()提供一个模块名字时,它返回在那个模块中定义的名字的列表.当没有为其提供参数时, 它返回当前模块中定义的名字的列表. 如果您需要快速获取任何的Python函数或语句的信息,那么您可以使用 ...
- java的IO操作:System类对IO的支持。
目标: 1,掌握SYStem对IO的三种支持: system.out system.in system.err 2,掌握system.out及system.err的区别. 3,掌握输入,输出重定向. ...
- hibernate 继承映射关系( TABLE_PER_CLASS)
Person,Student,Teacher各创建一个表,主键用一个中间表生成. package com.bjsxt.hibernate; import javax.persistence.Ent ...
- rational rose 2007安装破解全过程
1:下载安装文件 下载地址: http://pan.baidu.com/s/1c0ldKEs 2:下载虚拟光驱 由于下载的文件须要光驱安装,所以须要下载一个虚拟光驱,虚拟光驱名称:daemon too ...
- angular4 开发记录
1,传值问题 page setValue: [routerLink]="['/product-details', product.id]"> ts seValue: ...