- 浏览: 467016 次
- 性别:
- 来自: 深圳
-
文章分类
最新评论
-
wwwjiandan:
我现在遇到的问题是将Activity设置为非全屏,且andro ...
Android中软键盘弹出时底部菜单上移问题 -
zjhdreams:
求下载链接
Android 9Patch (NinePatch) -
wtjandjay:
受教了 .......
从程序员到项目经理 -
mr_zhang2011:
将博客搬至CSDN -
fyc0109:
为什么我传过去的值, 有的都不对呢, 顺序也是一样的.就是获得 ...
Android Parcelable序列化自定义类集合在Activity间传递
转载http://blog.csdn.net/ameyume/article/details/6138488
一个对话框一般是一个出现在当前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 Dialog
- break;
- case DIALOG_GAMEOVER_ID:
- // do the work to define the game over Dialog
- break;
- default:
- dialog = null;
- }
- return 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 增加按钮

- 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();
注意:对每种按钮类型,只能为AlertDialog创建一个。也就是说,一个AlertDialog不能有两个以上的"positive"按钮。这使得可能的按钮数量最多为三个:肯定、否定、中性。这些名字和实际功能没有联系,但是将帮助你记忆它们各做什么事情。

- 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();
Adding checkboxes and radio buttons 增加单选框和复选框

注意: 要在你的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();
Creating a ProgressDialog 创建进度对话框

打开一个进度对话框很简单,只需要调用 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 dialog
- button = (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 progress
- final 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;
- }
- }
- }

