Android 对话框中的进度条 ProgressDialog

简介:     显然要定义对话框进度条就要用ProgressDialog,首先我们需要创建ProgressDialog对象,当然这里同样使用了线程来控制进度条显示,另外可以使用以下方法来设置ProgressDialog。
    显然要定义对话框进度条就要用ProgressDialog,首先我们需要创建ProgressDialog对象,当然这里同样使用了线程来控制进度条显示,另外可以使用以下方法来设置ProgressDialog。 
  setProgressStyle:设置进度条风格,风格为圆形,旋转的。 
  setTitlt:设置ProgressDialog 标题 
  setMessage:设置ProgressDialog提示信息; 
  setIcon:设置ProgressDialog标题图标; 
  setIndeterminate:设置ProgressDialog 的进度条是否不明确; 
  setCancelable:设置ProgressDialog 是否可以按返回键取消; 
  setButton:设置ProgressDialog 的一个Button(需要监听Button事件); 
  show:显示ProgressDialog。 

  首先还是让我们看看运行效果吧 

img_f7039069b2a2f5933faa1e440782d519.jpg 
img_20450b2291a109e5bd330b4768e28292.jpg 
img_9ecafa9911ed003f4026768655bd490b.jpg 

   下面先来看看布局文件 
<?xml version="1.0" encoding="utf-8"?> 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" 
    /> 
android:id="@+id/Button01" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="圆形进度条"/> 

android:id="@+id/Button02" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="长形进度条"/> 
 

Activity01 
Java代码   收藏代码
  1. package xiaohang.zhimeng;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.ProgressDialog;  
  5. import android.content.DialogInterface;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10.   
  11. public class Activity01 extends Activity {  
  12.   
  13.     private Button xhButton01, xhButton02;  
  14.   
  15.     int xh_count = 0;  
  16.     // 声明进度条对话框  
  17.     ProgressDialog xh_pDialog;  
  18.   
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.main);  
  23.   
  24.         // 得到按钮对象  
  25.         xhButton01 = (Button) findViewById(R.id.Button01);  
  26.         xhButton02 = (Button) findViewById(R.id.Button02);  
  27.   
  28.         // 设置xhButton01的事件监听  
  29.         xhButton01.setOnClickListener(new OnClickListener() {  
  30.             @Override  
  31.             public void onClick(View v) {  
  32.                 // 创建ProgressDialog对象  
  33.                 xh_pDialog = new ProgressDialog(Activity01.this);  
  34.   
  35.                 // 设置进度条风格,风格为圆形,旋转的  
  36.                 xh_pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);  
  37.   
  38.                 // 设置ProgressDialog 标题  
  39.                 xh_pDialog.setTitle("提示");  
  40.   
  41.                 // 设置ProgressDialog提示信息  
  42.                 xh_pDialog.setMessage("这是一个圆形进度条对话框");  
  43.   
  44.                 // 设置ProgressDialog标题图标  
  45.                 xh_pDialog.setIcon(R.drawable.img1);  
  46.   
  47.                 // 设置ProgressDialog 的进度条是否不明确 false 就是不设置为不明确  
  48.                 xh_pDialog.setIndeterminate(false);  
  49.   
  50.                 // 设置ProgressDialog 是否可以按退回键取消  
  51.                 xh_pDialog.setCancelable(true);  
  52.   
  53.                 // 设置ProgressDialog 的一个Button  
  54.                 xh_pDialog.setButton("确定"new Bt1DialogListener());  
  55.   
  56.                 // 让ProgressDialog显示  
  57.                 xh_pDialog.show();  
  58.             }  
  59.   
  60.         });  
  61.   
  62.         // 设置xhButton02的事件监听  
  63.         xhButton02.setOnClickListener(new Button.OnClickListener() {  
  64.   
  65.             @Override  
  66.             public void onClick(View v) {  
  67.   
  68.                 xh_count = 0;  
  69.   
  70.                 // 创建ProgressDialog对象  
  71.                 xh_pDialog = new ProgressDialog(Activity01.this);  
  72.   
  73.                 // 设置进度条风格,风格为圆形,旋转的  
  74.                 xh_pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  75.   
  76.                 // 设置ProgressDialog 标题  
  77.                 xh_pDialog.setTitle("提示");  
  78.   
  79.                 // 设置ProgressDialog提示信息  
  80.                 xh_pDialog.setMessage("这是一个长形进度条对话框");  
  81.   
  82.                 // 设置ProgressDialog标题图标  
  83.                 xh_pDialog.setIcon(R.drawable.img2);  
  84.   
  85.                 // 设置ProgressDialog 的进度条是否不明确 false 就是不设置为不明确  
  86.                 xh_pDialog.setIndeterminate(false);  
  87.   
  88.                 // 设置ProgressDialog 进度条进度  
  89.                 xh_pDialog.setProgress(100);  
  90.   
  91.                 // 设置ProgressDialog 是否可以按退回键取消  
  92.                 xh_pDialog.setCancelable(true);  
  93.   
  94.                 // 让ProgressDialog显示  
  95.                 xh_pDialog.show();  
  96.   
  97.                 new Thread() {  
  98.                     @Override  
  99.                     public void run() {  
  100.                         try {  
  101.                             while (xh_count 100) {  
  102.                                 // 由线程来控制进度  
  103.                                 xh_pDialog.setProgress(xh_count++);  
  104.                                 Thread.sleep(100);  
  105.                             }  
  106.                             xh_pDialog.cancel();  
  107.                         } catch (Exception e) {  
  108.                             xh_pDialog.cancel();  
  109.                         }  
  110.                     }  
  111.                 }.start();  
  112.   
  113.             }  
  114.   
  115.         });  
  116.     }  
  117.   
  118.     // xhButton01的监听器类  
  119.     class Bt1DialogListener implements DialogInterface.OnClickListener {  
  120.         @Override  
  121.         public void onClick(DialogInterface dialog, int which) {  
  122.             // 点击“确定”按钮取消对话框  
  123.             dialog.cancel();  
  124.         }  
  125.     }  
  126. }  

