`
Aina_hk55HK
  • 浏览: 386373 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

Android ProgressBar的使用

阅读更多
ProgressBar进度条的使用:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TextView android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="@string/hello" />
	<ProgressBar android:id="@+id/ProgressBar01"
		style="?android:attr/progressBarStyleHorizontal" android:visibility="gone"
		android:layout_width="fill_parent" android:layout_height="wrap_content"></ProgressBar>
	<ProgressBar android:id="@+id/ProgressBar02"
		android:visibility="gone" android:max="100" android:progress="50"
		android:secondaryProgress="70" style="?android:attr/progressBarStyleLarge"
		android:layout_width="wrap_content" android:layout_height="wrap_content"></ProgressBar>
	<Button android:id="@+id/Button" android:text="start"
		android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>



package com.Aina.Android;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

public class Test_ProgressBar extends Activity {
	/** Called when the activity is first created. */
	private ProgressBar pBar1, pBar2;
	private Button btn;
	protected static final int STOP = 0x108;
	protected static final int NEXT = 0x109;
	private int iCount = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_PROGRESS);// 设置窗口属性
		this.setProgressBarVisibility(true);// 显示
		setContentView(R.layout.main);
		pBar1 = (ProgressBar) this.findViewById(R.id.ProgressBar01);
		pBar2 = (ProgressBar) this.findViewById(R.id.ProgressBar02);
		btn = (Button) this.findViewById(R.id.Button);
		pBar1.setIndeterminate(false);
		pBar2.setIndeterminate(false);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				pBar1.setVisibility(View.VISIBLE);
				pBar2.setVisibility(View.VISIBLE);// 设置为可见
				pBar1.setMax(100);// 设置最大值
				pBar1.setProgress(0);
				pBar2.setProgress(0);// 设置当前值
				new Thread(new Runnable(){

					@Override
					public void run() {
						// TODO Auto-generated method stub
						for(int i=0;i<10;i++){
							try{
								iCount = (i+1)*20;
								Thread.sleep(1000);
								if(i==4){
									Message msg = new Message();
									msg.what = STOP;
									Test_ProgressBar.this.mHandler.sendMessage(msg);
									break;
								}else{
									Message msg= new Message();
									msg.what = NEXT;
									Test_ProgressBar.this.mHandler.sendMessage(msg);
								}
							}catch(Exception ex){
								ex.printStackTrace();
							}
						}
					}
					
				}){
					
				}.start();
			}

		});
	}

	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch(msg.what){
			case STOP://到了最大值
				pBar1.setVisibility(View.GONE);
				pBar2.setVisibility(View.GONE);//设置进度条不可见
				Thread.currentThread().interrupt();//中断当前线程.
				break;
			case NEXT:
				if(!Thread.currentThread().isInterrupted()){//当前线程正在运行
					pBar1.setProgress(iCount);
					pBar2.setProgress(iCount);//设置当前值
					Test_ProgressBar.this.setProgress(iCount*100);
					Test_ProgressBar.this.setSecondaryProgress(iCount*100);
				}
				break;
			}
			super.handleMessage(msg);
		}
	};
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics