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. AnsiString 在 Delphi 中虽然不可用,但是,在 C++ 中可以用

    [C++] C++ Builder 中 Ansi 编码的字符串在Android/iOS程序中显示的问题 呃,这个问题说起来,其实也不麻烦,C++ Builder 本身在 TEncoding 做了处理, ...

  2. 计算阶乘并显示_winform (20以后的阶乘溢出)

    编写一个窗体应用程序,计算n的阶乘,显示其结果,同时,将结果显示在一个标签中. 新建窗体应用程序(如下),新建控件label1,label2,label3,textBOX1,button1,butto ...

  3. 【转】CentOS 6.5 生产环境优化指南

    原文链接:https://www.deepwebcn.com/82.html centos6.5 CentOS 6.5 系统安装之后并不能立即投入生产环境使用,常常需要先经过我们运维人员的优化才行.优 ...

  4. ZOJ 3879 Capture the Flag 15年浙江省赛K题

    每年省赛必有的一道模拟题,描述都是非常的长,题目都是蛮好写的... sigh... 比赛的时候没有写出这道题目 :( 题意:首先输入4个数,n,q,p,c代表有n个队伍,q个服务器,每支队伍的初始分数 ...

  5. iOS开发笔记--使用blend改变图片颜色

    最近对Core Animation和Core Graphics的内容东西比较感兴趣,自己之前也在这块相对薄弱,趁此机会也想补习一下这块的内容,所以之后几篇可能都会是对CA和CG学习的记录的文章. 在应 ...

  6. Python 内置函数 range的使用

    内置range函数可以用来方便的产生等差的数值序列.如: >>> range(5) [0, 1, 2, 3, 4] >>> range(1,5) [1, 2, 3, ...

  7. php抓取ajax页面返回图片。

    要抓取的页面:http://pic.hao123.com/ 当我们往下滚动的时候,图片是用ajax来动态获取的.这就需要我们仔细分析页面了. 可以看到,异步加载的ajax文件为: http://pic ...

  8. 基于visual Studio2013解决算法导论之017查找第n小元素

     题目 查找第n小元素 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <malloc.h> ...

  9. 使用ffmpeg将BMP图片编码为x264视频文件,将H264视频保存为BMP图片,yuv视频文件保存为图片的代码

    ffmpeg开源库,实现将bmp格式的图片编码成x264文件,并将编码好的H264文件解码保存为BMP文件. 实现将视频文件yuv格式保存的图片格式的測试,图像格式png,jpg, gif等等測试均O ...

  10. 零成本建立的.NET小组开发平台

    前言 说道.NET开发平台,首先想到的就是Visual Studio,建立.NET小组开发平台自然首推TFS.但其花费却也是相当昂贵的(当然在本国可以无视这些成本),近期的开发中接触到一些开源软件并读 ...