前言
         欢迎大家我分享和推荐好用的代码段~~
声明
         欢迎转载,但请保留文章原始出处:
         CSDN:http://www.csdn.net
         雨季o莫忧离:http://blog.csdn.net/luckkof

正文

摘要: 创建对话框 一个对话框一般是一个出现在当前Activity之上的一个小窗口. 处于下面的Activity失去焦点, 对话框接受所有的用户交互. 对话框一般用于提示信息和与当前应用程序直接相关的小功能.Android API 支持下列类型 ...

创建对话框
一个对话框一般是一个出现在当前Activity之上的一个小窗口. 处于下面的Activity失去焦点, 对话框接受所有的用户交互. 对话框一般用于提示信息和与当前应用程序直接相关的小功能.
Android API 支持下列类型的对话框对象:
警告对话框 AlertDialog:  一个可以有0到3个按钮, 一个单选框或复选框的列表的对话框. 警告对话框可以创建大多数的交互界面, 是推荐的类型.
进度对话框 ProgressDialog:  显示一个进度环或者一个进度条. 由于它是AlertDialog的扩展, 所以它也支持按钮.
日期选择对话框 DatePickerDialog:  让用户选择一个日期.
时间选择对话框 TimePickerDialog:  让用户选择一个时间.
如果你希望自定义你的对话框, 可以扩展Dialog类.
Showing a Dialog 显示对话框
一个对话框总是被创建和显示为一个Activity的一部分. 你应该在Activity的onCreateDialog(int)中创建对话框. 当你使用这个回调函数时,Android系统自动管理每个对话框的状态并将它们和Activity连接, 将Activity变为对话框的"所有者". 这样,每个对话框从Activity继承一些属性. 例如,当一个对话框打开时, MENU键会显示Activity的菜单, 音量键会调整Activity当前使用的音频流的音量.
注意: 如果你希望在onCreateDialog()方法之外创建对话框, 它将不会依附在Activity上. 你可以使用setOwnerActivity(Activity)来将它依附在Activity上.
当你希望显示一个对话框时, 调用showDialog(int)并将对话框的id传给它.
当一个对话框第一次被请求时,Android调用onCreateDialog(int). 这里是你初始化对话框的地方. 这个回调函数传入的id和showDialog(int)相同. 创建对话框之后,将返回被创建的对象.
在对话框被显示之前,Android还会调用onPrepareDialog(int, Dialog). 如果你希望每次显示对话框时有动态更改的内容, 那么就改写这个函数. 该函数在每次一个对话框打开时都调用. 如果你不定义该函数,则对话框每次打开都是一样的. 该函数也会传入对话框的id以及你在onCreateDialog()中创建的Dialog对象.
最好的定义onCreateDialog(int) 和onPrepareDialog(int, Dialog) 的方法就是使用一个switch语句来检查传入的id. 每个case创建相应的对话框. 例如, 一个游戏使用两个对话框: 一个来指示游戏暂停,另一个指示游戏结束. 首先, 为它们定义ID:static final int DIALOG_PAUSED_ID = 0;
static final int DIALOG_GAMEOVER_ID = 1;
然后, 在onCreateDialog(int)中加入一个switch语句:

