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);
}
};
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值