对话框就是一般的弹出窗口,主要用来提示用户,和用户交互。

 

创建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的更多相关文章

  1. Android 对话框(Dialog)大全 建立你自己的对话框

    Android 对话框(Dialog)大全 建立你自己的对话框 原文地址: http://www.cnblogs.com/salam/archive/2010/11/15/1877512.html A ...

  2. 转 Android 对话框(Dialog)大全 建立你自己的对话框

    Activities提供了一种方便管理的创建.保存.回复的对话框机制,例如 onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog( ...

  3. 95秀-自定义对话框 dialog 合集

    普通的确认对话框 NormalDialog.java import android.app.Dialog; import android.content.Context; import android ...

  4. Android 常用对话框Dialog封装

    Android 6种 常用对话框Dialog封装 包括: 消息对话框.警示(含确认.取消)对话框.单选对话框. 复选对话框.列表对话框.自定义视图(含确认.取消)对话框 分别如下图所示:       ...

  5. Android项目实战(三十二):圆角对话框Dialog

    前言: 项目中多处用到对话框,用系统对话框太难看,就自己写一个自定义对话框. 对话框包括:1.圆角 2.app图标 , 提示文本,关闭对话框的"确定"按钮 难点:1.对话框边框圆角 ...

  6. Android 对话框(Dialog)大全

    转自: http://www.cnblogs.com/salam/archive/2010/11/15/1877512.html Activities提供了一种方便管理的创建.保存.回复的对话框机制, ...

  7. Android 对话框(Dialog)

    Activities提供了一种方便管理的创建.保存.回复的对话框机制,例如 onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog( ...

  8. Android 对话框(Dialog) 及 自己定义Dialog

    Activities提供了一种方便管理的创建.保存.回复的对话框机制,比如 onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog( ...

  9. (转载)Android项目实战(三十二):圆角对话框Dialog

    Android项目实战(三十二):圆角对话框Dialog   前言: 项目中多处用到对话框,用系统对话框太难看,就自己写一个自定义对话框. 对话框包括:1.圆角 2.app图标 , 提示文本,关闭对话 ...

  10. android对话框(Dialog)的使用方法

    Activities提供了一种方便管理的创建.保存.回复的对话框机制.比如 onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog( ...

随机推荐

  1. Civil 3D API二次开发学习指南

    Civil 3D构建于AutoCAD 和 Map 3D之上,在学习Civil 3D API二次开发之前,您至少需要了解AutoCAD API的二次开发,你可以参考AutoCAD .NET API二次开 ...

  2. Macosx 安装 ionic 成功教程

    一.首先介绍一下ionic ionic是一个用来开发混合手机应用的,开源的,免费的代码库.可以优化html.css和js的性能,构建高效的应用程序,而且还可以用于构建Sass和AngularJS的优化 ...

  3. margin css的外边距

    h2{margin:10px 0;} div{margin:20px 0;} ...... <h2>这是一个标题</h2> <div> <h2>这是又一 ...

  4. 虚拟机备份克隆导致SQL SERVER 出现IO错误案例

      案例环境:   服务器配置: CPU: Intel E5-2690  RAM: 12G   虚拟机 操作系统  : Windows Server 2008 R2 Standard Edtion   ...

  5. Hive 分组问题

    group by 中出现的字段不能再select 后面单独显示,必须配合函数使用 上面中的 ' group by id 总结: Hive不允许直接访问非group by字段: 对于非group by字 ...

  6. [MySQL] Buffer Pool Adaptive Flush

    Buffer Pool Adaptive Flush 在MySQL的帮助文档中Tuning InnoDB Buffer Pool Flushing提到, innodb_adaptive_flushin ...

  7. SQL Server 列存储索引强化

    SQL Server 列存储索引强化 SQL Server 列存储索引强化 1. 概述 2.背景 2.1 索引存储 2.2 缓存和I/O 2.3 Batch处理方式 3 聚集索引 3.1 提高索引创建 ...

  8. 【转载】PHP性能优化干货

    PHP优化对于PHP的优化主要是对php.ini中的相关主要参数进行合理调整和设置,以下我们就来看看php.ini中的一些对性能影响较大的参数应该如何设置. # vi /etc/php.ini (1) ...

  9. .NET应用程序调试—原理、工具、方法

    阅读目录: 1.背景介绍 2.基本原理(Windows调试工具箱..NET调试扩展SOS.DLL.SOSEX.DLL) 2.1.Windows调试工具箱 2.2..NET调试扩展包,SOS.DLL.S ...

  10. MySQL插入语句解析

    1.INSERT INTO 最常用简单的插入语句,可以有以下两种用法 1>  INSERT INTO tb_user(id, name, age) VALUES (100022, 'Tom', ...