1、main.xml代码:
<?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">
<Button
android:id="@+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/mybutton" />
<ProgressBar
android:id="@+id/progress01"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:visibility="gone" />
<ProgressBar
android:id="@+id/progress02"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<Button
android:id="@+id/buttonstart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button02"/>
<ProgressBar
android:id="@+id/progress03"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:visibility="gone"/>
</LinearLayout>
2、string.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">PregeessBar</string>
<string name="action_settings">Settings</string>
<string name="mybutton">2个进度条</string>
<string name="button02">1个进度条</string>
</resources>
3、Activity01.java代码:
package com.example.pregeessbar;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
public class Activity01 extends Activity {
private ProgressBar progress01=null;
private ProgressBar progress02=null;
private ProgressBar progress03=null;
private Button mybutton=null;
private Button buttonstart=null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mybutton=(Button)findViewById(R.id.mybutton);
buttonstart=(Button)findViewById(R.id.buttonstart);
progress01=(ProgressBar)findViewById(R.id.progress01);
progress02=(ProgressBar)findViewById(R.id.progress02);
progress03=(ProgressBar)findViewById(R.id.progress03);
mybutton.setOnClickListener(new buttonListener());
buttonstart.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
progress03.setVisibility(View.VISIBLE);
updateBarHandler.post(updateThread);
}
});
}
class buttonListener implements android.view.View.OnClickListener{
private int i=0;
@Override
public void onClick(View v) {
if(i==0){
progress01.setVisibility(View.VISIBLE);
progress02.setVisibility(View.VISIBLE);
}
else if(i<100){
progress01.setProgress(i);
progress01.setSecondaryProgress(i+10);
}
else{
progress01.setVisibility(View.GONE);
progress02.setVisibility(View.GONE);
}
i=i+10;
}
}
Handler updateBarHandler=new Handler(){
public void handleMessage(Message msg){
progress03.setProgress(msg.arg1);
updateBarHandler.post(updateThread);
}
};
Runnable updateThread=new Runnable(){
int i=0;
@Override
public void run() {
i=i+10;
Message msg=updateBarHandler.obtainMessage();
msg.arg1=i;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
updateBarHandler.sendMessage(msg);
if(i==100){
updateBarHandler.removeCallbacks(updateThread);
System.exit(1);
}
}
};
}
4、运行效果: