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页面布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6.  
  7. <!--android:background="#fcc" 会覆盖掉style中原本设置的background属性值-->
  8. <TextView
  9. style="@style/TitleStyle"
  10. android:background="#fcc"
  11. android:text="添加黑名单号码" />
  12.  
  13. <EditText
  14. android:id="@+id/et_black_phone_call"
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. android:textColor="#000"
  18. android:hint="请输入拦截号码" />
  19.  
  20. <RadioGroup
  21. android:id="@+id/rg_black_call"
  22. android:layout_width="match_parent"
  23. android:layout_height="wrap_content"
  24. android:orientation="horizontal"
  25. android:gravity="center">
  26. <!--android:checked="true" 默认选中条目-->
  27. <RadioButton
  28. android:id="@+id/rb_sms"
  29. android:text="短信"
  30. android:textColor="#000"
  31. android:checked="true"
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content" />
  34.  
  35. <RadioButton
  36. android:id="@+id/rb_phone"
  37. android:text="电话"
  38. android:textColor="#000"
  39. android:layout_marginLeft="15dp"
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content" />
  42.  
  43. <RadioButton
  44. android:id="@+id/rb_all"
  45. android:text="所有"
  46. android:textColor="#000"
  47. android:layout_marginLeft="15dp"
  48. android:layout_width="wrap_content"
  49. android:layout_height="wrap_content" />
  50. </RadioGroup>
  51. <LinearLayout
  52. android:layout_width="match_parent"
  53. android:layout_height="wrap_content">
  54. <Button
  55. android:id="@+id/bt_black_confirm"
  56. android:layout_width="0dp"
  57. android:text="确认"
  58. android:textColor="#000"
  59. android:background="@drawable/selector_black_call_btn_bg"
  60. android:layout_height="wrap_content"
  61. android:layout_weight="1"/>
  62.  
  63. <Button
  64. android:id="@+id/bt_black_cancel"
  65. android:layout_width="0dp"
  66. android:text="取消"
  67. android:textColor="#000"
  68. android:background="@drawable/selector_black_call_btn_bg"
  69. android:layout_height="wrap_content"
  70. android:layout_weight="1"/>
  71. </LinearLayout>
  72. </LinearLayout>

(2)java后台代码

  1. private void showDialog() {
  2. //采用自定义的对话框样式,利用dialog.setView(view);
  3. AlertDialog.Builder builder=new AlertDialog.Builder(this);
  4. final AlertDialog dialog=builder.create();
  5. final View view=View.inflate(this,R.layout.dialog_set_black_call_list,null);
  6. dialog.setView(view);
  7. dialog.show();//显示对话框
  8.  
  9. //找到自定义对话框布局文件中的控件
  10. final EditText et_black_phone_call=view.findViewById(R.id.et_black_phone_call);
  11. RadioGroup radioGroup=view.findViewById(R.id.rg_black_call);
  12. Button bt_black_confirm=view.findViewById(R.id.bt_black_confirm);
  13. Button bt_black_cancel=view.findViewById(R.id.bt_black_cancel);
  14.  
  15. //监听radioGroup选中条目的切换过程
  16. radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  17. @Override
  18. public void onCheckedChanged(RadioGroup group, int checkedId) {
  19. switch (checkedId){
  20. case R.id.rb_sms:
  21. function_type="拦截短信";
  22. break;
  23. case R.id.rb_phone:
  24. function_type="拦截电话";
  25. break;
  26. case R.id.rb_all:
  27. function_type="拦截所有";
  28. break;
  29. }
  30. }
  31. });
  32.  
  33. bt_black_confirm.setOnClickListener(new View.OnClickListener() {
  34. @Override
  35. public void onClick(View v) {
  36. //1.获取输入框的电话号码
  37. String phoneNumber=et_black_phone_call.getText().toString();
  38. if(!TextUtils.isEmpty(phoneNumber)){
  39. //2.向数据库中插入当前用户输入的拦截号码
  40. BlackListCallDBUtil.insertOneRecord(phoneNumber,function_type);
  41. initData();
  42. }else {
  43. Toast.makeText(getApplicationContext(),"请输入电话号码",Toast.LENGTH_SHORT).show();
  44. }
  45. }
  46. });
  47.  
  48. bt_black_cancel.setOnClickListener(new View.OnClickListener() {
  49. @Override
  50. public void onClick(View v) {
  51. dialog.dismiss(); //关闭对话框
  52. }
  53. });
  54. }

