在Android应用中,有多种对话框:Dialog、AlertDialog、ProgressDialog、时间、日期等对话框。

  (1)Dialog类,是一切对话框的基类,需要注意的是,Dialog类虽然可以在界面上显示,但是并非继承与习惯的View类,而是直接从java.lang.Object开始构造出来的,类似于Activity,Dialog也是有生命周期的,它的生命周期由Activity来维护。Activity负责生产,保存,回复它,在生命周期的每个阶段都有一些回调函数供系统方向调用。

  (2)AlertDialog是Dialog的一个直接子类,AlertDialog也是Android系统当中最常用的对话框之一。一个AlertDialog可以有两个Button或3个Button,可以对一个AlertDialog设置title和message.不能直接通过AlertDialog的构造函数来生成一个AlertDialog.一般生成AlertDialog的时候都是通过它的一个内部静态类AlertDialog.builder来构造的。

  (3)顾名思义,这个Dialog负责给用户显示进度的相关情况,它是AlertDialog的一个子类。

  本章我们着重讲解一下AlertDialog!

  AlertDialog的构造方法全部是Protected的,所以不能直接通过new一个AlertDialog来创建出一个AlertDialog。

  要创建一个AlertDialog,就要用到AlertDialog.Builder中的create()方法。

  使用AlertDialog.Builder创建对话框需要了解以下几个方法:

    setTitle :为对话框设置标题     

setIcon :为对话框设置图标    

   setMessage:为对话框设置内容   

   setView : 给对话框设置自定义样式  

   setItems :设置对话框要显示的一个list,一般用于显示几个命令时    

   setMultiChoiceItems :用来设置对话框显示一系列的复选框   

 setNeutralButton :普通按钮

 setPositiveButton :给对话框添加"Yes"按钮   

   setNegativeButton :对话框添加"No"按钮    

  create : 创建对话框    

 show :显示对话框

一、简单对话框

  Dialog类虽然可以在界面上显示,但是并非继承与习惯的View类,而是直接从java.lang.Object开始构造出来的。

  1、打开“src/com.genwoxue.alertdialog_a/MainActivity.java”文件。

  然后输入以下代码:

.package com.example.alertdialog_a;
.
.import android.os.Bundle;
.import android.app.Activity;
.import android.app.AlertDialog.Builder;
.import android.app.AlertDialog;
.
.public class MainActivity extends Activity {
.
. @Override
. protected void onCreate(Bundle savedInstanceState) {
. super.onCreate(savedInstanceState);
. setContentView(R.layout.activity_main);
. //AlertDialog的构造方法全部是Protected的,所以不能直接通过new一个AlertDialog来创建出一个AlertDialog。
. //要创建一个AlertDialog,就要用到AlertDialog.Builder中的create()方法
. Builder adInfo=new AlertDialog.Builder(this);
. adInfo.setTitle("简单对话框"); //设置标题
. adInfo.setMessage("这是一个美丽的传说,精美的石头会唱歌……"); //设置内容
. adInfo.setIcon(R.drawable.ic_launcher); //设置图标
. adInfo.create();
. adInfo.show();
.
. }
.}

2、运行,显示界面:

  

二、带按钮的AlertDialog

  我们在执行删除、确认等操作时,常常在对话框中单击按钮,AlertDialog可以显示3个按钮。

  1、打开“src/com.genwoxue.alertdialog_bMainActivity.java”文件。

  然后输入以下代码:

.package com.example.alertdialog_b;
.
.import android.os.Bundle;
.import android.app.Activity;
.import android.app.AlertDialog.Builder;
.import android.app.AlertDialog;
.import android.content.DialogInterface;
.
.public class MainActivity extends Activity {
.
. @Override
. protected void onCreate(Bundle savedInstanceState) {
. super.onCreate(savedInstanceState);
. setContentView(R.layout.activity_main);
.
. Builder dialog = new AlertDialog.Builder(this);
. dialog.setTitle("确定删除?");
. dialog.setMessage("您确定删除该条信息吗?");
. dialog.setIcon(R.drawable.ic_launcher);
. //为“确定”按钮注册监听事件
. dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
. @Override
. public void onClick(DialogInterface dialog, int which) {
. // 根据实际情况编写相应代码。
. }
. });
. //为“取消”按钮注册监听事件
. dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
. @Override
. public void onClick(DialogInterface dialog, int which) {
. // 根据实际情况编写相应代码。
. }
. });
. //为“查看详情”按钮注册监听事件
. dialog.setNeutralButton("查看详情", new DialogInterface.OnClickListener() {
. @Override
. public void onClick(DialogInterface dialog, int which) {
. // 根据实际情况编写相应代码。
. }
. });
. dialog.create();
. dialog.show();
. }
.}

