您的位置:首页 > 移动开发 > Android开发

第33章、进度条对话框ProgressDialog(从零开始学Android)

2013-01-09 15:33 411 查看
  进度条对话框ProgressDialog经常用于不能马上完成的操作,为了避免用户莫名其妙的等待,给用户一个提示。

  本例中我们演示了两种进度条:条形进度条和圆形进度条。

一、设计界面

  1、打开“res/layout/activity_main.xml”文件。

  从工具栏向activity拖出2个按钮Button。

  


  2、打开activity_main.xml文件。

  代码如下:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <Button 
        android:id="@+id/progress" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="条形进度条" /> 
        
      <Button 
        android:id="@+id/circle" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="圆形进度条" /> 

</LinearLayout>


二、长按事件 

  打开“src/com.genwoxue.progress/MainActivity.java”文件。

  然后输入以下代码:  

package com.genwoxue.progress;

import android.app.Activity;  
import android.app.ProgressDialog;  
import android.content.DialogInterface;  
import android.os.Bundle;  
import android.view.View;  
import android.widget.Button;  

public class MainActivity extends Activity  

{  
	//声明按钮
     private Button btnCircle=null;
     private Button btnProgress=null; 
     //声明进度条对话框  
     private ProgressDialog pdDialog=null;  
     //进度计数
     int iCount  = 0;  

     @Override 
     public void onCreate(Bundle savedInstanceState)  

     {  

         super.onCreate(savedInstanceState);  
         setContentView(R.layout.activity_main);  

        //获取按钮对象  
         btnCircle = (Button)findViewById(R.id.circle);  
         btnProgress = (Button)findViewById(R.id.progress);  

         //设置btnCircle的事件监听  
         btnCircle.setOnClickListener(new Button.OnClickListener() {  

             @Override 
             public void onClick(View v){
            	 
            	 iCount  = 0;  
                 //创建ProgressDialog对象  
                 pdDialog = new ProgressDialog(MainActivity.this);  

                 //设置进度条风格,风格为圆形,旋转的  
                 pdDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);  

                 // 设置ProgressDialog 标题  
                 pdDialog.setTitle("圆形进度条");  

                 // 设置ProgressDialog 提示信息  
                 pdDialog.setMessage("正在下载中……");  

                 // 设置ProgressDialog 标题图标  
                 pdDialog.setIcon(R.drawable.ic_launcher);  
                 
                 // 设置ProgressDialog 进度条进度  
                 pdDialog.setProgress(100);  

                 // 设置ProgressDialog 的进度条是否不明确  
                 pdDialog.setIndeterminate(false);  

                 // 设置ProgressDialog 是否可以按退回按键取消  
                 pdDialog.setCancelable(true);  

                 // 设置ProgressDialog 的一个Button 
                 pdDialog.setButton("取消", new DialogInterface.OnClickListener() {  
                     public void onClick(DialogInterface dialog, int i)  
                     {  
                         //点击“取消”按钮取消对话框  
                         dialog.cancel();  
                     }  
                 });   

                 // 让ProgressDialog显示  
                 pdDialog.show();  
                 
               //创建线程实例
                 new Thread(){  
                      public void run(){  
                          try{  
                              while (iCount  <= 100) {  
                                  // 由线程来控制进度。  
                                  pdDialog.setProgress(iCount ++);  
                                  Thread.sleep(50);  
                             }  
                             pdDialog.cancel();  
                          }  
                          catch (InterruptedException e){  
                              pdDialog.cancel();  
                          }  
                      }  

                  }.start();   
             }  

         });  

           

       //设置btnProgress的事件监听  
         btnProgress.setOnClickListener(new Button.OnClickListener() {  
             @Override 
             public void onClick(View v)  
             {  
            	 iCount  = 0;  
                // 创建ProgressDialog对象  
                 pdDialog = new ProgressDialog(MainActivity.this); 
                 
                // 设置进度条风格,风格为长形  
                pdDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                
                // 设置ProgressDialog 标题  
                 pdDialog.setTitle("条形进度条");  
                 
                // 设置ProgressDialog 提示信息  
                pdDialog.setMessage("正在下载中……");  
                
                // 设置ProgressDialog 标题图标  
                 pdDialog.setIcon(R.drawable.ic_launcher);  

                 // 设置ProgressDialog 进度条进度  
                 pdDialog.setProgress(100);  

                // 设置ProgressDialog 的进度条是否不明确  
                 pdDialog.setIndeterminate(false);  

                 // 设置ProgressDialog 是否可以按退回按键取消  
                 pdDialog.setCancelable(true);  

                 // 让ProgressDialog显示  
                pdDialog.show();  

                //创建线程实例
                new Thread(){  
                     public void run(){  
                         try{  
                             while (iCount  <= 100) {  
                                 // 由线程来控制进度。  
                                 pdDialog.setProgress(iCount ++);  
                                 Thread.sleep(50);  
                            }  
                            pdDialog.cancel();  
                         }  
                         catch (InterruptedException e){  
                             pdDialog.cancel();  
                         }  
                     }  

                 }.start();   
             }  
         });  
     }  
 }


三、运行效果

  

 


    
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: