AlertDialog能够生成各种内容的对话框。可是每种对话框都会有这样的的结构:

类似下边这样的的:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDgwMDUzMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

这仅仅是最简单的对话框。

我们来看下创建一个对话框须要的步骤:

1、使用创建AlertDialog.Builder对象

2、调用AlertDialog.Builder的setTitle()或setCustomTitle()方法设置标题

3、调用AlertDialog.Builder的setIcon()方法设置图标

4、调用一些其它设置方法设置标题

5、调用AlertDialog.Builder的setPositiveButton()、setNegativeButton()或者setNeutralButton()加入多个button

6、调用create()方法创建AlertDialog对象,再调用AlertDialog对象的show()方法将该对话框显示出来。

新建Android项目,然后编写main.xml:

<?

xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<!-- 显示一个普通的文本编辑框组件 -->
<EditText
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"/>
<!-- 定义一个普通的button组件 -->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="简单对话框"
android:onClick="simple"
/>
<!-- 定义一个普通的button组件 -->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="简单列表项对话框"
android:onClick="simpleList"
/>
<!-- 定义一个普通的button组件 -->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="单选列表项对话框"
android:onClick="singleChoice"
/>
<!-- 定义一个普通的button组件 -->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="多选列表项对话框"
android:onClick="multiChoice"
/>
<!-- 定义一个普通的button组件 -->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自己定义列表项对话框"
android:onClick="customList"
/>
<!-- 定义一个普通的button组件 -->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自己定义View对话框"
android:onClick="customView"
/>
</LinearLayout>

这里是定义了6个button和一个文本显示框。而且设置了对应的onClick属性

接下来,我们就要编写主界面的java代码:AlertDialogTest.java

package org.crazyit.ui;

import android.app.Activity;
import android.app.AlertDialog; import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.TableLayout;
import android.widget.TextView; public class AlertDialogTest extends Activity
{
TextView show;
String[] items = new String[] {
"疯狂Java讲义", "疯狂Ajax讲义",
"轻量级Java EE企业应用实战",
"疯狂Android讲义" };
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
show = (TextView) findViewById(R.id.show);
} public void simple(View source)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this)
// 设置对话框标题
.setTitle("这是对话框标题")
// 设置图标
.setIcon(R.drawable.tools)
.setMessage("这是对话框内容");
// 为AlertDialog.Builder加入【确定】button
setPositiveButton(builder);
// 为AlertDialog.Builder加入【取消】button
setNegativeButton(builder)
.create()
.show();
} public void simpleList(View source)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this)
// 设置对话框标题
.setTitle("简单列表项对话框")
// 设置图标
.setIcon(R.drawable.tools)
// 设置简单的列表项内容
.setItems(items, new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
show.setText("你选中了《" + items[which] + "》");
}
});
// 为AlertDialog.Builder加入【确定】button
setPositiveButton(builder);
// 为AlertDialog.Builder加入【取消】button
setNegativeButton(builder)
.create()
.show();
} public void singleChoice(View source)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this)
// 设置对话框标题
.setTitle("单选列表项对话框")
// 设置图标
.setIcon(R.drawable.tools)
// 设置单选列表项,默认选中第二项(索引为1)
.setSingleChoiceItems(items, 1, new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
show.setText("你选中了《" + items[which] + "》");
}
});
// 为AlertDialog.Builder加入【确定】button
setPositiveButton(builder);
// 为AlertDialog.Builder加入【取消】button
setNegativeButton(builder)
.create()
.show();
}
public void multiChoice(View source)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this)
// 设置对话框标题
.setTitle("多选列表项对话框")
// 设置图标
.setIcon(R.drawable.tools)
// 设置多选列表项,设置勾选第2项、第4项
.setMultiChoiceItems(items
, new boolean[]{false , true ,false ,true}, null);
// 为AlertDialog.Builder加入【确定】button
setPositiveButton(builder);
// 为AlertDialog.Builder加入【取消】button
setNegativeButton(builder)
.create()
.show();
}
public void customList(View source)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this)
// 设置对话框标题
.setTitle("自己定义列表项对话框")
// 设置图标
.setIcon(R.drawable.tools)
// 设置自己定义列表项
.setAdapter(new ArrayAdapter<String>(this
, R.layout.array_item
, items), null);
// 为AlertDialog.Builder加入【确定】button
setPositiveButton(builder);
// 为AlertDialog.Builder加入【取消】button
setNegativeButton(builder)
.create()
.show();
} public void customView(View source)
{
//装载/res/layout/login.xml界面布局
TableLayout loginForm = (TableLayout)getLayoutInflater()
.inflate( R.layout.login, null);
new AlertDialog.Builder(this)
// 设置对话框的图标
.setIcon(R.drawable.tools)
// 设置对话框的标题
.setTitle("自己定义View对话框")
// 设置对话框显示的View对象
.setView(loginForm)
// 为对话框设置一个“确定”button
.setPositiveButton("登录" , new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog,
int which)
{
// 此处可运行登录处理
}
})
// 为对话框设置一个“取消”button
.setNegativeButton("取消", new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog,
int which)
{
// 取消登录。不做不论什么事情。 }
})
// 创建、并显示对话框
.create()
.show();
} private AlertDialog.Builder setPositiveButton(
AlertDialog.Builder builder)
{
// 调用setPositiveButton方法加入确定button
return builder.setPositiveButton("确定", new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
show.setText("单击了【确定】button! ");
}
});
} private AlertDialog.Builder setNegativeButton(
AlertDialog.Builder builder)
{
// 调用setNegativeButton方法加入取消button
return builder.setNegativeButton("取消", new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
show.setText("单击了【取消】button!");
}
});
}
}

在这里边,第五个和第六个button用到了两个样式:array_item.xml和login.xml

我们看下他们的内容:

array_item.xml:

<?

xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TextView"
android:textColor="#f0f"
android:textSize="30dp"
android:shadowColor="#ff0"
android:shadowRadius="2"
android:shadowDx="5"
android:shadowDy="5"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

login.xml:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/loginForm"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="username:"
android:textSize="10pt"
/>
<!-- 输入username的文本框 -->
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请填写登录帐号"
android:selectAllOnFocus="true"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="password:"
android:textSize="10pt"
/>
<!-- 输入password的文本框 -->
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请填写password"
android:password="true"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="电话号码:"
android:textSize="10pt"
/>
<!-- 输入电话号码的文本框 -->
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请填写您的电话号码"
android:selectAllOnFocus="true"
android:phoneNumber="true"
/>
</TableRow>
</TableLayout>

通过AlertDialog能够制作出不同风格的对话框,在非常多时候都比較实用

而且我们能够通过确定button来把数据通过Intent传递到另外一个界面中。

Adrnoid开发系列(二十五):使用AlertDialog创建各种类型的对话框的更多相关文章

  1. Java开发学习(二十五)----使用PostMan完成不同类型参数传递

    一.请求参数 请求路径设置好后,只要确保页面发送请求地址和后台Controller类中配置的路径一致,就可以接收到前端的请求,接收到请求后,如何接收页面传递的参数? 关于请求参数的传递与接收是和请求方 ...

  2. BizTalk开发系列(二十五) SQL Adapter

    SQL Server 是.NET开发的首选数据库.当然开发BizTalk应用程序很多也离不了SQL Server.针对SQL Server的数据操作BizTalk 提供了SQL Adapter作为与数 ...

  3. BizTalk开发系列(二十八) MSMQ 适配器

    MSMQ(MicroSoft Message Queue,微软消息队列)是在多个不同的应用之间实现相互通信的一种异步传输模式,相互通信的应用可以分布于同一台机器上,也可以分布于相连的网络空间 中的任一 ...

  4. BizTalk开发系列(二十二) 开发自定义Map Functoid

    尽管 BizTalk Server 提供许多Functoid以支持一系列不同的操作,但仍可能会遇到需要其他方法的情况.<BizTalk开发系列 Map扩展开发>介绍了通过使用自定义 XSL ...

  5. 网站开发进阶(二十五)js如何将html表格导出为excel文件

    js如何将html表格导出为excel文件        赠人玫瑰,手留余香.若您感觉此篇博文对您有用,请花费2秒时间点个赞,您的鼓励是我不断前进的动力,共勉! jsp页面数据导出成excel的方法很 ...

  6. SSE图像算法优化系列二十五:二值图像的Euclidean distance map(EDM)特征图计算及其优化。

    Euclidean distance map(EDM)这个概念可能听过的人也很少,其主要是用在二值图像中,作为一个很有效的中间处理手段存在.一般的处理都是将灰度图处理成二值图或者一个二值图处理成另外一 ...

  7. BizTalk开发系列(三十五) TCP/IP 适配器

    BizTalk 的TCP/IP适配器最初是为英国的保健行业开发.该适配器属于BizTalk进程内适配器,将消息通过TCP/IP 套接字符串在BizTalk服务器与远程客户端间进行通讯. TCP/IP适 ...

  8. BizTalk开发系列(二十六) 使用Web Service

    Web Service是在构建SOA平台中广泛使用的技术.在BizTalk开发过程中使用SOAP适配器接收和发送 Web Services 请求.业务流程可以发布为 Web Services 并使用外 ...

  9. BizTalk开发系列(二十四) BizTalk项目框架建议

    Asp.NET有MVC框架,大部份的开发都是按照MVC进行的.BizTalk是面向消息的开发,不能完全采用分层的开发模式.而微软只提供了 BizTalk项目开发的基本策略,通过分析相关的Complex ...