android 2.0  
源码 运行效果图片 见附件
CU博客本地下载见下面:
img_e25d4fb2f8de1caf41a735ec53088516.png49adb25a-e911-30f3-b302-324cf2c5a3aa.rar

http://blog.csdn.net/kaiyang45/article/details/6997131
http://byandby.iteye.com/blog/817214
目录
相关文章
|
23天前
|
XML Java Android开发
Android实现自定义进度条(源码+解析)
Android实现自定义进度条(源码+解析)
51 1
|
4月前
|
Android开发
Android Studio APP开发入门之对话框Dialog的讲解及使用(附源码 包括提醒对话框,日期对话框,时间对话框)
Android Studio APP开发入门之对话框Dialog的讲解及使用(附源码 包括提醒对话框,日期对话框,时间对话框)
34 0
|
4月前
|
XML API Android开发
Android 自定义View 之 圆环进度条
Android 自定义View 之 圆环进度条
|
1月前
|
Android开发
[Android]AlertDialog对话框
[Android]AlertDialog对话框
16 0
|
8月前
|
Android开发
Android 中ProgressDialog进度条对话框的使用(使用子线程模拟更新进度)
Android 中ProgressDialog进度条对话框的使用(使用子线程模拟更新进度)
101 0
|
4月前
|
XML API Android开发
Android 自定义View 之 饼状进度条
Android 自定义View 之 饼状进度条
|
4月前
|
Android开发 UED
[Android]ProgressBar进度条
[Android]ProgressBar进度条
40 0
|
4月前
|
XML Java Android开发
Android Studio App开发中异步任务AsynTask与异步服务IntentService的讲解与实战(实现四大名著的加载进度条 附源码)
Android Studio App开发中异步任务AsynTask与异步服务IntentService的讲解与实战(实现四大名著的加载进度条 附源码)
60 0
|
5月前
|
Android开发
android进度条
android进度条
35 0
|
8月前
|
缓存 Java API
#7,Android开发 控件 ProgressBar 进度条
#7,Android开发 控件 ProgressBar 进度条