Android对话框总结(普通对话框,单选对话框,多选对话框,自定义对话框)
一:AlterDialog对话框
二:普通对话框
运行效果:
代码详情
备注:onBackPressed()事件是用户点击回退键退出软件时触发,事件由系统自己监听。
三:单选对话框
运行效果:
代码详情
四:多选对话框
运行效果:
代码详情
五:自定义对话框
运行效果:
1:自定义对话框dialog.xml布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#11ffffff"><LinearLayoutandroid:layout_width="260dp"android:layout_height="wrap_content"android:layout_centerInParent="true"android:background="@drawable/radius_bomb_box"android:orientation="vertical"><TextViewandroid:id="@+id/title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:text="警告!!!"android:textColor="#38ADFF"android:textSize="16sp"/><TextViewandroid:id="@+id/message"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:layout_marginRight="20dp"android:layout_gravity="center"android:text="这是自定义对话框"/><Viewandroid:layout_width="match_parent"android:layout_height="1px"android:layout_marginTop="15dp"android:background="#E4E4E4"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="40dp"android:orientation="horizontal"><Buttonandroid:id="@+id/no"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:layout_marginLeft="10dp"android:background="@null"android:gravity="center"android:lines="1"android:text="取消"android:textColor="#7D7D7D"android:textSize="16sp"/><Viewandroid:layout_width="1px"android:layout_height="match_parent"android:background="#E4E4E4"/><Buttonandroid:id="@+id/yes"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:layout_marginRight="10dp"android:background="@null"android:gravity="center"android:lines="1"android:text="确定"android:textColor="#38ADFF"android:textSize="16sp"/></LinearLayout></LinearLayout>
</RelativeLayout>
2:对话框圆角显示(在drawable下创建radius_bomb_box.xml)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<solid android:color="#ffffff" />
<strokeandroid:width="0.8dp"android:color="#ffffff" />
<!-- 圆角 -->
<corners android:radius="6dp" />
</shape>
3:Styles样式设置
<resources><!-- Base application theme. --><style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"><!-- Customize your theme here. --><item name="colorPrimary">@color/colorPrimary</item><item name="colorPrimaryDark">@color/colorPrimaryDark</item><item name="colorAccent">@color/colorAccent</item></style><!--自定义Dialog背景全透明无边框theme--><style name="MyDialog" parent="android:style/Theme.Dialog"><!--背景颜色和透明程度--><item name="android:windowBackground">@android:color/transparent</item><!--是否去除标题--><item name="android:windowNoTitle">true</item><!--是否去除边框--><item name="android:windowFrame">@null</item><!--是否浮现在activity之上--><item name="android:windowIsFloating">true</item><!--是否模糊--><item name="android:backgroundDimEnabled">false</item></style>
</resources>
4:创建MyDialog对话框类(继承Dialog类)
package cn.cg.promptbox;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;public class MyDialog extends Dialog {private Button yes;//确定按钮private Button no;//取消按钮private TextView titleTV;//消息标题文本private TextView message;//消息提示文本private String titleStr;//从外界设置的title文本private String messageStr;//从外界设置的消息文本//确定文本和取消文本的显示的内容private String yesStr, noStr;private onNoOnclickListener noOnclickListener;//取消按钮被点击了的监听器private onYesOnclickListener yesOnclickListener;//确定按钮被点击了的监听器public MyDialog(Context context, int themeResId) {super(context, themeResId);}/*** 设置取消按钮的显示内容和监听** @param str* @param onNoOnclickListener*/public void setNoOnclickListener(String str, onNoOnclickListener onNoOnclickListener) {if (str != null) {noStr = str;}this.noOnclickListener = onNoOnclickListener;}/*** 设置确定按钮的显示内容和监听** @param str* @param yesOnclickListener*/public void setYesOnclickListener(String str, onYesOnclickListener yesOnclickListener) {if (str != null) {yesStr = str;}this.yesOnclickListener = yesOnclickListener;}protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//布局视图setContentView(R.layout.dialog);//空白处不能取消动画setCanceledOnTouchOutside(false);//初始化界面控件initView();//初始化界面数据initData();//初始化界面控件的事件initEvent();}/*** 初始化界面控件*/private void initView() {yes = findViewById(R.id.yes);no = findViewById(R.id.no);titleTV = (TextView) findViewById(R.id.title);message = (TextView) findViewById(R.id.message);}/*** 初始化界面控件的显示数据*/private void initData() {//如果用户自定了title和messageif (titleStr != null) {titleTV.setText(titleStr);}if (messageStr != null) {message.setText(messageStr);}//如果设置按钮文字if (yesStr != null) {yes.setText(yesStr);}if (noStr != null) {no.setText(noStr);}}/*** 初始化界面的确定和取消监听*/private void initEvent() {//设置确定按钮被点击后,向外界提供监听yes.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if (yesOnclickListener != null) {yesOnclickListener.onYesOnclick();}}});//设置取消按钮被点击后,向外界提供监听no.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if (noOnclickListener != null) {noOnclickListener.onNoClick();}}});}/*** 从外界Activity为Dialog设置标题** @param title*/public void setTitle(String title) {titleStr = title;}/*** 从外界Activity为Dialog设置message** @param message*/public void setMessage(String message) {messageStr = message;}//取消按钮监听事件public interface onNoOnclickListener {public void onNoClick();}//确定按钮监听事件public interface onYesOnclickListener {public void onYesOnclick();}
}
5:通过自己创建的Dialog 类(MyDialog)进行显示
/*自定义对话框*///自定义对话框private MyDialog myDialog;private void DisplayCustomDialog(View view) {myDialog=new MyDialog(MainActivity.this,R.style.MyDialog);myDialog.setTitle("警告");myDialog.setMessage("这是自定义对话框!");myDialog.setYesOnclickListener("确定", new MyDialog.onYesOnclickListener() {@Overridepublic void onYesOnclick() {//确定点击事件Toast.makeText(MainActivity.this,"你点击了自定义提示框确定按钮",Toast.LENGTH_SHORT).show();myDialog.dismiss();}});myDialog.setNoOnclickListener("取消", new MyDialog.onNoOnclickListener() {@Overridepublic void onNoClick() {//取消点击事件Toast.makeText(MainActivity.this,"你点击了自定义提示框取消按钮",Toast.LENGTH_SHORT).show();myDialog.dismiss();}});myDialog.show();}
备注:自定义对话框来源于来自这里
关于@null的解释
最后整个项目下载地址点击下载吧!
Android对话框总结(普通对话框,单选对话框,多选对话框,自定义对话框)相关推荐
- android alertdialog view,Android AlertDialog 方法setView(view,0,0,0,0)开发自定义对话框
如果使用 m_currDlg = new AlertDialog.Builder(m_activity).create(); m_currDlg.setView(textEntryView,0,0,0 ...
- Android ListView 使用checkbox 实现 单选、多选操作
项目开发中经常会遇到 ListView +CheckBox 结合使用的场景,比如 手机应用程序管理程序 中的卸载功能,需要让用户去 勾选 要卸载的 应用程序,点击 即可进行批量卸载.等等这样的例子很多 ...
- Android开发笔记(六十六)自定义对话框
AlertDialog Android中最常用的对话框是AlertDialog,它可以完成常见的交互操作,如提示.确认.选择等等,然后就是进度对话框ProgressDialog(参见< Andr ...
- InstallShield自定义对话框浅谈(转)
说明:本文档的InstallShield为6.22版本,语言:中文.操作系统为Windows2000.资源编辑工具:Microsoft Visual C ++ 6.0.修改的DLL:_isuer.dl ...
- android单选对话框代码,Android实现单选与多选对话框的代码
android开发中实现单选与多选对话框的代码非常简单,具体代码如下所示: public void myClick(View view) { // 单选对话框 //singleCheckDialog( ...
- Android 图片选择对话框,通过本地相册或照相机获得图片,可单选或多选,单选可设置是否裁剪
AndroidPickPhotoDialog 项目地址: wanliyang1990/AndroidPickPhotoDialog 简介:Android 图片选择对话框,通过本地相册或照相机获得图片, ...
- android 之Dialog的多种Dialog对话框(以及反射在自定义对话框的运用)
实际效果: 实例代码: 一个"启动初始Activity.xml" 一个弹出的Activity 业务逻辑代码: package com.hsj.example.alertdialog ...
- 使用Android studio完成”仿QQ的头像选择弹出的对话框“步骤及知识梳理
闲着无聊,提前请假回家闭关学习... 先看效果图: 该DEMO 是我网上找的,比较喜欢这个动画效果,就自己动手实现了一遍,原理不难,但是真正理解使用起来也不简单. 一:自定义Dialog 当你点击按钮 ...
- 如何在android中创建自定义对话框?
本文翻译自:How to create a Custom Dialog box in android? I want to create a custom dialog box like below ...
- android 继承dialog自定义对话框
android 继承dialog自定义对话框 样式如下: 代码实现: 主要Activity: public class MainActivity extends Activity { Button ...
最新文章
- 激活函数之softmax介绍及C++实现
- 基于图像的三维物体重建:在深度学习时代的最新技术和趋势综述之训练
- C#复制图片_并重命名
- iOS7与iOS8的比較
- php新浪api,php调用新浪短链接API的方法,php调用新浪api_PHP教程
- python之print实践
- centos7安装es mysql_Centos7 安装MySQL详细步骤
- php后台无法登入,typecho 后台无法登陆
- B 站疯传!P8技术大佬分享了20个免费Java课程,白拿不谢!!
- 从jvm层面来看多线程问题
- 科大讯飞语音合成python版
- QUIC协议是如何做到0RTT加密传输的(addons)
- DPDK-l3fwd示例IPv6测试
- 记录一次服务器被植入挖矿脚本事件
- 袖珍计算器c语言设计源码,Calculator 示例:Windows 窗体袖珍计算器
- 【MFC】学习笔记:文件操作类——CFile
- int函数python_int()函数
- MySQL GIS功能介绍
- 三台机器互相免密登录
- 家庭新成员:德牧听泉
热门文章
- 云计算摆摊的可行性分析 | 凌云时刻
- 一文读懂数据湖 | 凌云时刻
- 文荣:7月24日阿里云上海峰会网络大神
- 开源中国开源世界高峰论坛如期将至
- 【优化算法】多目标萤火虫算法(MOFA)【含Matlab源码 1595期】
- 【路径规划】基于matlab A_star算法多机器人路径规划【含Matlab源码 1251期】
- 【雷达通信】基于matlab CDIF算法雷达信号分选【含Matlab源码 1186期】
- 【图像分割】基于matlab超像素图像分割【含Matlab源码 720期】
- 【背包问题】基于matlab遗传算法求解背包问题【含Matlab源码 578期】
- php 数据结构set,ES6的Set数据结构是什么