3.效果图

024 Android 自定义样式对话框(AlertDialog)的更多相关文章

  1. Android中的对话框AlertDialog使用技巧合集-转载

    Android中的对话框AlertDialog使用技巧合集     文章来自:http://blog.csdn.net/blue6626/article/details/6641105   今天我用自 ...

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

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

  3. Android自定义 Dialog 对话框

    Android自定义Dialoghttp://www.cnblogs.com/and_he/archive/2011/09/16/2178716.html Android使用自定义AlertDialo ...

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

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

  5. android中提示&对话框----AlertDialog

    AlertDialog(对话框) 一.对话框的基本使用流程 step1:创建AlertDialog.Buider; step2:调用setIcon()设置图标,setTitle()或者setCusto ...

  6. 11.Android之常用对话框AlertDialog学习

    (1)首先我们写个简单的AlertDialog对话框,要创建一个AlertDialog,就要用到AlertDialog.Builder中的create()方法,然后创建对话框可以设置对话框的属性,比如 ...

  7. android 自定义View 对话框

    package com.example.dialog5; import android.os.Bundle;import android.app.Activity;import android.app ...

  8. Android自定义样式

    1.AndroidManifest.xml android:theme="@style/Theme.CustomDialog 样式要用:@style <?xml version=&qu ...

  9. [android] 安卓自定义样式和主题

    简单练习自定义样式和主题,样式是加在View上,主题是加在Application或者Activity上 styles.xml <?xml version="1.0" enco ...

随机推荐

  1. 模板 - 数据结构 - 链表/LinkedList

    一个只供删除的双向链表,为了简单不再引入head节点,而且也不进行next的套娃操作.空间使用略微多了一些,但是无伤大雅. struct LinkedList { static const int M ...

  2. 在Ubuntu 18.04 下安装mysql,没有初始密码,重设root密码

    在Ubuntu 18.04 下安装mysql 不知道是由于mysql更新为新版还是.Ubuntu18.04中的特性,安装过程中没有设置密码的环节,在网络上找了半天,总算解决了!特此记录下来,以便以后查 ...

  3. Open vSwitch系列实验(一):Open vSwitch使用案例扩展实验

    一.实验目的 通过python脚本调用OpenvSwitch命令: 学习Mininet基于python脚本创建拓扑的实现: 进一步深度使用“ovs-vsctl”命令直接控制Open vSwitch. ...

  4. [spring-boot] 多环境配置

    application-{profile}.properties 按照格式创建两个配置文件,一个DEV环境,一个测试环境 修改其端口: server.port=8888 DEV server.port ...

  5. Linux系列 | Ubuntu 各版本号和名称对照【转】

    转载处:https://blog.csdn.net/songfulu/article/details/85310273   版本 开发代号 中译 发布日期 支持结束时间 内核版本 桌面版 服务器版 4 ...

  6. 产品经理 写SQL

    产品经理必备技能:写SQL - 云+社区 - 腾讯云https://cloud.tencent.com/developer/news/3177 产品经理学SQL(一)一个小时上手SQL | 人人都是产 ...

  7. win10下caffe+anaconda+python+Jupyter Notebooks安装流程

    python3.5(推荐)或者python2.7 CUDA 8+ cuDNN5.1 python环境不能单独配置,必须先编译caffe,才能编译python环境. 下载caffe prebuild版本 ...

  8. ffmpeg-php扩展

    php视频缩略图,较常用的是ffmpeg-php 1: 安装 ffmpeg ffmpeg的下载链接  http://ffmpeg.org/download.html 解压安装包 tar -jxvf f ...

  9. 【转载】 漫谈Code Review的错误实践

    原文地址: https://www.cnblogs.com/chaosyang/p/code-review-wrong-practices.html ------------------------- ...

  10. Django架站的16堂課

    Django架站的16堂課-活用Django+Web+Framework快速构建移动网站 目录 第1堂 网站开发环境的建立 1 1.1 网站的基础知识 1 1.1.1 网站的运行流程 1 1.1.2 ...