protected Dialog onCreateDialog(int id) {Dialog dialog;switch(id) {case DIALOG_PAUSED_ID:// do the work to define the pause Dialogbreak;case DIALOG_GAMEOVER_ID:// do the work to define the game over Dialogbreak;default:dialog = null;}return dialog;
} 
注意: 在这个例子中, case语句为空因为定义Dialog的程序在后面会有介绍.
在需要显示对话框是, 调用showDialog(int), 传入对话框的id:
showDialog(DIALOG_PAUSED_ID);Dismissing a Dialog 解除对话框
当你准备关闭对话框时, 你可以使用dismiss()函数. 如果需要的话, 你也可以从Activity调用dismissDialog(int), 二者效果是一样的.
如果你使用onCreateDialog(int)来管理你的对话框的状态, 那么每次你的对话框被解除时, 该对话框对象的状态会被Activity保存. 如果你决定你不再需要这个对象或者需要清除对话框的状态, 那么你应该调用 removeDialog(int). 这将把所有该对象的内部引用移除, 如果该对话框在显示的话将被解除.
Using dismiss listeners 使用解除监听器
如果你希望在对话框解除时运行某些程序, 那么你应该给对话框附加一个解除监听器.
首先定义DialogInterface.OnDismissListener接口. 这个接口只有一个方法, onDismiss(DialogInterface), 该方法将在对话框解除时被调用.
然后将你的OnDismissListener实现传给setOnDismissListener().
然而,注意对话框也可以被"取消". 这是一个特殊的情形, 它意味着对话框被用户显式的取消掉. 这将在用户按下"back"键时, 或者对话框显式的调用cancel()(按下对话框的cancel按钮)时发生. 当一个对话框被取消时, OnDismissListener将仍然被通知, 但如果你希望在对话框被显示取消(而不是正常解除)时被通知, 则你应该使用setOnCancelListener()注册一个DialogInterface.OnCancelListener.
Creating an AlertDialog 创建警告对话框
An AlertDialog is an extension of the Dialog class. It is capable of constructing most dialog user interfaces and is the suggested dialog type. You should use it for dialogs that use any of the following features:
一个警告对话框是对话框的一个扩展. 它能够创建大多数对话框用户界面并且是推荐的对话框类新星. 对于需要下列任何特性的对话框,你都应该使用它:
一个标题
一条文字消息
1个-3个按钮
一个可选择的列表(单选框或者复选框)
要创建一个AlertDialog, 使用AlertDialog.Builder子类. 使用AlertDialog.Builder(Context)来得到一个Builder, 然后使用该类的公有方法来定义AlertDialog的属性. 设定好以后, 使用create()方法来获得AlertDialog对象.
下面的主题展示了如何为AlertDialog定义不同的属性, 使用AlertDialog.Builder类. 如果你使用这些示例代码, 你可以在onCreateDialog()中返回最后的Dialog对象来获得图片中对话框的效果.
Adding buttons 增加按钮
要创建一个如图所示的窗口, 使用set...Button()方法:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?").setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int id) {MyActivity.this.finish();}}).setNegativeButton("No", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int id) {dialog.cancel();}});
AlertDialog alert = builder.create();
首先,使用setMessage(CharSequence)为对话框增加一条消息。 然后, 开始连续调用方法, 使用setCancelable(boolean)将对话框设为不可取消(不能使用back键来取消)。对每一个按钮,使用set...Button()方法,该方法接受按钮名称和一个DialogInterface.OnClickListener,该监听器定义了当用户选择该按钮时应做的动作。
注意:对每种按钮类型,只能为AlertDialog创建一个。也就是说,一个AlertDialog不能有两个以上的"positive"按钮。这使得可能的按钮数量最多为三个:肯定、否定、中性。这些名字和实际功能没有联系,但是将帮助你记忆它们各做什么事情。
Adding a list 增加列表
要创建一个具有可选项的AlertDialog,使用setItems()方法:
final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int item) {Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();}
});
AlertDialog alert = builder.create();
首先增加一个标题。然后使用setItems()增加一个可选列表,该列表接受一个选项名称的列表和一个DialogInterface.OnClickListener, 后者定义了选项对应的响应。

Adding checkboxes and radio buttons 增加单选框和复选框
要创建一个带有多选列表或者单选列表的对话框, 使用setMultiChoiceItems()和setSingleChoiceItems()方法。如果你在onCreateDialog()中创建可选择列表, Android会自动管理列表的状态. 只要activity仍然活跃, 那么对话框就会记住刚才选中的选项,但当用户退出activity时,该选择丢失。
注意: 要在你的acitivity离开和暂停时保存选择, 你必须在activity的声明周期中正确的保存和恢复设置。为了永久性保存选择,你必须使用数据存储技术中的一种。
要创建一个具有单选列表的AlertDialog,只需将一个例子中的setItems()换成 setSingleChoiceItems():
final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int item) {Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();}
});
AlertDialog alert = builder.create();
第二个参数是默认被选中的选项位置,使用“-1”来表示默认情况下不选中任何选项。

