Android中ProgressDialog的简单示例

转载自 http://guocc.iteye.com/blog/1181467

 

 

网上一般对进度条的示例都是如何显示,没有在任务结束如何关闭的文章,参考其他文章经过试验之后把整套进度条显示的简单示例如下:

建立android工程等工作都略去,Google一下就可以了。

下面来介绍主要的Activity
ProgressBarDemo.java

Java代码 复制代码  收藏代码
  1. package com.lveyo.android.demo.progressbar;   
  2.   
  3. import android.app.Activity;   
  4. import android.app.ProgressDialog;   
  5. import android.os.Bundle;   
  6. import android.os.Handler;   
  7. import android.os.Message;   
  8. import android.view.View;   
  9. import android.widget.Button;   
  10. import android.widget.TextView;   
  11.   
  12. public class ProgressBarDemo extends Activity {   
  13.        
  14.     private TextView statusTextView;   
  15.     private Button beginBtn;   
  16.     private ProgressDialog progressDialog;   
  17.        
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {   
  20.         super.onCreate(savedInstanceState);   
  21.         setContentView(R.layout.main);   
  22.         statusTextView = (TextView)findViewById(R.id.status);   
  23.         beginBtn = (Button)findViewById(R.id.beginBtn);   
  24.         setListener();   
  25.     }   
  26.        
  27.     /**  
  28.      * 用Handler来更新UI  
  29.      */  
  30.     private Handler handler = new Handler(){   
  31.   
  32.         @Override  
  33.         public void handleMessage(Message msg) {   
  34.                
  35.             //关闭ProgressDialog   
  36.             progressDialog.dismiss();   
  37.                
  38.             //更新UI   
  39.             statusTextView.setText("Completed!");   
  40.         }};   
  41.        
  42.            
  43.     /**  
  44.      * 点击按钮事件listener  
  45.      */  
  46.     private void setListener(){   
  47.         beginBtn.setOnClickListener(new View.OnClickListener() {   
  48.                
  49.             @Override  
  50.             public void onClick(View v) {   
  51.                    
  52.                 //显示ProgressDialog   
  53.                 progressDialog = ProgressDialog.show(ProgressBarDemo.this"Loading...""Please wait..."truefalse);   
  54.                    
  55.                 //新建线程   
  56.                 new Thread(){   
  57.   
  58.                     @Override  
  59.                     public void run() {   
  60.                         //需要花时间计算的方法   
  61.                         Calculation.calculate(4);   
  62.                            
  63.                         //向handler发消息   
  64.                         handler.sendEmptyMessage(0);   
  65.                     }}.start();   
  66.             }   
  67.         });   
  68.     }   
  69.        
  70. }  
Java代码 复制代码  收藏代码
  1. package com.lveyo.android.demo.progressbar;   
  2.   
  3. import android.app.Activity;   
  4. import android.app.ProgressDialog;   
  5. import android.os.Bundle;   
  6. import android.os.Handler;   
  7. import android.os.Message;   
  8. import android.view.View;   
  9. import android.widget.Button;   
  10. import android.widget.TextView;   
  11.   
  12. public class ProgressBarDemo extends Activity {   
  13.        
  14.     private TextView statusTextView;   
  15.     private Button beginBtn;   
  16.     private ProgressDialog progressDialog;   
  17.        
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {   
  20.         super.onCreate(savedInstanceState);   
  21.         setContentView(R.layout.main);   
  22.         statusTextView = (TextView)findViewById(R.id.status);   
  23.         beginBtn = (Button)findViewById(R.id.beginBtn);   
  24.         setListener();   
  25.     }   
  26.        
  27.     /**  
  28.      * 用Handler来更新UI  
  29.      */  
  30.     private Handler handler = new Handler(){   
  31.   
  32.         @Override  
  33.         public void handleMessage(Message msg) {   
  34.                
  35.             //关闭ProgressDialog   
  36.             progressDialog.dismiss();   
  37.                
  38.             //更新UI   
  39.             statusTextView.setText("Completed!");   
  40.         }};   
  41.        
  42.            
  43.     /**  
  44.      * 点击按钮事件listener  
  45.      */  
  46.     private void setListener(){   
  47.         beginBtn.setOnClickListener(new View.OnClickListener() {   
  48.                
  49.             @Override  
  50.             public void onClick(View v) {   
  51.                    
  52.                 //显示ProgressDialog   
  53.                 progressDialog = ProgressDialog.show(ProgressBarDemo.this"Loading...""Please wait..."truefalse);   
  54.                    
  55.                 //新建线程   
  56.                 new Thread(){   
  57.   
  58.                     @Override  
  59.                     public void run() {   
  60.                         //需要花时间计算的方法   
  61.                         Calculation.calculate(4);   
  62.                            
  63.                         //向handler发消息   
  64.                         handler.sendEmptyMessage(0);   
  65.                     }}.start();   
  66.             }   
  67.         });   
  68.     }   
  69.        
  70. }  
