使用对话框 —— 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( ...
随机推荐
- iOS 学习 - 21 系统自带解析 XML
准备工作: new -> file -> other -> Empty ,在 Save As: 中随便起个名字后缀为 .xml 拷贝下面 <person> <stu ...
- iOS开发之功能模块--高仿Boss直聘的IM界面交互功能
本人公司项目属于社交类,高仿Boss直聘早期的版本,现在Boss直聘界面风格,交互风格都不如Boss直聘以前版本的好看. 本人通过iPhone模拟器和本人真机对聊,将完成的交互功能通过Mac截屏模拟器 ...
- Python语言Web开发框架web2py
python开发的强大的网络框架web2py,这个框架需要下载和集成. http://www.web2py.com/examples/static/web2py_src.zip
- Eclipse部署项目的原理简介eclipse,wtpwebapps,tomcat
转载请注明出处! http://www.cnblogs.com/libingbin/ 感谢您的阅读.如果文章对您有用,那么请轻轻点个赞,以资鼓励.
- MYSQL服务器my.cnf配置文档详解
MYSQL服务器my.cnf配置文档详解 硬件:内存16G [client] port = 3306 socket = /data/3306/mysql.sock [mysql] no-auto-re ...
- .NET应用架构设计—适当使用活动记录模式代替领域模型模式
阅读目录: 1.背景介绍 2.简单介绍领域模型模式.活动记录模式 3.活动记录模式的简单示例及要点 4.总结 1.背景介绍 对软件开发方法论有兴趣的博友应该发现最近“领域驱动设计”慢慢的被人发现被人实 ...
- PostgreSQL-pg_dump,pg_restore
逻辑备份和psql一样,pg_dump.pg_restore有基本的和数据库连接的参数 -h 目标地址(对应环境变量$PGHOST) -p 连接端口(对应环境变量$PGPORT) -U 连接使用的用户 ...
- Memcached学习笔记
[TOC] 前言 此为学习笔记汇总,如有纰漏之处,还望不吝指出,谢谢. 启动流程 调用settings_init()设定初始化参数 从启动命令中读取参数来设置setting值 设定LIMIT参数 开始 ...
- C++ string类的实现
c++中string类的实现 今天面试被考到了, 全给忘记了!!! //string类的实现 #include <iostream> #include <string.h> ...
- 每天一个linux命令(3):pwd命令
Linux中用 pwd 命令来查看”当前工作目录“的完整路径. 简单得说,每当你在终端进行操作时,你都会有一个当前工作目录. 在不太确定当前位置时,就会使用pwd来判定当前目录在文件系统内的确切位置. ...