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

 

创建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. iOS之两个ImageView实现图片滚动

    原创作者:codingZero 导语 在不少项目中,都会有图片轮播这个功能,现在网上关于图片轮播的框架层出不穷,千奇百怪,笔者根据自己的思路,用两个imageView也实现了图片轮播,这里说说笔者的主 ...

  2. Android项目实战(二十四):项目包成jar文件,并且将工程中引用的jar一起打入新的jar文件中

    前言: 关于.jar文件: 平时我们Android项目开发中经常会用到第三方的.jar文件. 其实.jar文件就是一个类似.zip文件的压缩包,里面包含了一些源代码,注意的是.jar不包含资源文件(r ...

  3. Android framework编译出来的jar包classes.jar的位置

    在源码环境下编译 Android framework编译出来的jar包classes.jar的位置  out/target/common/obj/JAVA_LIBRARIES/framework_in ...

  4. JavaScript的个人学习随手记(二)

    JS HTML DOM 改变 HTML 输出流 JavaScript 能够创建动态的 HTML 内容: 今天的日期是: Sat Sep 24 2016 15:06:50 GMT+0800 (中国标准时 ...

  5. regsvr32命令

    regsvr32是Windows操作系统命令,用来注册及反注册DLL文件和ActiveX文件. 1.  使用示例 regsvr32  foo.dll    // 注册foo.dll文件到Windows ...

  6. Ignite China 2015 之行

    微软首届Ignite China选择了金秋十月的北京,在顺义的九华山庄举办.这几天北京的空气特别好,再加上郊区高楼少,令人心胸开阔了不少.这次Ignite之行的任务有两个,其一是27号晚上与Windo ...

  7. Linux MySQL源码安装缺少ncurses-devel包

    在Red Hat Enterprise Linux Server release 5.7 上用源码安装MySQL-5.6.23时,遇到了" remove CMakeCache.txt and ...

  8. SQL SERVER中关于OR会导致索引扫描或全表扫描的浅析

    在SQL SERVER的查询语句中使用OR是否会导致不走索引查找(Index Seek)或索引失效(堆表走全表扫描 (Table Scan).聚集索引表走聚集索引扫描(Clustered Index ...

  9. asp.net signalR 专题—— 第三篇 如何从外部线程访问 PersistentConnection

    在前面的两篇文章中,我们讲到的都是如何将消息从server推向client,又或者是client再推向server,貌似这样的逻辑没什么异常,但是放在真实 的环境中,你会很快发现有一个新需求,如何根据 ...

  10. eAccelerator、memcached、xcache、APC 等四个加速扩展的区别

    折腾VPS的朋友,在安装好LNMP等Web运行环境后都会选择一些缓存扩展安装以提高PHP运行速度,常被人介绍的有eAccelerator.memcached.xcache.Alternative PH ...