package com.lveyo.android.demo.progressbar;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ProgressBarDemo extends Activity {
    
	private TextView statusTextView;
	private Button beginBtn;
	private ProgressDialog progressDialog;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        statusTextView = (TextView)findViewById(R.id.status);
        beginBtn = (Button)findViewById(R.id.beginBtn);
        setListener();
    }
    
    /**
     * 用Handler来更新UI
     */
    private Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			
			//关闭ProgressDialog
			progressDialog.dismiss();
			
			//更新UI
			statusTextView.setText("Completed!");
		}};
    
		
	/**
	 * 点击按钮事件listener
	 */
    private void setListener(){
    	beginBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				//显示ProgressDialog
				progressDialog = ProgressDialog.show(ProgressBarDemo.this, "Loading...", "Please wait...", true, false);
				
				//新建线程
				new Thread(){

					@Override
					public void run() {
						//需要花时间计算的方法
						Calculation.calculate(4);
						
						//向handler发消息
						handler.sendEmptyMessage(0);
					}}.start();
			}
		});
    }
    
}



Calculation.java

Java代码 复制代码  收藏代码
  1. package com.lveyo.android.demo.progressbar;   
  2.   
  3. /**  
  4.  * 示意方法  
  5.  * @author lveyo  
  6.  *  
  7.  */  
  8. public class Calculation {   
  9.        
  10.     public static void calculate(int sleepSeconds){   
  11.         try {   
  12.             Thread.sleep(sleepSeconds * 1000);   
  13.         } catch (Exception e) {   
  14.             // TODO: handle exception   
  15.         }   
  16.     }   
  17.   
  18. }  
Java代码 复制代码  收藏代码
  1. package com.lveyo.android.demo.progressbar;   
  2.   
  3. /**  
  4.  * 示意方法  
  5.  * @author lveyo  
  6.  *  
  7.  */  
  8. public class Calculation {   
  9.        
  10.     public static void calculate(int sleepSeconds){   
  11.         try {   
  12.             Thread.sleep(sleepSeconds * 1000);   
  13.         } catch (Exception e) {   
  14.             // TODO: handle exception   
  15.         }   
  16.     }   
  17.   
  18. }  
package com.lveyo.android.demo.progressbar;

/**
 * 示意方法
 * @author lveyo
 *
 */
public class Calculation {
	
	public static void calculate(int sleepSeconds){
		try {
			Thread.sleep(sleepSeconds * 1000);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

}




main.xml文件

Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView android:id="@+id/status"  
  8.     android:layout_width="fill_parent"    
  9.     android:layout_height="wrap_content"    
  10.     android:text="@string/hello"  
  11.     />  
  12. <Button android:id="@+id/beginBtn"  
  13.     android:layout_width="fill_parent"  
  14.     android:layout_height="wrap_content"  
  15.     android:text="begin"  
  16.     />  
  17. </LinearLayout>  
Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView android:id="@+id/status"  
  8.     android:layout_width="fill_parent"    
  9.     android:layout_height="wrap_content"    
  10.     android:text="@string/hello"  
  11.     />  
  12. <Button android:id="@+id/beginBtn"  
  13.     android:layout_width="fill_parent"  
  14.     android:layout_height="wrap_content"  
  15.     android:text="begin"  
  16.     />  
  17. </LinearLayout>  



在android中,通常我们无法在单独的线程中更新UI,而要在主线程中,这也就是为什么我们要使用 Handler了,当handler收到消息中,它会把它放入到队列中等待执行,通常来说这会很快被执行。

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值