android中常见对话框之一AlertDialog
来源:互联网 发布:中文字体软件 编辑:程序博客网 时间:2024/06/07 02:20
在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开始构造出来的。
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个按钮。
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,第一个参数是要显示的数据的数组,第二个参数指定默认选中项,第三个参数设置监听处理事件。
public class MainActivity extends Activity { //声明选中项变量 private int selectedCityIndex = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //定义城市数组 final String[] arrayCity = new String[] { "杭州", "纽约", "威尼斯", "北海道" }; //实例化AlertDialog对话框 AlertDialog alertDialog = new AlertDialog.Builder(this) .setTitle("你最喜欢哪个地方?") //设置标题 .setIcon(R.drawable.ic_launcher) //设置图标 //设置对话框显示一个单选List,指定默认选中项,同时设置监听事件处理 .setSingleChoiceItems(arrayCity, 0, 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,第一个参数是要显示的数据的数组,第二个参数指定默认选中项,第在个参数设置监听处理事件。
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对话框 AlertDialog 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 = 0; 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
- 常见对话框之一AlertDialog
- 第30章、常见对话框之一AlertDialog(从零开始学Android)
- Android创建AlertDialog对话框 ==之一
- Android中显示AlertDialog对话框
- Android:AlertDialog.Builder创建常见对话框
- Android学习笔记常见对话框AlertDialog
- Android:AlertDialog.Builder创建常见对话框
- Android中Alertdialog对话框点击消失?
- Android中 Alertdialog对话框点击消失?
- Android中AlertDialog对话框的使用
- Android中AlertDialog(对话框)的使用
- Android中对话框之一:对话框基础
- Android中的对话框AlertDialog
- 【Android】对话框 AlertDialog
- Android对话框之AlertDialog
- Android 对话框 (AlertDialog & ProgressDialog)
- android AlertDialog对话框
- 读书笔记:持续集成软件质量改进和风险降低之道-第三章 和第四章
- Sicily2014
- IDS(入侵检测系统)
- jsaper导出的excel自动适应长度
- 李天平:技术以外的功夫
- android中常见对话框之一AlertDialog
- Android Http抓包
- phonegap自定义插件 笔记
- 详解C中volatile关键字
- 自定义简单的工具栏类(真彩色)
- 关于控制Dcom程序使用端口的问题及解决方式
- IOS开发中实现复选框
- 嵌入式 hi3518c移植wget实现http或者ftp下载以及wget选项详解
- C# 允许多个套接字绑定在同一个端口上 端口重用