2、运行,显示界面:

  

三、带有单选按钮、类似ListView的AlertDialog对话框

  setSingleChoiceItems(CharSequence[] items, int checkedItem,final OnClickListener listener)方法来实现类似ListView的AlertDialog,第一个参数是要显示的数据的数组,第二个参数指定默认选中项,第三个参数设置监听处理事件。

  1、打开“src/com.genwoxue.alertdialog_c/MainActivity.java”文件。

  然后输入以下代码:

.package com.genwoxue.alertdialog_c;
.
.import android.app.Activity;
.import android.app.AlertDialog;
.import android.app.Dialog;
.import android.content.DialogInterface;
.import android.os.Bundle;
.import android.widget.Toast;
.
.public class MainActivity extends Activity {
. //声明选中项变量
. private int selectedCityIndex = ;
.
. @Override
. public void onCreate(Bundle savedInstanceState) {
. super.onCreate(savedInstanceState);
. setContentView(R.layout.activity_main);
. //定义城市数组
. final String[] arrayCity = new String[] { "杭州", "纽约", "威尼斯", "北海道" };
.
. //实例化AlertDialog对话框
. Dialog alertDialog = new AlertDialog.Builder(this)
. .setTitle("你最喜欢哪个地方?") //设置标题
. .setIcon(R.drawable.ic_launcher) //设置图标
. //设置对话框显示一个单选List,指定默认选中项,同时设置监听事件处理
. .setSingleChoiceItems(arrayCity, , new DialogInterface.OnClickListener() {
.
. @Override
. public void onClick(DialogInterface dialog, int which) {
. selectedCityIndex = which; //选中项的索引保存到选中项变量
. }
. })
. //添加取消按钮并增加监听处理
. .setNegativeButton("取消", new DialogInterface.OnClickListener() {
. @Override
. public void onClick(DialogInterface dialog, int which) {
. // TODO Auto-generated method stub
. }
. })
. //添加确定按钮并增加监听处理
. .setPositiveButton("确认", new DialogInterface.OnClickListener() {
. @Override
. public void onClick(DialogInterface dialog, int which) {
. Toast.makeText(getApplication(), arrayCity[selectedCityIndex], Toast.LENGTH_SHORT).show();
. }
. })
. .create();
. alertDialog.show();
. }
.}

2、运行,显示界面:

  

四、带有复选框、类似ListView的AlertDialog对话框

  setMultiChoiceItems(CharSequence[] items, boolearn[] checkedItems,final OnMultiChoiceClickListener listener)方法来实现类似ListView的AlertDialog,第一个参数是要显示的数据的数组,第二个参数指定默认选中项,第在个参数设置监听处理事件。

  1、打开“src/com.genwoxue.alertdialog_d/MainActivity.java”文件。

  然后输入以下代码:

.package com.genwoxue.alertdialog_d;
.
.import android.app.Activity;
.import android.app.AlertDialog;
.import android.app.Dialog;
.import android.content.DialogInterface;
.import android.os.Bundle;
.import android.widget.Toast;
.
.
.public class MainActivity extends Activity {
.
. @Override
. public void onCreate(Bundle savedInstanceState) {
. super.onCreate(savedInstanceState);
. setContentView(R.layout.activity_main);
. //定义运动数组
. final String[] arraySport = new String[] { "足球", "篮球", "网球", "乒乓球" };
. final boolean[] arraySportSelected = new boolean[] {false, false, false, false};
.
. //实例化AlertDialog对话框
. Dialog alertDialog = new AlertDialog.Builder(this)
. .setTitle("你喜欢哪些运动?") //设置标题
. .setIcon(R.drawable.ic_launcher) //设置图标
. //设置对话框显示一个复选List,指定默认选中项,同时设置监听事件处理
. .setMultiChoiceItems(arraySport, arraySportSelected, new DialogInterface.OnMultiChoiceClickListener() {
.
. @Override
. public void onClick(DialogInterface dialog, int which, boolean isChecked) {
. arraySportSelected[which] = isChecked; //选中项的布尔真假保存到选中项变量
. }
. })
. //添加取消按钮并增加监听处理
. .setPositiveButton("确认", new DialogInterface.OnClickListener() {
.
. @Override
. public void onClick(DialogInterface dialog, int which) {
. StringBuilder stringBuilder = new StringBuilder();
. for (int i = ; i < arraySportSelected.length; i++) {
. if (arraySportSelected[i] == true){
. stringBuilder.append(arraySport[i] + "、");
. }
. }
. Toast.makeText(getApplication(), stringBuilder.toString(), Toast.LENGTH_SHORT).show();
. }
. })
.
. //添加确定按钮并增加监听处理
. .setNegativeButton("取消", new DialogInterface.OnClickListener() {
.
. @Override
. public void onClick(DialogInterface dialog, int which) {
. // TODO Auto-generated method stub
. }
. })
. .create();
.
. alertDialog.show();
. }
.}

