ProgressDialog进度对话框也是一个用的很广的控件,比如下载、支付、或者游戏里面打怪当前进度是多少了呢?那就需要它弹出来提醒用户了。既然是对话框,如果用户没有关闭它,就不能进行其他操作,先看看sdk里面是怎么说的。
public class ProgressDialog extends AlertDialog
java.lang.Object
↳ android.app.Dialog
↳ android.app.AlertDialog
↳ android.app.ProgressDialog
Class Overview
A dialog showing a progress indicator and an optional text message or view. Only a text message or a view can be used at the same time.
The dialog can be made cancelable on back key press.
The progress range is 0..10000.
从继承关系可以看出它继承成AlertDialog,所以我们可以理解成功ProgressDialog=ProgressBar+AlertDialog。本篇先讨论怎么简单的使用系统自带的ProgressDialog,后面一篇文章将学习,如何编写我们自定义的ProgressDialog,自定义组件是一个研究话题,人家编写SDK的作者也不可能做到面面俱到的满足所有人的需求,不过提供了我们扩展的机会。好,开始我们的实验。
第一步、编写页面progressview.xml
放置了两个按钮
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示圆形进度条" android:id="@+id/btnspinner" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_marginTop="25dp" android:layout_alignParentRight="true"></Button>
<Button android:id="@+id/btnhorizon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示条形进度条" android:layout_below="@+id/btnspinner" android:layout_alignParentLeft="true" android:layout_marginTop="38dp" android:layout_alignParentRight="true"></Button>
</RelativeLayout>
第二步、编写页面Activity ProgressActivity.java
/**
*
*/
package com.figo.helloworld;
import android.app.Activity;
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.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
/**
* @author zhuzhifei
*
*/
public class ProgressActivity extends Activity implements OnClickListener {
Button btnspinner;
Button btnhorizon;
int mCount;
ProgressDialog pd;
String tag="ProgressActivity";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progressview);
btnspinner = (Button) findViewById(R.id.btnspinner);
btnspinner.setOnClickListener(this);
btnhorizon = (Button) findViewById(R.id.btnhorizon);
btnhorizon.setOnClickListener(this);
}
// 按钮事件
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 构建一个下载进度条
switch (v.getId()) {
case R.id.btnspinner:
mCount = 0;
pd = new ProgressDialog(ProgressActivity.this);
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置风格为圆形进度条
pd.setTitle("提示");// 设置标题
pd.setIcon(R.drawable.icon);// 设置图标
pd.setMessage("这是一个圆形进度条");
pd.setIndeterminate(false);// 设置进度条是否为不明确
pd.setButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
pd.show();
MyThread thread1 = new MyThread();
new Thread(thread1).start();
break;
case R.id.btnhorizon:
mCount = 0;
pd = new ProgressDialog(ProgressActivity.this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setTitle("提示");
pd.setIcon(R.drawable.icon);
pd.setMessage("这是一个长型进度条");
pd.setMax(100);
pd.setProgress(0);
pd.setSecondaryProgress(20);
pd.setIndeterminate(false);
pd.setButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
pd.show();
MyThread thread2 = new MyThread();
new Thread(thread2).start();
break;
}
}
// 创建子线程
class MyThread implements Runnable {
public void run() {
try{
while(mCount<=100){
pd.setProgress(mCount++);
Thread.sleep(100);
}
pd.dismiss();
}catch(Exception ex){
pd.dismiss();
}
}
}
}
第三步、AndroidManifest.xml注册我们的Activity
<activity android:name=".ProgressActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
第四步、运行效果
android开发步步为营之26:进度条对话框ProgressDialog的用法
最新推荐文章于 2024-09-01 08:11:42 发布