024 Android 自定义样式对话框(AlertDialog)

时间:2023-01-23 09:04:32

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属性值-->
<TextView
style="@style/TitleStyle"
android:background="#fcc"
android:text="添加黑名单号码" /> <EditText
android:id="@+id/et_black_phone_call"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:hint="请输入拦截号码" /> <RadioGroup
android:id="@+id/rg_black_call"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<!--android:checked="true" 默认选中条目-->
<RadioButton
android:id="@+id/rb_sms"
android:text="短信"
android:textColor="#000"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <RadioButton
android:id="@+id/rb_phone"
android:text="电话"
android:textColor="#000"
android:layout_marginLeft="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <RadioButton
android:id="@+id/rb_all"
android:text="所有"
android:textColor="#000"
android:layout_marginLeft="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android: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"/> <Button
android: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() {
@Override
public 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() {
@Override
public 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() {
@Override
public void onClick(View v) {
dialog.dismiss(); //关闭对话框
}
});
}

3.效果图

024 Android 自定义样式对话框(AlertDialog)

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

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

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

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

    原文: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&period;Builder使用方法

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

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

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

  6. 11&period;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. &lbrack;android&rsqb; 安卓自定义样式和主题

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

随机推荐

  1. &lbrack;USACO&rsqb; 铺放矩形块 题解

    题目大意: 给定4个矩形块,找出一个最小的封闭矩形将这4个矩形块放入,但不得相互重叠.所谓最小矩形指该矩形面积最小. 思路: 枚举矩形的安放顺序,再按照题目所给的图判断即可,主要要想到枚举. 代码: ...

  2. 【Javascript】—— 1 方法function的高级特性

    本篇仅仅对于function作简单的讲解,在javascript中function不仅仅是方法,它其实是一个变量,因此拥有自己的属性,并且可以当做参数传递给其他的方法. 那么传统的方法,按照java的 ...

  3. HDU 2108 Shape of HDU

    题解:按照输入顺序依次将点连接起来,对于连续的三个点p0,p1,p2,令向量a=p1-p0,b=p2-p1 若是凸多边形,那么b相对于a一定是向逆时针方向旋转的 判断两向量的旋转方向,可以使用向量的叉 ...

  4. 5分钟解决google play上架App设置隐私政策声明问题

    本文同步自javaexception 问题: 在我们的app上架到google play后,为了赚点小钱,就集成google ads,然而这会引发一个新的问题,那就是设置隐私政策声明的问题,通常我们会 ...

  5. iOS XML解析使用-韩国庆

    欢迎-------(北京-iOS移动开发金牌教师QQ:2592675215)韩老师给你带来XML解析课程 今天给大家讲解下xml解析的第三方简单用法:首先我解释下,json和xml解析格式. JSON ...

  6. python 将一个列表去重,并且不打乱它原有的排列顺序

    old_lst = [2, 2, 1, 1, 3, 4] new_lst = list(set(old_lst)) new_lst.sort(key=old_lst.index) print(new_ ...

  7. POJ-3107 Godfather 求每个节点连接的联通块数量

    dp[n][2],维护儿子的联通块数量和父亲的联通块数量. 第一遍dfs求儿子,第二遍dfs求爸爸. #include<iostream> #include<cstring> ...

  8. python3使用requests模块完成get&sol;post&sol;代理&sol;自定义header&sol;自定义Cookie

    一.背景说明 http请求的难易对一门语言来说是很重要的而且是越来越重要,但对于python一是urllib一些写法不太符合人的思维习惯文档也相当难看,二是在python2.x和python3.x中写 ...

  9. 对象序列化:pickle和shelve

    import pickle class DVD: def __init__(self,tilte,year=None,duration=None,director_id=None): self.tit ...

  10. css style 标签可编辑

    一次偶然在鑫大技术博客上发现  style  标签配合contenteditable 可编辑属性 实现动态编辑css 这里我就回顾了下  contenteditable  可编辑属性 (这个属性并无浏 ...