2、运行,显示界面:

  

android中常见对话框之一AlertDialog的更多相关文章

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

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

  2. Android 中常见控件的介绍和使用

    1 TextView文本框 1.1 TextView类的结构 TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域.TextView类的层次关系如下: java.la ...

  3. Android Studio常见对话框(普通对话框、单选对话框、多选对话框、进度条对话框、消息对话框、自定义对话框)

    Android Studio常见对话框(普通对话框.单选对话框.多选对话框.进度条对话框.消息对话框.自定义对话框) 1.普通对话框 2.单选对话框 3.多选对话框 4.进度条对话框 5.消息对话框 ...

  4. Android中常见的内存泄漏

    为什么会产生内存泄漏? 当一个对象已经不需要再使用了,本该被回收时,而有另外一个正在使用的对象持有它的引用从而导致它不能被回收,这导致本该被回收的对象不能被回收而停留在堆内存中,这就产生了内存泄漏. ...

  5. Android中Dialog对话框的调用及监听

    Android中经常会需要在Android界面上弹出一些对话框提示用户,比如App的退出的时候都会有各种框来挽留你的心,支付宝的时候输入密码的密码框,非常常见及其实用的功能,类似于JS中的alter, ...

  6. Android中常见的MVC模式

    MVC模式的简要介绍 MVC是三个单词的缩写,分别为: 模型(Model),视图(View)和控制Controller). MVC模式的目的就是实现Web系统的职能分工. Model层实现系统中的业务 ...

  7. Android中Dialog对话框

    布局文件xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns ...

  8. Android中弹出对话框,AlertDialog关键代码

    写在这里便于以后查看. Android中弹出对话框的关键代码: btn01.setOnClickListener(new OnClickListener() { @Override public vo ...

  9. (转载)Android开发——Android中常见的4种线程池(保证你能看懂并理解)

    0.前言 转载请注明出处:http://blog.csdn.net/seu_calvin/article/details/52415337 使用线程池可以给我们带来很多好处,首先通过线程池中线程的重用 ...

随机推荐

  1. my class 2.0

    www.dropbox.com www.google.com/voice www.prezi.com www.evernote.com

  2. java基础之类与继承 详解

    Java:类与继承 对于面向对象的程序设计语言来说,类毫无疑问是其最重要的基础.抽象.封装.继承.多态这四大特性都离不开类,只有存在类,才能体现面向对象编程的特点,今天我们就来了解一些类与继承的相关知 ...

  3. iOS 使用两个tableview的瀑布流

    代码 悦德财富:https://www.yuedecaifu.com 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...

  4. 移动设备和SharePoint 2013 - 第1部分:概述

    博客地址:http://blog.csdn.net/foxdave 原文地址 在该系列文章中,作者展示了SharePoint 2013最显著的新功能概观--对移动设备的支持. 该系列文章: 移动设备和 ...

  5. 解决使用IIS5.0配置的FTP服务器,客户端浏览器访问时无法获取目录列表的问题。

    我在windows xp sp3下利用iis构架了FTP服务器,允许且只允许匿名用户登陆.但刚开始配置好后,不管是使用命令行模式还是使用浏览器都发现无法访问. 于是怀疑防火墙屏蔽端口所致,果不其然,在 ...

  6. 2016 - 1 -17 GCD学习总结

    一:GCD中的两个核心概念,队列与任务: 1.任务:执行什么操作.(代码块 block) 任务执行的类型分为以下两种: 1.1同步执行任务:在当前线程执行任务.不会开辟新的线程. 1.2异步执行任务: ...

  7. Ubuntu安装Fcitx(小企鹅五笔输入法)

    安装配置如下: 1. 安装 fcitx sudo apt-get install fcitx 2. 配置默认输入法为 fcitx im-switch -s fcitx // 注意无须加 sudo 3. ...

  8. 【Android UI】 Shape详解

    在Android开发过程中,经常需要改变控件的默认样式, 那么通常会使用多个图片来解决.不过这种方式可能需要多个图片,比如一个按钮,需要点击时的式样图片,默认的式样图片. 这样就容易使apk变大. 那 ...

  9. Logger日志打印普通方法

    using System; using System.IO; using System.Text; namespace Core { public class LogHelper { private ...

  10. 在Web.config中注册自定义控件

    之前都是在每个页面的顶端注册用户控件或者是自定义控件,这样不简洁,而且麻烦. 现在只要在在web.config文件中声明,其他地方就可以直接使用前缀加控件如下黄色代码所示: <configura ...