使用对话框 —— Dialog
对话框就是一般的弹出窗口,主要用来提示用户,和用户交互。
创建Activity对话框
使用Activity模拟对话框。这个比较简单,主要是使用Activity自带的Dialog主题。
创建DialogActivity,并在AndroidManifest中注册。
改变DialogActivity的主题:
<activity
android:theme="@android:style/Theme.Dialog"
android:name="com.whathecode.usingdialog.DialogActivity"
android:label="@string/title_activity_dialog" >
</activity>
DialogActivity代码示例:
package com.whathecode.usingdialog; import android.app.Activity;
import android.os.Bundle;
import android.view.View; public class DialogActivity extends Activity
{ @Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
} //用来关闭这个Activity
public void close(View view)
{
finish();
}
}
DialogActivity布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:orientation="vertical"
android:gravity="center_vertical|center_horizontal"
tools:context=".DialogActivity" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是基于Activity的Dialog" /> <LinearLayout
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"> <Button
android:id="@+id/confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定"
android:onClick="close"/> <Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消"
android:onClick="close"/>
</LinearLayout> </LinearLayout>
MainActivity代码:
package com.whathecode.usingdialog; import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View; public class MainActivity extends FragmentActivity
{ @Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} public void openActivityDialog(View view)
{
Intent intent = new Intent(this, DialogActivity.class);
startActivity(intent);
}
}
运行效果:

创建单选,多选和带进度条的对话框:
主要是使用AlertDialog类,首先通过创建AlertDialog类的实例,然后使用showDialog显示对话框。
showDialog方法的执行会引发onCreateDialog方法被调用
示例代码:
package com.whathecode.usingdialog; import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Toast; public class MainActivity extends Activity
{ private static final int SINGLE_CHOICE_DIALOG = 0;
private static final int MULTI_CHOICE_DIALOG = 1;
private static final int PROGRESS_DIALOG = 2;
protected static final int MAX_PROGRESS = 30;
private CharSequence items[] = new String[] { "apple", "google",
"microsoft" };
private boolean checkedItems[] = new boolean[3]; private Handler progressHandler;
private int progress;
protected ProgressDialog progressDialog; @Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); progressHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (progress >= MAX_PROGRESS) {
progressDialog.dismiss(); //关闭progressDialog
} else {
progress++; //进度条加1
progressDialog.incrementProgressBy(1);
//只要当前进度小于总进度,每个100毫秒发送一次消息
progressHandler.sendEmptyMessageDelayed(0, 100);
}
}
};
} public void openActivityDialog(View view)
{
Intent intent = new Intent(this, DialogActivity.class);
startActivity(intent);
} //显示单选对话框
public void openSinglechoiceDialog(View view)
{
showDialog(SINGLE_CHOICE_DIALOG);
} //显示多选对话框
public void openMultichoiceDialog(View view)
{
showDialog(MULTI_CHOICE_DIALOG);
} //显示进度条对话框
public void openProgressDialog(View view)
{
showDialog(PROGRESS_DIALOG);
progress = 0;
progressDialog.setProgress(0);
progressHandler.sendEmptyMessage(0);
} @Override
@Deprecated
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case SINGLE_CHOICE_DIALOG:
return createSingleChoiceDialog(); case MULTI_CHOICE_DIALOG:
return createMultichoiceDialog(); case PROGRESS_DIALOG:
return createProgressDialog(); default:
break;
}
return null;
} /**
* 创建单选对话框
*
*/
public Dialog createSingleChoiceDialog()
{
return new AlertDialog.Builder(this)
.setTitle("单选对话框") //设置对话框标题
.setNegativeButton("取消", null) //设置取消按钮钮
.setPositiveButton("确定", null) //设置确定按
.setSingleChoiceItems(items, 0, //绑定数据
new DialogInterface.OnClickListener()
{ @Override
public void onClick(DialogInterface dialog,
int which)
{
Toast.makeText(getBaseContext(),
items[which].toString(),
Toast.LENGTH_SHORT).show();
}
}).create();
} /**
* 创建多选对话框
*
*/
public Dialog createMultichoiceDialog()
{
return new AlertDialog.Builder(this)
.setTitle("多选对话框") //设置对话框标题
.setNegativeButton("取消", null) //设置取消按钮
.setPositiveButton("确定", null) //设置确定按钮
.setMultiChoiceItems(items, checkedItems, //绑定数据
new DialogInterface.OnMultiChoiceClickListener()
{ @Override
public void onClick(DialogInterface dialog,
int which, boolean isChecked)
{
Toast.makeText(
getBaseContext(),
isChecked ? items[which] + " check"
: items[which] + " uncheck",
Toast.LENGTH_SHORT).show();
}
}).create();
} /**
* 创建带进度条的对话框
*
*/
public Dialog createProgressDialog()
{
progressDialog = new ProgressDialog(this);
progressDialog.setTitle("下载对话框");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(MAX_PROGRESS);
progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener()
{ @Override
public void onClick(DialogInterface dialog, int which)
{ }
});
progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener()
{ @Override
public void onClick(DialogInterface dialog, int which)
{ }
}); return progressDialog;
}
}
运行效果:

