介绍

The AlertDialog class allows you to build a variety of dialog designs and is often the only dialog class you’ll need. there are three regions of an alert dialog:



1.Title

This is optional and should be used only when the content area is occupied by a detailed message, a list, or custom layout. If you need to state a simple message or question (such as the dialog in figure 1), you don’t need a title.

2.Content area

This can display a message, a list, or other custom layout.

3.Action buttons

There should be no more than three action buttons in a dialog.

一般的AlertDialog分为2个部分,头部,内容和Action Button,头部包含图标和文字,内容部分可以自定义,也可以使用一些常见的列表,单选项,多选项等内容,Action Button就是常见的OK啊, Cancel啊等等。


类结构

AlertDialog

重点关注一下Builder类的结构

关于这些方法的使用说明,这里不做一一介绍,后面通过实例可以看出端倪,不过可以通过查阅API文档熟悉一下,AlertDialog.Builder


实际使用

Simple AlertDialog

代码使用

    private void showSimpleAlertDialog() {
//创建alertdialog builder
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
//设置图标,标题,消息内容
builder.setIcon(getResources().getDrawable(R.drawable.ic_adb_black_24dp))
.setTitle(R.string.title)
.setMessage(R.string.content);
//设置Acton Buttons
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(mContext, "Cancal is clicked", Toast.LENGTH_SHORT).show();
}
}); builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(mContext, "OK is clicked", Toast.LENGTH_SHORT).show();
}
}); builder.setNeutralButton(R.string.reminder, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(mContext, "Remind me later is clicked", Toast.LENGTH_SHORT).show();
}
});
//显示alert dialog 这个是AlertDialog的show()方法是等价的
builder.show();
}

效果

备注

Action Button 意义
setNegativeButton 做取消、返回等操作,如Cancel
setPositiveButton 做确认、提交等操作,如OK
setNeutralButton 做中性操作,如Update Later

AlertDialog.Builder中的show()方法和AlertDialog的show()方法是等价的


AlertDialog With Array

代码使用

    public void showAlertDialogWithArray() {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setIcon(getResources().getDrawable(R.drawable.ic_assessment_black_24dp))
.setTitle(R.string.title)
.setItems(R.array.dialoglist, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
switch (i) {
case 0:
showTwo.setBackgroundResource(R.color.pink);
break;
case 1:
showTwo.setBackgroundResource(R.color.stone);
break;
case 2:
showTwo.setBackgroundResource(R.color.yellow);
break;
}
}
});
builder.create().show();
}

效果

备注

方法
setItems 通过array来设置dialog的内容
setAdapter 通过ListAdapter来设置dialog的内容

ListAdapter可以显示的内容应该比Array要丰富,但是现在开源库很多,针对Dialog的美观的开源库也不少,一般开发过程中会使用现成的。


AlertDialog With MultiChoice

代码使用

    public void showAlertDialogWithMutiChoice() {
final ArrayList<String> selecteditems = new ArrayList<>();
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setIcon(R.drawable.ic_event_seat_black_24dp)
.setTitle(R.string.title)
.setMultiChoiceItems(R.array.fruits, null,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which,
boolean isChecked) {
if (isChecked) {
selecteditems.add(getResources()
.getStringArray(R.array.fruits)[which]);
Toast.makeText(mContext, selecteditems.toString(), Toast.LENGTH_SHORT).show();
} else {
selecteditems.remove(getResources()
.getStringArray(R.array.fruits)[which]);
Toast.makeText(mContext, selecteditems.toString(), Toast.LENGTH_SHORT).show();
}
}
});
builder.create().show();
}

效果


AlertDialog With Single Choice

代码使用

    public void showAlertDialogWithSingleChoice() {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setIcon(R.drawable.ic_build_black_24dp)
.setTitle(R.string.title);
builder.setSingleChoiceItems(R.array.places, -1,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(mContext,
getResources().getStringArray(R.array.places)[i] + " is selected",
Toast.LENGTH_SHORT).show();
}
});
builder.create().show();
}

效果


Custom Alert Dialog

代码使用

    public void showCustomAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setIcon(R.drawable.ic_assessment_black_24dp).setTitle(R.string.login);
builder.setView(R.layout.custom_alert);
builder.setCustomTitle(LayoutInflater.from(mContext).inflate(R.layout.custom_title , null ));
builder.create().show();
}

自定义布局文件

Title自定义

<?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="60dp"
android:background="@drawable/shape_title"
android:orientation="horizontal"> <ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:paddingLeft="20dp"
android:src="@drawable/ic_adb_black_24dp" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="40dp"
android:text="Custom Title"
android:textSize="15pt"
android:textStyle="bold" /> </LinearLayout>

Content Area自定义

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/activity_horizontal_margin"
android:orientation="vertical"> <android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"> <EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="UserName" />
</android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:counterEnabled="true"
app:counterMaxLength="40"> <EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:hint="Password"
android:inputType="textPassword" />
</android.support.design.widget.TextInputLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/activity_vertical_margin"
android:orientation="horizontal"> <Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/shaperect"
android:text="Submit" /> <Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/shape_two"
android:text="Reset" />
</LinearLayout> </LinearLayout>

效果

备注

自定义标题栏和显示的内容,Action Button可以通过在Content中添加Button的方式来实现,丰富了Alert Dialog的显示内容,Alert Dialog中应该还有很多东西待挖掘,锄头需要挥起来~