例如,创建如图所示的对话框:
创建一个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"
- />
将以上布局设为对话框的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类建立的对话框必须有一个标题。如果你不调用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();
1. Context.public abstract Object getSystemService (String name) :Return the handle to a system-level service by name. The class of the returned object varies by the requested name. 具体参见文档。
2. 2种获得LayoutInflater的方法
(1)通过SystemService获得
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
(2)从给定的context中获取
(3)二者区别:实质是一样的,请看源码
- public static LayoutInflater from(Context context) {
- LayoutInflater LayoutInflater =
- (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- if (LayoutInflater == null) {
- throw new AssertionError("LayoutInflater not found.");
- }
- return LayoutInflater;
- }
3. LayoutInflater.inflate()
将Layout文件转换为View,顾名思义,专门供Layout使用的Inflater。虽然Layout也是View的子类,但在android中如果想将xml中的Layout转换为View放入.java代码中操作,只能通过Inflater,而不能通过findViewById(),这一段描述有误,看如下代码 。看下面文档写的已经很清楚。
- <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <LinearLayout android:id="@+id/placeslist_linearlayout"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical">
- </LinearLayout>
- </ScrollView>
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.placeslist_linearlayout);
linearLayout.addView(place_type_text);
这是可运行的,这上面的xml中,LinearLayout不再是Layout的代表,而只是一个普通的View。
4. findViewById有2中形式
R.layout.xx 是引用res/layout/xx.xml的布局文件(inflate方法),R.id.xx是引用布局文件里面的组件,组件的id是xx...(findViewById方法)。看看R.java配置文件吧,R对文件分类管理,多写几个layout.xml后你会发现,所有的组件id都能用R.id.xx来查看,但是组件不在setContentView()里面的layout中就无法使用,Activity.findViewById()会出现空指针异常 。
(1)Activity中的findViewById()
(2)View中的findViewById()
5.
- LayoutInflater.rar (40.7 KB)
- 下载次数: 45
发表评论
-
android Log.isLoggable方法的使用
2014-06-06 15:35 3016android 动态控制logcat日志开关,通过Log. ... -
View not attached to window manager
2013-01-10 11:55 1086View not attached to window man ... -
代码设置android:icon,android:label
2013-01-05 15:26 8010requestWindowFeature( Window.F ... -
Android项目为一个apk设置多个ICON图标和执行入口
2013-01-05 12:13 3888Android开发中,一个工程对应一个AndroidManif ... -
Intent跳转到系统应用中的拨号界面、联系人界面、短信界面及其他
2012-12-04 12:15 01:调用系统发送短信界面 Uri smsT ... -
Android 源码目录结构详解
2012-11-22 17:20 1500这是Android2.1的源代码的 ... -
如何获取android源代码
2012-07-10 17:50 2746如何获取android源代码 研究 ... -
Android uses-permission大全
2012-06-12 17:33 1082android.permission.ACCESS_CHE ... -
Android 应用程序基础(Application Fundamentals)
2012-05-23 17:42 1291Android 应用程序基础(Application Fun ... -
转Android 音乐频谱实现
2012-02-18 23:35 0最近由于需要实现音乐频谱,所以今天就为大家普及一下。 这里实现 ... -
android VideoView本工程mp4文件
2012-01-09 18:42 2716最近在做一个demo,要求播放视频,记录一下。使用的是Vid ... -
关于android播放mp3与歌词同步问题收集
2012-01-09 17:57 1712关于android播放mp3与歌词同步问题收集,有时间再试。 ... -
android使用MediaPlayer播放音乐文件时遇到的问题
2012-01-09 17:38 18405把mp3文件放在Assets文件夹里,然后用MediaPlay ... -
android资源目录---assets与res/raw的不同
2012-01-05 11:49 2401android资源目录---assets与 ... -
Android 的cpu硬盘 内存 网络设置 系统信息 硬件信息
2012-01-04 10:56 1638转载 http://www.cnmsdn.com/html/2 ... -
使用 Android Compatibility Package 来简化开发工作
2012-01-04 10:12 1442转载 http://www.cnmsdn.com/html/2 ... -
Android 9Patch (NinePatch)
2011-12-26 16:06 45451:介绍 NinePatch图片以*.9.png结尾,和普通 ... -
Windows平台下Android源码的下载
2011-12-21 11:06 1269Windows平台下Android源码的下载 ... -
改变MenuItem默认背景
2011-12-07 19:58 1452@Override public boolean onCre ... -
Android横屏竖屏切换
2011-11-10 18:20 107转载 http://blog.csdn.net/leesido ...
相关推荐
- 对于Android版本兼容性问题,可能需要使用Support Library或AndroidX库来创建Dialog。 总之,Dialog在Android应用中起着至关重要的作用,为用户提供了一种优雅的交互方式。这个资源包“安卓Dialog对话框相关-...
通过阅读和理解这个示例,开发者可以更好地掌握Android Dialog的使用方法。 5. 注意事项: - 在主线程中创建和显示Dialog,否则可能会导致ANR(Application Not Responding)错误。 - 对于长时间运行的任务,应该...
1. **Android Dialog 基础**:首先,你需要对 Android 原生 Dialog 有一定的了解,知道如何创建基本的 AlertDialog 和 ProgressDialog。这是自定义 Dialog 的基础。 2. **自定义视图**:为了实现自定义样式,你需要...
在Android中,我们可以使用`AlertDialog`、`ProgressDialog`等系统内置的Dialog类型,但更多的时候,为了满足特定的设计需求,我们会选择自定义Dialog。 弹幕发送框界面主要包含两个核心部分:输入框和发送按钮。...
描述中提到“各种各样的Dialog,android自带的dialog”,这将涵盖Android SDK提供的不同类型的对话框及其用法。 首先,Android系统提供了几种内置的`Dialog`类型: 1. **AlertDialog**:这是最常见的`Dialog`类型...
Android 系统提供了 AlertDialog、ProgressDialog 和 Dialog 三种基本类型的 Dialog。它们都有预设的样式,但可能无法满足所有设计需求。`android-styled-dialogs` 库就是为了解决这个问题,它允许开发者通过简单的 ...
创建基础Dialog需要使用`AlertDialog.Builder`,设置标题、消息和按钮,然后调用`create()`和`show()`方法。 2. **自定义布局Dialog** 如果系统默认的Dialog样式无法满足需求,可以创建自定义布局。首先,创建XML...
7. **注意废弃**: Android团队在API 26(Android Oreo)中已将ProgressDialog标记为废弃,建议使用`ProgressBar`配合`AlertDialog`或自定义View实现类似功能。这主要是为了提升用户体验,因为ProgressDialog会阻止...
综上所述,"Android课件(4Dialog).zip"应该涵盖了Android Dialog的基本用法、高级应用和最佳实践,对于学习和提升Android应用的用户体验设计有极大的帮助。通过深入学习和实践这些内容,开发者能够更好地掌握如何...
- 使用`AlertDialog.Builder`提供的方法来配置Dialog的各种属性,如标题、消息文本、按钮等。 - 调用`create()`方法创建Dialog对象。 - 最后调用`show()`方法展示Dialog。 2. **常用方法介绍:** - `setTitle...
- 通过查看 `JavaApk源码说明.txt`,我们可以学习库的使用方法和注意事项。 - 使用第三方库时,需要注意兼容性问题,确保库支持的最低 SDK 版本能满足应用的需求。 - 需要注意的是,由于 `dialogplus.zip` 内的...
这个“Android dialog 大合集源码”提供了多种对话框的实现,涵盖了Android编程中可能遇到的各种对话框场景,是开发者学习和实践Dialog使用的一个宝贵资源。 1. **基础对话框**: - `AlertDialog`:Android系统中...
在本文中,我们将深入探讨Android Dialog的基本概念、种类、创建方法以及如何通过提供的"Dialog对话框Demo源码"进行实践学习。 1. **Dialog基本概念** Dialog在Android中是一个浮动窗口,它覆盖在主应用程序界面之...
创建自定义的进度对话框,首先需要继承自`AlertDialog.Builder`或直接使用`ProgressDialog`。`AlertDialog.Builder`允许我们自定义对话框的标题、消息、按钮等元素,而`ProgressDialog`则专门用于显示带有进度条的...
综上所述,"Android Dialog 大合集"可能包括了所有这些方面的示例和代码,开发者可以通过学习和实践来掌握Dialog的使用,提升应用的用户体验。通过理解和运用这些知识点,可以创建出符合用户习惯、交互流畅的对话框...
除了基本的AlertDialog,Android还提供了其他类型的Dialog,如ProgressDialog用于显示进度,DatePickerDialog和TimePickerDialog用于选择日期和时间。例如,创建一个ProgressDialog: ```java ProgressDialog ...
Dialog对话框的使用 (progressDialog、AlertDialog、点击不消失、进度条) 具体可参考我文章:https://blog.csdn.net/qq_28056277/article/details/84591086 【更新-->下载所需积分太高,更改为固定分值了】
Android提供了多种预设的Dialog样式,如AlertDialog、ProgressDialog等,但这些样式可能无法满足所有需求,因此自定义Dialog就显得尤为重要。 要创建一个自定义Dialog,我们需要遵循以下步骤: 1. **创建Dialog...
在Android开发中,对话框(Dialog)是一种非常重要的用户界面元素,它用于向用户显示临时信息或进行交互操作,而不打断应用程序的主要流程。本主题主要关注两种常见的对话框:AlertDialog和ProgressDialog,以及如何...