ProgressBar, ProgressDialog

参考: http://blog.csdn.net/Android_Tutor/article/details/5695170

【详解】Android ProgressBar, ProgressDialog 进度条 进度条对话框
  http://www.oschina.net/question/157182_37992
在android中,通常我们无法在单独的线程中更新UI,而要在主线程中,这也就是为什么我们要使用 Handler了,当handler收到消息中,它会把它放入到队列中等待执行,通常来说这会很快被执行。


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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />


    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ProgressBar
        android:id="@+id/progressBar2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示进度条" />

</LinearLayout>


package com.pandy.progress;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class ProgressBarDemoActivity extends Activity {
    /** Called when the activity is first created. */
	private ProgressBar progressBar1,progressBar2;
	private Button button1;
	
	protected static final int STOP = 0x10000;  
    protected static final int NEXT = 0x10001;  
    private int iCount = 0;  
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        progressBar1 = (ProgressBar)findViewById(R.id.progressBar1);
        progressBar2 = (ProgressBar)findViewById(R.id.progressBar2);
        button1 = (Button)findViewById(R.id.button1);
        
        progressBar1.setIndeterminate(false);
        progressBar2.setIndeterminate(false);
        
        progressBar1.setVisibility(View.GONE);  
        progressBar2.setVisibility(View.GONE); 
        
        button1.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				progressBar1.setVisibility(View.VISIBLE);
				progressBar2.setVisibility(View.VISIBLE);
				final int max = 100;
				
				progressBar1.setMax(max);
				progressBar1.setProgress(0);
				progressBar2.setProgress(0);
				//创建线程
				Thread t = new Thread(new Runnable(){

					@Override
					public void run() {
						// TODO Auto-generated method stub
						for(int i=0; i<max; i++){
							try {
								iCount = (i+1)*1;
								Thread.sleep(10);
								if(iCount>=max){
									Message msg = new Message();
									msg.what=STOP;
									//传递消息
									mHandler.sendMessage(msg);
								}else{
									Message msg = new Message();
									msg.what=NEXT;
									//传递消息
									mHandler.sendMessage(msg);
								}
								
							} catch (Exception e) {
								// TODO: handle exception
								Log.i("ProgressBarDemoActivity", "ProgressBarDemoActivity-------------");
							}
							
						}
						
					}
					
				});
				//启动线程
				t.start();
				
			}
        	
        });
        
    }
   //消息处理
   private Handler mHandler = new Handler(){
	   public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case STOP:
				progressBar1.setVisibility(View.GONE);
				progressBar2.setVisibility(View.GONE);
				Thread.currentThread().interrupt();
				break;
			case NEXT:
				if(!Thread.currentThread().isInterrupted()){
					progressBar1.setProgress(iCount);
					progressBar2.setProgress(iCount);
				}
				break;
			}
	   };
	   
   };
}

你可能感兴趣的:(ProgressDialog)