1.AlertDialog介绍

AlertDialog并不需要到布局文件中创建,而是在代码中通过构造器(AlertDialog.Builder)来构造标题、图标和按钮等内容的。

常规使用步骤(具体参见Android 开发博客中的024篇):

(1)创建构造器AlertDialog.Builder的对象;
(2)通过构造器的对象调用setTitle、setMessage等方法构造对话框的标题、信息和图标等内容;
(3)根据需要,设置正面按钮、负面按钮和中立按钮;
(4)调用create方法创建AlertDialog的对象;
(5)AlertDialog的对象调用show方法,让对话框在界面上显示。

只显示简单的标题和信息是满足不了我们的要求,比如我们要实现一个登录对话框的话,那就需要在对话框上放置EditText输入框了。AlertDialog早就为我们准备好了setView方法,只要往里面放进我们需要显示的View对象就可以了。

2.自定义对话框

(1)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:background="#fcc" 会覆盖掉style中原本设置的background属性值--><TextViewstyle="@style/TitleStyle"android:background="#fcc"android:text="添加黑名单号码" /><EditTextandroid:id="@+id/et_black_phone_call"android:layout_width="match_parent"android:layout_height="wrap_content"android:textColor="#000"android:hint="请输入拦截号码" /><RadioGroupandroid:id="@+id/rg_black_call"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:gravity="center"><!--android:checked="true" 默认选中条目--><RadioButtonandroid:id="@+id/rb_sms"android:text="短信"android:textColor="#000"android:checked="true"android:layout_width="wrap_content"android:layout_height="wrap_content" /><RadioButtonandroid:id="@+id/rb_phone"android:text="电话"android:textColor="#000"android:layout_marginLeft="15dp"android:layout_width="wrap_content"android:layout_height="wrap_content" /><RadioButtonandroid:id="@+id/rb_all"android:text="所有"android:textColor="#000"android:layout_marginLeft="15dp"android:layout_width="wrap_content"android:layout_height="wrap_content" /></RadioGroup><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><Buttonandroid:id="@+id/bt_black_confirm"android:layout_width="0dp"android:text="确认"android:textColor="#000"android:background="@drawable/selector_black_call_btn_bg"android:layout_height="wrap_content"android:layout_weight="1"/><Buttonandroid:id="@+id/bt_black_cancel"android:layout_width="0dp"android:text="取消"android:textColor="#000"android:background="@drawable/selector_black_call_btn_bg"android:layout_height="wrap_content"android:layout_weight="1"/></LinearLayout>
</LinearLayout>

(2)java后台代码

private void showDialog() {//采用自定义的对话框样式,利用dialog.setView(view);AlertDialog.Builder builder=new AlertDialog.Builder(this);final AlertDialog dialog=builder.create();final View view=View.inflate(this,R.layout.dialog_set_black_call_list,null);dialog.setView(view);dialog.show();//显示对话框//找到自定义对话框布局文件中的控件final EditText et_black_phone_call=view.findViewById(R.id.et_black_phone_call);RadioGroup radioGroup=view.findViewById(R.id.rg_black_call);Button bt_black_confirm=view.findViewById(R.id.bt_black_confirm);Button bt_black_cancel=view.findViewById(R.id.bt_black_cancel);//监听radioGroup选中条目的切换过程radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {switch (checkedId){case R.id.rb_sms:function_type="拦截短信";break;case R.id.rb_phone:function_type="拦截电话";break;case R.id.rb_all:function_type="拦截所有";break;}}});bt_black_confirm.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//1.获取输入框的电话号码String phoneNumber=et_black_phone_call.getText().toString();if(!TextUtils.isEmpty(phoneNumber)){//2.向数据库中插入当前用户输入的拦截号码
                    BlackListCallDBUtil.insertOneRecord(phoneNumber,function_type);initData();}else {Toast.makeText(getApplicationContext(),"请输入电话号码",Toast.LENGTH_SHORT).show();}}});bt_black_cancel.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {dialog.dismiss(); //关闭对话框
            }});}

3.效果图

转载于:https://www.cnblogs.com/luckyplj/p/10847912.html