Creating a ProgressDialog 创建进度对话框
一个ProgressDialog(进度对话框)是AlertDialog的扩展。它可以显示一个进度的动画——进度环或者进度条。这个对话框也可以提供按钮,例如取消一个下载等。
打开一个进度对话框很简单,只需要调用 ProgressDialog.show()即可。例如,上图的对话框可以不通过onCreateDialog(int),而直接显示:
ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "",
"Loading. Please wait...", true);
第一个参数是应用程序上下文。第二个为对话框的标题(这里为空),第三个为对话框内容, 最后一个为该进度是否为不可确定的(这只跟进度条的创建有关,见下一节)。
进度对话框的默认样式为一个旋转的环。如果你希望显示进度值,请看下一节。
Showing a progress bar 显示进度条
使用一个动画进度条来显示进度:
使用 ProgressDialog(Context)构造函数来初始化一个ProgressDialog对象。
将进度样式设置为"STYLE_HORIZONTAL",使用setProgressStyle(int)方法。并且设置其它属性,例如内容等。
在需要显示时调用show()或者从onCreateDialog(int)回调函数中返回该ProgressDialog。
你可以使用 setProgress(int)或者incrementProgressBy(int)来增加显示的进度。
例如,你的设置可能像这样:ProgressDialog progressDialog;
progressDialog = new ProgressDialog(mContext);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);
设置很简单。大部分创建进度对话框需要的代码是在更新它的进程中。你可能需要在一个新的线程中更新它,并使用Handler来将进度报告给Activity。如果你不熟悉使用Handler和另外的线程,请看下列例子,该例子使用了一个新的线程来更新进度。
Example ProgressDialog with a second thread 例--使用一个线程来显示进度对话框
这个例子使用一个线程来跟踪一个进程的进度(其实为从1数到100)。每当进度更新时,该线程通过Handler给主activity发送一个消息。主Activity更新ProgressDialog.package com.example.progressdialog;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class NotificationTest extends Activity {static final int PROGRESS_DIALOG = 0;Button button;ProgressThread progressThread;ProgressDialog progressDialog;/** Called when the activity is first created. */public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main); // Setup the button that starts the progress dialogbutton = (Button) findViewById(R.id.progressDialog);button.setOnClickListener(new OnClickListener(){public void onClick(View v) {showDialog(PROGRESS_DIALOG);}});}protected Dialog onCreateDialog(int id) {switch(id) {case PROGRESS_DIALOG:progressDialog = new ProgressDialog(NotificationTest.this);progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);progressDialog.setMessage("Loading...");progressThread = new ProgressThread(handler);progressThread.start();return progressDialog;default:return null;}} // Define the Handler that receives messages from the thread and update the progressfinal Handler handler = new Handler() {public void handleMessage(Message msg) {int total = msg.getData().getInt("total");progressDialog.setProgress(total);if (total >= 100){dismissDialog(PROGRESS_DIALOG);progressThread.setState(ProgressThread.STATE_DONE);}}}; /** Nested class that performs progress calculations (counting) */private class ProgressThread extends Thread {Handler mHandler;final static int STATE_DONE = 0;final static int STATE_RUNNING = 1;int mState;int total;ProgressThread(Handler h) {mHandler = h;}public void run() {mState = STATE_RUNNING;   total = 0;while (mState == STATE_RUNNING) {try {Thread.sleep(100);} catch (InterruptedException e) {Log.e("ERROR", "Thread Interrupted");}Message msg = mHandler.obtainMessage();Bundle b = new Bundle();b.putInt("total", total);msg.setData(b);mHandler.sendMessage(msg);total++;}}/* sets the current state for the thread,* used to stop the thread */public void setState(int state) {mState = state;}}
}
Creating a Custom Dialog 创建自定义对话框
如果你想自定义一个对话框,你可以使用布局元素来创造你的对话框的布局。定义好布局后,将根View对象或者布局资源ID传给setContentView(View).
例如,创建如图所示的对话框:
创建一个xml布局custom_dialog.xml:
http://schemas.android.com/apk/res/android"android:id="@+id/layout_root"android:orientation="horizontal"android:layout_width="fill_parent"android:layout_height="fill_parent"android:padding="10dp">android:layout_width="wrap_content"android:layout_height="fill_parent"android:layout_marginRight="10dp"/>android:layout_width="wrap_content"android:layout_height="fill_parent"android:textColor="#FFF"/>

该xml定义了一个LinearLayout中的一个ImageView 和一个TextView。
将以上布局设为对话框的content view,并且定义ImageView 和 TextView的内容:

Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.android);

在初始化Dialog之后,使用setContentView(int),将布局资源id传给它。现在Dialog有一个定义好的布局,你可以使用findViewById(int)来找到该元素的id并修改它的内容。
使用前面所讲的方法显示对话框。
一个使用Dialog类建立的对话框必须有一个标题。如果你不调用setTitle(),那么标题区域会保留空白。如果你不希望有一个标题,那么你应该使用AlertDialog类来创建自定义对话框。然而,由于一个AlertDialog使用AlertDialog.Builder类来建立最方便,所以你没有方法使用setContentView(int),而是只能使用setView(View)。该方法接受一个View对象,所以你需要从xml中展开你的根View。
要展开一个xml布局,使用 getLayoutInflater() (或 getSystemService())取得LayoutInflater,然后调用inflate(int, ViewGroup),第一个参数为布局id,而第二个参数为根view的id。现在,你可以使用展开后的布局来找到View对象并定义ImageView和TextView元素的内容。然后实例化AlertDialog.Builder并使用setView(View)来为对话框设置展开后的布局。例如:

AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,(ViewGroup) findViewById(R.id.layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
使用AlertDialog来自定义对话框,可以利用其内置特性例如按钮、选择列表、标题、图标。

Android Dialog详解相关推荐

  1. android dialog 详解,详解android dialog自定义实例

    现在很多应用程序的提示对话框都很有个性的,如果你使用的对话框样子还是传统的对话框,是不是有点LOST了呢?今天爱站技术频道小编就给大家讲讲详解android dialog自定义实例. 第一个需求:简单 ...

  2. Android Preference详解之初识Preference及Preference系(一)

    Android Preference详解之初识Preference及Preference系(一) 转载:https://blog.csdn.net/google_huchun/article/deta ...

  3. 【转】Android菜单详解——理解android中的Menu--不错

    原文网址:http://www.cnblogs.com/qingblog/archive/2012/06/08/2541709.html 前言 今天看了pro android 3中menu这一章,对A ...

  4. Android菜单详解——理解android中的Menu

    前言 今天看了pro android 3中menu这一章,对Android的整个menu体系有了进一步的了解,故整理下笔记与大家分享. PS:强烈推荐<Pro Android 3>,是我至 ...

  5. Android LayoutInflater详解

    Android LayoutInflater详解 在实际开发中LayoutInflater这个类还是非常有用的,它的作用类 似于findViewById().不同点是LayoutInflater是用来 ...

  6. android Fragments详解

    android Fragments详解一:概述 android Fragments详解二:创建Fragment 转载于:https://my.oschina.net/liangzhenghui/blo ...

  7. android WebView详解,常见漏洞详解和安全源码(下)

    上篇博客主要分析了 WebView 的详细使用,这篇来分析 WebView 的常见漏洞和使用的坑.  上篇:android WebView详解,常见漏洞详解和安全源码(上)  转载请注明出处:http ...

  8. android WebView详解,常见漏洞详解和安全源码(上)

    这篇博客主要来介绍 WebView 的相关使用方法,常见的几个漏洞,开发中可能遇到的坑和最后解决相应漏洞的源码,以及针对该源码的解析.  由于博客内容长度,这次将分为上下两篇,上篇详解 WebView ...

  9. android子视图无菜单,Android 菜单详解

    Android中菜单分为三种,选项菜单(OptionMenu),上下文菜单(ContextMenu),子菜单(SubMenu) 选项菜单 可以通过两种办法增加选项菜单,一是在menu.xml中添加,该 ...

  10. Android StateFlow详解

    转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/121913352 本文出自[赵彦军的博客] 文章目录 系列文章 一.冷流还是热流 S ...

最新文章

  1. 最新3D目标检测文章汇总(包含ECCV20和ACMMM20)
  2. 前端CSS基础7.8
  3. 7、Docker容器数据卷volumes-from
  4. python paramiko长连接_【Python】 SSH连接的paramiko
  5. 17-取石子-hdu1846(巴什博奕)
  6. php中不让数组初始化,javascript中数组与php数组初始化差异
  7. 分布科技荣登海南省实施区块链应用示范揭榜工程名单
  8. 算法导论 中+英 PDF分享 附书单
  9. 高德地图哪个语音包最好_高德语音导航怎么呼叫?高德地图语音包怎么设置
  10. android 转音频格式,android_Lame转换音频格式
  11. 思考与总结:数据分片的一些问题
  12. SM2 SM3 SM4 国密版本,基于bouncycastle 实现
  13. windows11安装wsa安卓子系统
  14. 记一次排查服务器被挖矿记录
  15. Apache Druid 总结
  16. python timer详解_python线程定时器Timer实现原理解析
  17. php大商创 安装,【大商创使用教程】大商创2.0数据库配置及安装教程
  18. 经典白话算法之桶排序
  19. java毕业设计超市进销存管理系统Mybatis+系统+数据库+调试部署
  20. 摇杆芯片速度测试软件,摇杆控制小车+L9110芯片

热门文章

  1. html5页面签字,html5 canvas实现的手机端签字板
  2. hal 外部唤醒脚唤醒_探索者 STM32F407 开发板资料连载第二十二章 待机唤醒实验...
  3. 哈弗h2s车内时间怎么调_你想拥抱的品质生活,就让哈弗H4乐享版帮你实现吧
  4. Vue.use 写多个_西双版纳能写立项报告收费公司
  5. 徐州计算机专业技校,2021徐州所有的中专技校职高排名
  6. arma找不到合适的模型_愉悦宝贝丨投资水育早教找不到合适品牌?那是因为你没遇到愉悦宝贝!...
  7. 图标X轴this显示值自定义
  8. 帆软JS获取控件扩展的值的两种方法
  9. linux安装python的拓展包,linux 安装python拓展包pexpect
  10. SQL Server之旅:(二)Could not load the DLL xpstar90.dll