package org.lxh.demo;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
public class MyProgressBarDemo extends Activity {
private ProgressBar myprobarA, myprobarB, myprobarC, myprobarD, myprobarE;
private Button mybut; // 控制按钮
protected static final int STOP = 1; // 停止消息
protected static final int CONTINUE = 2; // 继续消息
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.myprobarA = (ProgressBar) this.findViewById(R.id.myprobarA); // 取得进度条
this.myprobarB = (ProgressBar) this.findViewById(R.id.myprobarB); // 取得进度条
this.myprobarC = (ProgressBar) this.findViewById(R.id.myprobarC); // 取得进度条
this.myprobarD = (ProgressBar) this.findViewById(R.id.myprobarD); // 取得进度条
this.myprobarE = (ProgressBar) this.findViewById(R.id.myprobarE); // 取得进度条
this.mybut = (Button) this.findViewById(R.id.mybut); // 取得按钮
this.myprobarA.setIndeterminate(false); // 确定不确定模式
this.myprobarB.setIndeterminate(false); // 确定不确定模式
this.myprobarC.setIndeterminate(true); // 确定确定模式
this.myprobarD.setIndeterminate(false); // 确定不确定模式
this.myprobarE.setIndeterminate(false); // 确定不确定模式
this.mybut.setOnClickListener(new OnClickListenerImpl()) ; // 设置单击事件
}
private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View v) { // 单击事件
MyProgressBarDemo.this.myprobarB.setSecondaryProgress(0); // 第二进度条
MyProgressBarDemo.this.myprobarA.setVisibility(View.VISIBLE); // 组件可见
MyProgressBarDemo.this.myprobarB.setVisibility(View.VISIBLE); // 组件可见
MyProgressBarDemo.this.myprobarC.setVisibility(View.VISIBLE); // 组件可见
MyProgressBarDemo.this.myprobarD.setVisibility(View.VISIBLE); // 组件可见
MyProgressBarDemo.this.myprobarE.setVisibility(View.VISIBLE); // 组件可见
MyProgressBarDemo.this.myprobarA.setMax(120); // 设置最大值
MyProgressBarDemo.this.myprobarB.setMax(120); // 设置最大值
MyProgressBarDemo.this.myprobarA.setProgress(0); // 设置当前值
MyProgressBarDemo.this.myprobarB.setProgress(0); // 设置当前值
new Thread(new Runnable() {
public void run() { // 线程主体
int count = 0; // 用于保存当前进度值
for (int i = 0; i < 10; i++) { // 循环设置内容
try {
count = (i + 1) * 20; // 设置进度条当前值
Thread.sleep(500); // 休眠0.5秒
if (i == 6) { // 如果为6,则进度为120
Message m = new Message(); // 定义消息
m.what = MyProgressBarDemo.STOP; // 消息代码
MyProgressBarDemo.this.myMessageHandler
.sendMessage(m); // 发送消息
break; // 循环中断
} else {
Message m = new Message(); // 定义消息
m.arg1 = count; // 设置参数
m.what = MyProgressBarDemo.CONTINUE; // 消息代码
MyProgressBarDemo.this.myMessageHandler
.sendMessage(m); // 发送消息
}
} catch (Exception ex) { // 处理sleep()异常
ex.printStackTrace();
}
}
}
}).start(); // 启动线程
}
}
private Handler myMessageHandler = new Handler() { // 共用一个Handler
@Override
public void handleMessage(Message msg) {
switch (msg.what) { // 判断消息类型
case MyProgressBarDemo.STOP: // 停止记录
myprobarA.setVisibility(View.GONE); // 组件不可见
myprobarB.setVisibility(View.GONE); // 组件不可见
myprobarC.setVisibility(View.GONE); // 组件不可见
myprobarD.setVisibility(View.GONE); // 组件不可见
myprobarE.setVisibility(View.GONE); // 组件不可见
Thread.currentThread().interrupt(); // 组件不可见
break;
case MyProgressBarDemo.CONTINUE: // 组件增长
if (!Thread.currentThread().isInterrupted()) {
myprobarA.setProgress(msg.arg1); // 设置当前进度
myprobarB.setProgress(msg.arg1); // 设置当前进度
myprobarC.setProgress(msg.arg1); // 设置当前进度
myprobarD.setProgress(msg.arg1); // 设置当前进度
myprobarE.setProgress(msg.arg1); // 设置当前进度
}
break;
}
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MyLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ProgressBar
android:id="@+id/myprobarA"
style="?android:attr/progressBarStyle"
android:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ProgressBar
android:id="@+id/myprobarB"
style="?android:attr/progressBarStyleHorizontal"
android:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ProgressBar
android:id="@+id/myprobarC"
style="?android:attr/progressBarStyleHorizontal"
android:visibility="gone"
android:max="120"
android:progress="0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ProgressBar
android:id="@+id/myprobarD"
android:visibility="gone"
android:max="120"
android:progress="50"
android:secondaryProgress="70"
style="?android:attr/progressBarStyleLarge"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ProgressBar
android:id="@+id/myprobarE"
android:visibility="gone"
android:max="120"
android:progress="50"
android:secondaryProgress="70"
style="?android:attr/progressBarStyleSmall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/mybut"
android:text="显示进度条"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>