在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. [vijos P1023] Victoria的舞会3

    这… 本来想学习一下Tarjan算法的,没想到码都码好了发现这题不是求强连通分量而是简单的连通分量…图论基础都还给老师了啊啊啊!最后深搜通通解决! v标记是否被访问过,scc标记每个的祖先(本来想写T ...

  2. 赋值运算符、拷贝初始化和this指针

    一.赋值运算符和拷贝构造函数(重载技术) 赋值运算符和拷贝构造函数有编译器默认提供,但如果想做更复杂的事,需要重载. 1.下面用一个简单的例子先区分一下赋值运算符和拷贝构造函数: #include&l ...

  3. matlab调用opencv函数的配置

    环境: VS2010 活动解决方案平台x64 WIN 8.1 Opencv 2.4.3 Matlab 2012a 1.  首先保证vs2010能正确调用opencv函数, 2.  Matlab中选择编 ...

  4. VMware Workstation 10.0.0.1295980 CN

    从V10版本开始,VMware Workstation 官方自带简体中文了,以后大家不需要汉化啦! Winner of more than 50 industry awards, VMware Wor ...

  5. SharePoint 2013 Nintex Workflow 工作流帮助(十)

    博客地址 http://blog.csdn.net/foxdave 工作流动作 23. Create appointment(企业版才有) 该操作用于在Microsoft Exchange中创建一个商 ...

  6. Android 自带图标库 android.R.drawable

    在xml文件中调用. android:title="@string/secure_connect"android:orderInCategory="100"an ...

  7. swift语言之多线程操作和操作队列(下)———坚持51天吃掉大象(写技术文章)

    欢迎有兴趣的朋友,参与我的美女同事发起的活动<51天吃掉大象>,该美女真的很疯狂,希望和大家一起坚持51天做一件事情,我加入这个队伍,希望坚持51天每天写一篇技术文章.关注她的微信公众号: ...

  8. UIView 翻转动画

    [_mapView removeFromSuperview]; [self addSubview:_tableView]; //应将self.view设置为翻转对象 [UIView transitio ...

  9. 《day09---继承-抽象类-接口》

    //面向对象_继承_概述---单继承_多继承. //描述学生. /* class Student { //属性. String name; int age; //行为: void study() { ...

  10. 使用sslsplit嗅探tls/ssl连接

    首先发一个从youtube弄到的sslsplit的使用教程 http://v.qq.com/page/x/k/s/x019634j4ks.html 我最近演示了如何使用mitmproxty执行中间人攻 ...