随机推荐

  1. 用200行Python代码“换脸”

    介绍 本文将介绍如何编写一个只有200行的Python脚本,为两张肖像照上人物的“换脸”. 这个过程可分为四步: 检测面部标记. 旋转.缩放和转换第二张图像,使之与第一张图像相适应. 调整第二张图像的 ...

  2. Zoj 3842 Beauty of Array

    Problem地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5520 根据题目的要求,需要算出所有连续子数组的the be ...

  3. 如何提高banner设计含量--网上的一篇文章--感悟

    "修改": 本质上是改什么?改大小?图片?文字?颜色? 老板说:修改本质上是提高“设计含量”.检测一个作品设计含量的高低,可以将作品中每一个设计元素进行分析,看它的“属性”与“操作 ...

  4. android 项目中log信息的正确处理

    我们在做项目中,调试程序的时候通常是debug或者打log,特别是看一些json数据之类的,都喜欢打log看看,可是在项目上线时log信息一定不能被打印出来,大家能够平时下载一些做的烂的app,连接e ...

  5. 为joomla加入�下拉菜单的方法

    用 Joomla! 建站的大多数站长都须要在站点前台使用下拉菜单(dropdown menu),或者叫弹出菜单(slide menu),由于这样能够在有限的页面空间上公布很多其它的导航菜单,而且能够进 ...

  6. Codeforces 57C Array dp暴力找到规律

    主题链接:点击打开链接 的非增量程序首先,计算, 如果不增加的节目数量x, 非减少一些方案是x 答案就是 2*x - n 仅仅需求得x就可以. 能够先写个n3的dp,然后发现规律是 C(n-1, 2* ...

  7. Python获取并修改hosts

    #!/usr/bin/env python #-*- coding: utf-8 -*- #谷歌host修改脚本 #author 坤子<root#pythonpy.com> #date 0 ...

  8. Struts 2 OGNL

    1.什么是OGNL? 2.Struts 2 OGNL 表达式      ======================  华丽丽的分割线  ======================  1.什么是OG ...

  9. 全球在一个 level 上思考的价值观和想法是一样的(转)

    近日,福布斯中文版总编辑周建工对话马云,谈到腾讯频繁的大笔收购,马云点评称腾讯收购的所有的案子,老百性都看得懂,这就错了.战略就像买股票一样,如果老太太都开始买股票了,一定有问题. 以下是对话内容,转 ...

  10. uva 10271 Chopsticks(dp)

    题目连接:10271 - Chopsticks 题目大意:给出m和n, 然后给出n根筷子从小到大给出, 现在要从这n根筷子中选出m + 8组筷子, 每组筷子包括三根, 现在要求所有m + 8组每组筷子 ...