024 Android 自定义样式对话框(AlertDialog)相关推荐

  1. android自定义dialog对话框,android的自定义dialog对话框实现

    Android自定义dialog对话框 一:自定义dialog 如何填充布局 ①自定MyDialog类,在类里关联布局 public class MyDialog extends AlertDialo ...

  2. Android自定义样式

    Android自定义样式 1.AndroidManifest.xml android:theme="@style/Theme.CustomDialog 样式要用:@style <?xm ...

  3. Android自定义一个对话框,完全自定义Android对话框AlertDialog的实现

    Android本身封装的AlertDialog.Builder很方便易用,但如果想要自定义弹出对话框的风格,如标题字体背景元素间隔之类的,那就比较困难了. 最近我就遇到了这个问题,一个工程的界面风格全 ...

  4. Android的警示对话框AlertDialog简单使用实例(附Demo)

    目录 AlertDialog默认样式 单选样式的AlertDialog: AlertDialog多选样式: AlertDialog自定义样式: 自定义Dialog: Demo地址:https://gi ...

  5. android自定义样式大全:shape,selector,layer-list,style,动画全部内容

    原文:http://keeganlee.me/post/android/20150830 以下摘取了部分内容: shape 一般用shape定义的xml文件存放在drawable目录下,若项目没有该目 ...

  6. Android 中文 API ——对话框 AlertDialog.Builder

    2019独角兽企业重金招聘Python工程师标准>>> 刚开始接触android的时候,我在做一个自定义对话框的时候,也是通过继承的方式来实现,后来随着对文档了解的深入,发现了and ...

  7. Android详细的对话框AlertDialog.Builder使用方法

    我们在平时做开发的时候,免不了会用到各种各样的对话框,相信有过其他平台开发经验的朋友都会知道,大部分的平台都只提供了几个最简单的实现,如果我们想实现自己特定需求的对话框,大家可能首先会想到,通过继承等 ...

  8. android 自定义时间对话框,android自定义日期和时间选择对话框得实现

    1,先写布局文件,把时间选择器和日期选择器都放到一起去 android:layout_width="match_parent" android:layout_height=&quo ...

  9. android自定义退出对话框,Android自定义Dialog(仿QQ同步助手退出对话框)

    继承Dialog类就可以了,写写布局文件,写写style,就OK了.下面开始. 先上布局文件: xmlns:android="http://schemas.android.com/apk/r ...

最新文章

  1. MATLAB中的vpa函数简单实用记录——精度控制
  2. Track与nqa联动 VS 静态路由优先级相同
  3. 《Android群英传》读书笔记 (5) 第十一章 搭建云端服务器 + 第十二章 Android 5.X新特性详解 + 第十三章 Android实例提高...
  4. python 需求分析
  5. input点击事件不能用_用js简单写一个计算器
  6. 关键词分词工具_快图制作工具 | 如何制作词云图?
  7. 网络通信协议-TCP/IP模型实战
  8. 怎样在半年内把一个团队带垮?
  9. c++ builder firemonkey 实现填充椭圆
  10. 余承东吐槽苹果续航;微软 IE 浏览器被曝漏洞;React Native 0.61.0 发布 | 极客头条...
  11. R语言中的机器学习包
  12. html 表单 元素 美化,分享10款jQuery的表单元素样式美化插件
  13. TOGAF 企业连续系列
  14. 详解反向传播神经网络 (Back Propagation Neural Network, BPNN)
  15. Infor SunSystems咨询服务市场报告-市场规模、市场份额、市场定位、产品类型以及发展规划
  16. oliver什么意思java_英语名字“oliver”是什么意思?
  17. IOS 上传IPA到AppStore
  18. 通信之道-傅立叶分析
  19. 盘点机器视觉三大落地成熟应用
  20. 基于vue+springboot的校园疫情健康打卡和离校审批系统的设计 (百度地图API对接)

热门文章

  1. 高斯投影正反算C语言程序代码,高斯投影正反算-对网络上面流行的C代码的修改(已正确运行)...
  2. sql服务找不到服务器,我找不到SQL服务管理器
  3. linux简单使用命令发送邮件
  4. mysql 实现nextval_mysql实现nextVal功能
  5. powerbuilder中实现多线程同步查询_Power Query中的“追加查询”可以实现合并多个工作表和工作簿...
  6. 4个不同小球放入4个不同盒子_初婚老公和二婚老公有什么不同?听听这4个再婚女人的真实想法...
  7. 抽屉开关_技术天地|380伏抽屉开关指示灯更换流程
  8. 【若依(ruoyi)】自定义layer
  9. nginx 限制上传文件大小
  10. vue 修改favicon