对于下面的代码虽然为转载内容,但第二个图片的方法已经验证过。

首先需要创建ProgressDialog对象,然后使用线程控制进度条显示,具体源代码:

001 package com.yarin.android.TestOnPDialog;
002  
003 import android.app.Activity;
004 import android.app.ProgressDialog;
005 import android.content.DialogInterface;
006 import android.os.Bundle;
007 import android.view.View;
008 import android.widget.Button;
009  
010 public class PDialog extends Activity
011 {
012     private Button mButton01,mButton02;
013     
014     int m_count = 0;
015     //声明进度条对话框
016     ProgressDialog m_pDialog;
017     
018     @Override
019     public void onCreate(Bundle savedInstanceState)
020     {
021         super.onCreate(savedInstanceState);
022         setContentView(R.layout.main);
023         
024         //得到按钮对象
025         mButton01 = (Button)findViewById(R.id.Button01);
026         mButton02 = (Button)findViewById(R.id.Button02);
027         
028         //设置mButton01的事件监听
029         mButton01.setOnClickListener(new Button.OnClickListener() {
030             @Override
031             public void onClick(View v)
032             {
033                 // TODO Auto-generated method stub
034                 
035                 //创建ProgressDialog对象
036                 m_pDialog = new ProgressDialog(PDialog.this);
037  
038                 // 设置进度条风格,风格为圆形,旋转的
039                 m_pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
040  
041                 // 设置ProgressDialog 标题
042                 m_pDialog.setTitle("提示");
043                 
044                 // 设置ProgressDialog 提示信息
045                 m_pDialog.setMessage("这是一个圆形进度条对话框");
046  
047                 // 设置ProgressDialog 标题图标
048                 m_pDialog.setIcon(R.drawable.img1);
049  
050                 // 设置ProgressDialog 的进度条是否不明确
051                 m_pDialog.setIndeterminate(false);
052                 
053                 // 设置ProgressDialog 是否可以按退回按键取消
054                 m_pDialog.setCancelable(true);
055                 
056                 // 设置ProgressDialog 的一个Button
057                 m_pDialog.setButton("确定", new DialogInterface.OnClickListener() {
058                     public void onClick(DialogInterface dialog, int i)
059                     {
060                         //点击“确定按钮”取消对话框
061                         dialog.cancel();
062                     }
063                 });
064  
065                 // 让ProgressDialog显示
066                 m_pDialog.show();
067             }
068         });
069         
070       //设置mButton02的事件监听
071         mButton02.setOnClickListener(new Button.OnClickListener() {
072             @Override
073             public void onClick(View v)
074             {
075                 // TODO Auto-generated method stub
076                 
077                 m_count = 0;
078                 
079                 // 创建ProgressDialog对象
080                 m_pDialog = new ProgressDialog(PDialog.this);
081                 
082                 // 设置进度条风格,风格为长形
083                 m_pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
084                 
085                 // 设置ProgressDialog 标题
086                 m_pDialog.setTitle("提示");
087                 
088                 // 设置ProgressDialog 提示信息
089                 m_pDialog.setMessage("这是一个长形对话框进度条");
090                 
091                 // 设置ProgressDialog 标题图标
092                 m_pDialog.setIcon(R.drawable.img2);
093                 
094                 // 设置ProgressDialog 进度条进度
095                 m_pDialog.setProgress(100);
096                 
097                 // 设置ProgressDialog 的进度条是否不明确
098                 m_pDialog.setIndeterminate(false);
099                 
100                 // 设置ProgressDialog 是否可以按退回按键取消
101                 m_pDialog.setCancelable(true);
102                 
103                 // 让ProgressDialog显示
104                 m_pDialog.show();
105                 
106                 new Thread()
107                 {
108                     public void run()
109                     {
110                         try
111                         {
112                             while (m_count <= 100)
113                             {
114                                 // 由线程来控制进度。
115                                 m_pDialog.setProgress(m_count++);
116                                 Thread.sleep(100);
117                             }
118                             m_pDialog.cancel();
119                         }
120                         catch (InterruptedException e)
121                         {
122                             m_pDialog.cancel();
123                         }
124                     }
125                 }.start();
126                 
127             }
128         });
129     }
130 }