ProgressDialog使用总结
以前对ProgressDialog不太熟练,特地看到的这个文章
ProgressDialog的使用
ProgressDialog 继承自AlertDialog,AlertDialog继承自Dialog,实现DialogInterface接口。
ProgressDialog的创建方式有两种,一种是new Dialog ,一种是调用Dialog的静态方法Dialog.show()。
- // 方式一:new Dialog
- final ProgressDialog dialog = new ProgressDialog(this);
- dialog.show();
// 方式一:new Dialog
final ProgressDialog dialog = new ProgressDialog(this);
dialog.show();
- // 方式二:使用静态方式创建并显示,这种进度条只能是圆形条,设置title和Message提示内容
- ProgressDialog dialog2 = ProgressDialog.show(this, "提示", "正在登陆中");
// 方式二:使用静态方式创建并显示,这种进度条只能是圆形条,设置title和Message提示内容
ProgressDialog dialog2 = ProgressDialog.show(this, "提示", "正在登陆中");
- // 方式三 使用静态方式创建并显示,这种进度条只能是圆形条,这里最后一个参数boolean indeterminate设置是否是不明确的状态
- ProgressDialog dialog3 = ProgressDialog
- .show(this, "提示", "正在登陆中", false);
// 方式三 使用静态方式创建并显示,这种进度条只能是圆形条,这里最后一个参数boolean indeterminate设置是否是不明确的状态
ProgressDialog dialog3 = ProgressDialog
.show(this, "提示", "正在登陆中", false);
- // 方式四 使用静态方式创建并显示,这种进度条只能是圆形条,这里最后一个参数boolean cancelable 设置是否进度条是可以取消的
- ProgressDialog dialog4 = ProgressDialog.show(this, "提示", "正在登陆中",
- false, true);
// 方式四 使用静态方式创建并显示,这种进度条只能是圆形条,这里最后一个参数boolean cancelable 设置是否进度条是可以取消的
ProgressDialog dialog4 = ProgressDialog.show(this, "提示", "正在登陆中",
false, true);
- // 方式五 使用静态方式创建并显示,这种进度条只能是圆形条,这里最后一个参数 DialogInterface.OnCancelListener
- // cancelListener用于监听进度条被取消
- ProgressDialog dialog5 = ProgressDialog.show(this, "提示", "正在登陆中", true,
- true, cancelListener);
// 方式五 使用静态方式创建并显示,这种进度条只能是圆形条,这里最后一个参数 DialogInterface.OnCancelListener
// cancelListener用于监听进度条被取消
ProgressDialog dialog5 = ProgressDialog.show(this, "提示", "正在登陆中", true,
true, cancelListener);
方式五中需要一个cancelListener,代码如下;
- private OnCancelListener cancelListener = new OnCancelListener() {
- @Override
- public void onCancel(DialogInterface dialog) {
- // TODO Auto-generated method stub
- Toast.makeText(MainActivity.this, "进度条被取消", Toast.LENGTH_LONG)
- .show();
- }
- };
private OnCancelListener cancelListener = new OnCancelListener() { @Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "进度条被取消", Toast.LENGTH_LONG)
.show(); }
};
ProgressDialog的样式有两种,一种是圆形不明确状态,一种是水平进度条状态
第一种方式:圆形进度条
- final ProgressDialog dialog = new ProgressDialog(this);
- dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置进度条的形式为圆形转动的进度条
- dialog.setCancelable(true);// 设置是否可以通过点击Back键取消
- dialog.setCanceledOnTouchOutside(false);// 设置在点击Dialog外是否取消Dialog进度条
- dialog.setIcon(R.drawable.ic_launcher);//
- // 设置提示的title的图标,默认是没有的,如果没有设置title的话只设置Icon是不会显示图标的
- dialog.setTitle("提示");
- // dismiss监听
- dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
- @Override
- public void onDismiss(DialogInterface dialog) {
- // TODO Auto-generated method stub
- }
- });
- // 监听Key事件被传递给dialog
- dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
- @Override
- public boolean onKey(DialogInterface dialog, int keyCode,
- KeyEvent event) {
- // TODO Auto-generated method stub
- return false;
- }
- });
- // 监听cancel事件
- dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
- @Override
- public void onCancel(DialogInterface dialog) {
- // TODO Auto-generated method stub
- }
- });
- //设置可点击的按钮,最多有三个(默认情况下)
- dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- }
- });
- dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- }
- });
- dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "中立",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- }
- });
- dialog.setMessage("这是一个圆形进度条");
- dialog.show();
- new Thread(new Runnable() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- try {
- Thread.sleep(5000);
- // cancel和dismiss方法本质都是一样的,都是从屏幕中删除Dialog,唯一的区别是
- // 调用cancel方法会回调DialogInterface.OnCancelListener如果注册的话,dismiss方法不会回掉
- dialog.cancel();
- // dialog.dismiss();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }).start();
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置进度条的形式为圆形转动的进度条
dialog.setCancelable(true);// 设置是否可以通过点击Back键取消
dialog.setCanceledOnTouchOutside(false);// 设置在点击Dialog外是否取消Dialog进度条
dialog.setIcon(R.drawable.ic_launcher);//
// 设置提示的title的图标,默认是没有的,如果没有设置title的话只设置Icon是不会显示图标的
dialog.setTitle("提示");
// dismiss监听
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() { @Override
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub }
});
// 监听Key事件被传递给dialog
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() { @Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
// TODO Auto-generated method stub
return false;
}
});
// 监听cancel事件
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub }
});
//设置可点击的按钮,最多有三个(默认情况下)
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub }
});
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub }
});
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "中立",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub }
});
dialog.setMessage("这是一个圆形进度条");
dialog.show();
new Thread(new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(5000);
// cancel和dismiss方法本质都是一样的,都是从屏幕中删除Dialog,唯一的区别是
// 调用cancel方法会回调DialogInterface.OnCancelListener如果注册的话,dismiss方法不会回掉
dialog.cancel();
// dialog.dismiss();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}).start();
其中通过Thread.sleep(5000)模拟后台操作。
cancel和dismiss方法本质都是一样的,都是从屏幕中删除Dialog,唯一的区别是:调用cancel方法会回调DialogInterface.OnCancelListener如果注册的话,dismiss方法不会回掉。
第二种方式:水平进度条
- // 进度条还有二级进度条的那种形式,这里就不演示了
- final ProgressDialog dialog = new ProgressDialog(this);
- dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 设置水平进度条
- dialog.setCancelable(true);// 设置是否可以通过点击Back键取消
- dialog.setCanceledOnTouchOutside(false);// 设置在点击Dialog外是否取消Dialog进度条
- dialog.setIcon(R.drawable.ic_launcher);// 设置提示的title的图标,默认是没有的
- dialog.setTitle("提示");
- dialog.setMax(100);
- dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- }
- });
- dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- }
- });
- dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "中立",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- }
- });
- dialog.setMessage("这是一个水平进度条");
- dialog.show();
- new Thread(new Runnable() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- int i = 0;
- while (i < 100) {
- try {
- Thread.sleep(200);
- // 更新进度条的进度,可以在子线程中更新进度条进度
- dialog.incrementProgressBy(1);
- // dialog.incrementSecondaryProgressBy(10)//二级进度条更新方式
- i++;
- } catch (Exception e) {
- // TODO: handle exception
- }
- }
- // 在进度条走完时删除Dialog
- dialog.dismiss();
- }
- }).start();
// 进度条还有二级进度条的那种形式,这里就不演示了
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 设置水平进度条
dialog.setCancelable(true);// 设置是否可以通过点击Back键取消
dialog.setCanceledOnTouchOutside(false);// 设置在点击Dialog外是否取消Dialog进度条
dialog.setIcon(R.drawable.ic_launcher);// 设置提示的title的图标,默认是没有的
dialog.setTitle("提示");
dialog.setMax(100);
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub }
});
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub }
});
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "中立",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub }
});
dialog.setMessage("这是一个水平进度条");
dialog.show();
new Thread(new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
int i = 0;
while (i < 100) {
try {
Thread.sleep(200);
// 更新进度条的进度,可以在子线程中更新进度条进度
dialog.incrementProgressBy(1);
// dialog.incrementSecondaryProgressBy(10)//二级进度条更新方式
i++; } catch (Exception e) {
// TODO: handle exception
}
}
// 在进度条走完时删除Dialog
dialog.dismiss(); }
}).start();
更多的是使用自定义的ProgressDialog,以满足不同显示的需要。
ProgressDialog使用总结的更多相关文章
- Android 之 ProgressDialog用法介绍
布局文件测试: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" androi ...
- Android中通过线程实现更新ProgressDialog(对话进度条)
作为开发者我们需要经常站在用户角度考虑问题,比如在应用商城下载软件时,当用户点击下载按钮,则会有下载进度提示页面出现,现在我们通过线程休眠的方式模拟下载进度更新的演示,如图(这里为了截图方便设置对话进 ...
- Android 屏幕旋转 处理 AsyncTask 和 ProgressDialog 的最佳方案
的最佳方案 标签: Android屏幕旋转AsyncTaskProgressDialog 2014-07-19 09:25 39227人阅读 评论(46) 收藏 举报 分类: [android 进阶之 ...
- 进度条ProgressDialog
1.效果图 public void click(View view) { final ProgressDialog pdDialog = new ProgressDialog(this); //设置标 ...
- 设立点击ProgressDialog外的区域对话框不消失
设置点击ProgressDialog外的区域对话框不消失ProgressDialog mpDialog = new ProgressDialog(OrderTable.this); mpDialog ...
- android ProgressDialog 正在载...Loading...
final ProgressDialog pd = new ProgressDialog(mContext); pd.setMessage("正在加载..."); pd.show( ...
- ProgressDialog的使用
ProgressDialog 继承自AlertDialog,AlertDialog继承自Dialog,实现DialogInterface接口. ProgressDialog的创建方式有两种,一种是ne ...
- Dialog , ProgressDialog , PopWindow 区别
本质区别: Dialog:非阻塞对话框,弹出对话框时时,后台还可以做事情,点击背景时,对话框消失 ProgressDialog:带有圆形进度或者条形进度的对话框,一般结合handler使用.任务完成后 ...
- Android之alertDialog、ProgressDialog
一.alertDialog 置顶于所有控件之上的,可以屏蔽其他控件的交互能力.通过AlertDialog.Builder创建一个AlertDialog,并通过setTittle(),setMesseg ...
随机推荐
- Struts学习总结-02 类型转换
一 类型转换 input.jsp <%@ page language="java" import="java.util.*" pageEncoding=& ...
- linux系统性能监视命令
preface as a linux engineer,you should know how to use these command of monitor system,so let's lear ...
- 【原】jquery图片预览
平时我们在做图片上传的时候,如果可以让用户选择图片的时候,看到图片的效果,那这样用户体验会好很多,因为用户可以就可以决定是否继续用这张图片,尤其是和ajaxuploadfile结合使用的时候,图片的预 ...
- python学习笔记-(三)条件判断和循环
1.条件判断语句 Python中条件选择语句的关键字为:if .elif .else这三个.其基本形式如下: age_of_cc = 27 age = int(input("guessage ...
- c#正则表达式2
System.Text.RegularExpressions.Regex ___rx = new System.Text.RegularExpressions.Regex(@""& ...
- mysql_query()与mysql_real_query()
mysql_query() cannot be used ” character, which mysql_query() interprets as the end of the statement ...
- Visual Studio无法查找或打开 PDB 文件解决办法
Visual Studio无法查找或打开 PDB 文件解决办法 用VS调试程序时,有时会在VS底部的“输出”框中提示“无法查找或打开 PDB 文件”.这该怎么解决呢? 下面,我们以VS2013为例,来 ...
- Redis总结(三)Redis 的主从复制
接着上一篇,前面两篇我总结了<Redis总结(一)Redis安装>和<Redis总结(二)C#中如何使用redis> 所以这一篇,会讲讲Redis 的主从复制以及C#中如何调用 ...
- centos 7.0 ssh 登陆
CentOS 7.0 最小化安装 默认连接 默认端口是22 安装完查看IP 命令 ip addr ip: 192.168.1.103 自己家里练习的机子 都没改配置了 使用putty 远程连接 下载页 ...
- 在Centos上安装RabbitMQ流程(转)
在Centos上安装RabbitMQ流程------------------------ 1. 需求 由于项目中要用到消息队列,经过ActiveMQ与RabbitMQ的比较,最终选择了RabbbitM ...