<Android 基础(十五)> Alert Dialog的更多相关文章

  1. Bootstrap<基础十五> 输入框组

    Bootstrap 支持的另一个特性,输入框组.输入框组扩展自 表单控件.使用输入框组,可以很容易地向基于文本的输入框添加作为前缀和后缀的文本或按钮. 通过向输入域添加前缀和后缀的内容,您可以向用户输 ...

  2. android基础(五)网络数据解析方法

    在网络上传输数据时最常用的方法有两种:XML和JSON,下面就对这两种类型的数据解析进行讲解. 一.XML数据解析 在Android中,常见的XML解析器分别为SAX解析器.DOM解析器和PULL解析 ...

  3. android基础(五)网络编程

    android 的网络编程一般可以分为两种:基于Socket的,基于Http的. 一.socket与Http socket封装了TCP/IP协议,TPC/IP协议是传输层协议,主要解决数据如何在网络中 ...

  4. Android入门(十五)通知

    原文链接:http://www.orlion.ga/663/ 1.通知的基本用法 创建通知的步骤,首先需要一个NotificationManager来对通知进行管理,可以调用Context的getSy ...

  5. Android进阶(十五)socket通信——聊天室

    想做一个聊天室,花费了将近一天的时间,各种错误.讲解知识点之前,絮叨几句:动手能力还是很重要的,有时看似简单的一个问题,当你真正着手去解决的时候就有可能会遇到各种各样的问题,原因之一就是因为你的知识储 ...

  6. java基础(十五)----- Java 最全异常详解 ——Java高级开发必须懂的

    本文将详解java中的异常和异常处理机制 异常简介 什么是异常? 程序运行时,发生的不被期望的事件,它阻止了程序按照程序员的预期正常执行,这就是异常. Java异常的分类和类结构图 1.Java中的所 ...

  7. <Android 基础(五)> MVVM

    介绍 MVVM,Model-View-ViewModel,与上次讲的MVP模式比较的类似,MVP中需要大量的接口文件,而MVVM模式下,View和ViewModel直接关联,使用上比较方便,简化了代码 ...

  8. python 基础(十五) socket编程

    SOCKET TCP协议: 有请求 有响应 称之为 tcp协议 是面向连接的协议 就是在收发数据之前 必须先要建立一个可靠的链接 三次握手 如:网站 UDP协议: 是一个非链接的协议 传输之前不需要键 ...

  9. WDA基础十五:POPUP WINDOW

    1.组件控制器定义属性: 2.实现popup方法: METHOD stock_popup . DATA: l_cmp_api TYPE REF TO if_wd_component, l_window ...

  10. 安卓Android基础第五天

    使用HttpUrlConnection方式提交到服务器2 Get方式:组拼url地址把数据组拼到url上,有大小限制1kb(浏览器)或4kb(http协议) Post方式:post方式提交安全,没有大 ...

随机推荐

  1. 树链剖分【洛谷P4114】 Qtree1

    P4114 Qtree1 题目描述 给定一棵n个节点的树,有两个操作: CHANGE i ti 把第i条边的边权变成ti QUERY a b 输出从a到b的路径中最大的边权,当a=b的时候,输出0 码 ...

  2. 高仿JDK动态代理 底层源码实现

    动态代理实现思路 实现功能:通过Proxy.newProxyInstance返回代理对象 1.创建一个处理业务逻辑的接口,我们也和JDK一样,都使用InvocationHandler作为接口名,然后接 ...

  3. linux线程切换问题

    处理器总处于以下状态中的一种: 1.内核态,运行于进程上下文,内核代表进程运行于内核空间: 2.内核态,运行于中断上下文,内核代表硬件运行于内核空间: 3.用户态,运行于用户空间:   一个进程的上下 ...

  4. react常用开发工具

    https://www.cnblogs.com/vipstone/p/7125338.html

  5. POJ 2299 Ultra-QuickSort (树状数组 && 离散化&&逆序)

    题意 : 给出一个数n(n<500,000), 再给出n个数的序列 a1.a2.....an每一个ai的范围是 0~999,999,999  要求出当通过相邻两项交换的方法进行升序排序时需要交换 ...

  6. HIVE分析函数

    ROWS BETWEEN含义,也叫做WINDOW子句: PRECEDING:往前 FOLLOWING:往后 CURRENT ROW:当前行 UNBOUNDED:起点,UNBOUNDED PRECEDI ...

  7. 14-----BBS论坛

    BBS论坛(十四) 14.1注册完成跳到上一个页面 (1)front/form.py # front/forms.py __author__ = 'derek' from ..forms import ...

  8. firewall 端口转发

    centos 7 使用背景:某次新购阿里云服务器安装nginx后配置80转8080的内部转发 systemctl status firewalld ---查看守护进程状态systemctl start ...

  9. 手工安装XDB 组件in oracle 11g

    #############. sample 1 install guide below step is only for oracle 11g database installation, 10g d ...

  10. python面向对象, 单例模式

    目录 单利模式 实现单利模式的方法 使用模块 使用__new__ 为了使类只能出现一个实例,我们可以使用 new 来控制实例的创建过程,代码如下: 使用装饰器 使用 metaclass 补充:元类(m ...