这里比较难理解还是ProgressDialog,因为它需要增加进度。这里我们通过向Activity线程发送消息,
从而能够使用progressDialog.incrementProgressBy(1)方法递增进度条。
使用对话框 —— Dialog的更多相关文章
- Android 对话框(Dialog)大全 建立你自己的对话框
Android 对话框(Dialog)大全 建立你自己的对话框 原文地址: http://www.cnblogs.com/salam/archive/2010/11/15/1877512.html A ...
- 转 Android 对话框(Dialog)大全 建立你自己的对话框
Activities提供了一种方便管理的创建.保存.回复的对话框机制,例如 onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog( ...
- 95秀-自定义对话框 dialog 合集
普通的确认对话框 NormalDialog.java import android.app.Dialog; import android.content.Context; import android ...
- Android 常用对话框Dialog封装
Android 6种 常用对话框Dialog封装 包括: 消息对话框.警示(含确认.取消)对话框.单选对话框. 复选对话框.列表对话框.自定义视图(含确认.取消)对话框 分别如下图所示: ...
- Android项目实战(三十二):圆角对话框Dialog
前言: 项目中多处用到对话框,用系统对话框太难看,就自己写一个自定义对话框. 对话框包括:1.圆角 2.app图标 , 提示文本,关闭对话框的"确定"按钮 难点:1.对话框边框圆角 ...
- Android 对话框(Dialog)大全
转自: http://www.cnblogs.com/salam/archive/2010/11/15/1877512.html Activities提供了一种方便管理的创建.保存.回复的对话框机制, ...
- Android 对话框(Dialog)
Activities提供了一种方便管理的创建.保存.回复的对话框机制,例如 onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog( ...
- Android 对话框(Dialog) 及 自己定义Dialog
Activities提供了一种方便管理的创建.保存.回复的对话框机制,比如 onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog( ...
- (转载)Android项目实战(三十二):圆角对话框Dialog
Android项目实战(三十二):圆角对话框Dialog 前言: 项目中多处用到对话框,用系统对话框太难看,就自己写一个自定义对话框. 对话框包括:1.圆角 2.app图标 , 提示文本,关闭对话 ...
- android对话框(Dialog)的使用方法
Activities提供了一种方便管理的创建.保存.回复的对话框机制.比如 onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog( ...
随机推荐
- 1-1 gulp 实战
npm install gulp-htmlmin gulp-imagemin imagemin-pngcrush gulp-minify-css gulp-jshint gulp-uglify gul ...
- 关于举办 2015年 Autodesk 助力云应用项目开发活动通知
各位尊敬的Autodesk 合作伙伴,大家好! 相信您在过去的一年里应该对Autodesk最新的云服务技术有所了解,您是不是曾经闪现过一些很好的想法,却由于不确定是否真实可行,或担心没有技术支持来帮助 ...
- iOS之获取屏幕尺寸
//app尺寸,去掉状态栏 CGRect appRect = [UIScreenmainScreen].applicationFrame; NSLog(@"%f, %f, %f,%f&quo ...
- iOS面试题总结 (二)
14 OC的理解和特性 OC作为一个面向对象的语言,他也就具有面向对象的特点-封装,继承,多态. OC是一门动态性的语言,他具有动态绑定,动态加载,动态类型.动态即就是在运行时才会做的一些事情. 动态 ...
- iOS开发new与alloc/init的区别
[className new]基本等同于[[className alloc] init]: 区别只在于alloc分配内存的时候使用了zone. 这个zone是个什么东东呢? 它是给对象分配内存的时候, ...
- 基于Ruby的Watir-WebDriver自动化测试方案
Watir-WebDriver —— 软件测试的自动化时代 QQ群:160409929 自动化测试方案书 系统架构 该自动化测试框架分三个模块:Test用例.Control控制层.Tool ...
- 大数据系列(1)——Hadoop集群坏境搭建配置
前言 关于时下最热的技术潮流,无疑大数据是首当其中最热的一个技术点,关于大数据的概念和方法论铺天盖地的到处宣扬,但其实很多公司或者技术人员也不能详细的讲解其真正的含义或者就没找到能被落地实施的可行性方 ...
- mysql-Federated存储方式,远程表,相当于sql server的linked server
MySQL中针对不同的功能需求提供了不同的存储引擎.所谓的存储引擎也就是MySQL下特定接口的具体实现. FEDERATED是其中一个专门针对远程数据库的实现.一般情况下在本地数据库中建表会在数据库目 ...
- 算法: 斐波那契数列C/C++实现
斐波那契数列: 1,1,2,3,5,8,13,21,34,.... //求斐波那契数列第n项的值 //1,1,2,3,5,8,13,21,34... //1.递归: //缺点:当n过大时,递归 ...
- Linux Shell编程入门
从程序员的角度来看, Shell本身是一种用C语言编写的程序,从用户的角度来看,Shell是用户与Linux操作系统沟通的桥梁.用户既可以输入命令执行,又可以利用 Shell脚本编程,完成更加复杂的操 ...