Android 自定义ProgressDialog进度条对话框用法详解

所属分类: 软件编程 / Android 阅读数: 1725
收藏 0 赞 0 分享

ProgressDialog的基本用法

ProgressDialog为进度对话框。android手机自带的对话框显得比较单一,我们可以通过ProgressDialog来自己定义对话框中将要显示出什么东西。

首先看看progressDialog里面的方法

setProgressStyle:设置进度条风格,风格为圆形,旋转的。

setTitlt:设置标题
setMessage:设置提示信息;
setIcon:设置标题图标;
setIndeterminate:设置ProgressDialog 的进度条是否不明确;这个属性对于ProgressDailog默认的转轮模式没有实际意义,默认下设置为true,它仅仅对带有ProgressBar的Dialog有作用。修改这个属性为false后可以实时更新进度条的进度。

setCancelable:设置ProgressDialog 是否可以按返回键取消;
CancelListner:当前Dialog强制取消之后将会被执行,通常用来清理未完成的任务。

setButton:设置ProgressDialog 的一个Button(需要监听Button事件);

show:显示ProgressDialog。

cancel:删除progressdialog

dismiss: 删除progressdialog 作用和cancel相同

setProgress(intCounter);更新进度条,当然一般都需要Handler的结合来更新进度条
<!--EndFragment-->

然后我们看看效果

实现代码如下

<span style="">package cn.android;
import android.app.Activity;
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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class SecondActivity extends Activity implements Runnable{
/**
* Called when the activity is first created.
* Activity入口
* */
private Button b_dialog,b_dialog1,button;//两个按钮
private ProgressDialog pd,pd1;//进度条对话框
private int count;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);//关联对应的界面
b_dialog = (Button)this.findViewById(R.id.button_dialog);
b_dialog1 = (Button)this.findViewById(R.id.Button_dialog1);
//处理事件发生时要做的事
b_dialog.setOnClickListener(listener);
b_dialog1.setOnClickListener(listener);
</span>
<span style=""> }
//定义监听器对象
private OnClickListener listener = new OnClickListener() {
// 鼠标按下后
public void onClick(View v) {
// 得到当前被触发的事件的ID —— 类型是int
int id = v.getId();
if(id == R.id.button_dialog){
//按下确定键就会消失的进程对话框
// pd = new ProgressDialog(SecondActivity.this);// 创建ProgressDialog对象
// pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置进度条风格,风格为圆形,旋转的
// pd.setTitle("提示");// 设置ProgressDialog 标题
// pd.setMessage("这是一个圆形进度条对话框");// 设置ProgressDialog提示信息
// pd.setIcon(R.drawable.icon);// 设置ProgressDialog标题图标
// // 设置ProgressDialog 的进度条是否不明确 false 就是不设置为不明确
// pd.setIndeterminate(false);
// pd.setCancelable(true); // 设置ProgressDialog 是否可以按退回键取消
// pd.setButton("确定", new Bt1DialogListener()); // 设置ProgressDialog 的一个Button
// pd.show(); // 让ProgressDialog显示
//过1秒钟就会自己消失的进程对话框
//弹出另外一种对话框
pd = ProgressDialog.show(SecondActivity.this, "自动关闭对话框", "Working,,,,,,1秒", true, false);
Thread thread = new Thread(SecondActivity.this);//开启一个线程来延时
thread.start();//启动线程
}else if(id == R.id.Button_dialog1){
pd1 = new ProgressDialog(SecondActivity.this);// 创建ProgressDialog对象
pd1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 设置进度条风格,风格为圆形,旋转的
pd1.setTitle("提示");// 设置ProgressDialog 标题
pd1.setMessage("这是一个条状进度条对话框");// 设置ProgressDialog提示信息
pd1.setIcon(R.drawable.secondback);// 设置ProgressDialog标题图标
// 设置ProgressDialog 的进度条是否不明确 false 就是不设置为不明确
pd1.setIndeterminate(false);
pd1.setCancelable(true); // 设置ProgressDialog 是否可以按退回键取消
pd1.setProgress(100);// 设置ProgressDialog 进度条进度
pd1.show(); // 让ProgressDialog显示
count = 0;
new Thread() {
public void run() {
try {
while(count <= 100) {
// 由线程来控制进度
pd1.setProgress(count++);
Thread.sleep(100);
}
pd1.cancel();
} catch (Exception e) {
pd1.cancel();
}
}
}.start();
}
}
};
//run的是实现
public void run() {
try {
Thread.sleep(1000);//睡1秒
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.sendEmptyMessage(0);//传送消息
}
//定义处理消息的对象
private Handler handler = new Handler(){
//加入传消息来了就这么么办
public void handleMessage(Message msg){
pd.dismiss();//对话框消失
Toast.makeText(SecondActivity.this, "对话框就消失了", 3).show();
}
};
// pdButton01的监听器类
class Bt1DialogListener implements DialogInterface.OnClickListener {
@Override
public void onClick(DialogInterface dialog, int which) {
// 点击“确定”按钮取消对话框
dialog.cancel();
}
}
}
</span>

更多精彩内容其他人还在看

Android中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多