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" >
<ProgressBar
android:id="@+id/progressBar2"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="278dp"
android:visibility="gone"
android:layout_gravity="center_vertical"
android:max="100" />
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动进度条" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止进度条" />
</LinearLayout>
</LinearLayout>
ProgressbarActivity.java
package com.android.forlinx;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
public class ProgressbarActivity extends Activity {
private ProgressBar progressbar = null;
private Button start = null ,stop = null;
private Handler handler = new Handler();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
progressbar = (ProgressBar)findViewById(R.id.progressBar2);
start = (Button)findViewById(R.id.button1);
stop = (Button)findViewById(R.id.button2);
progressbar.setProgress(0);
start.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
handler.post(runnable);
}
});
stop.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
handler.removeCallbacks(runnable);
}
});
}
int progress = 0;
Runnable runnable = new Runnable() {
public void run() {
progressbar.setVisibility(View.VISIBLE);
progress = progressbar.getProgress()+10;
progressbar.setProgress(progress);
if (progress < 100){
handler.postDelayed(runnable, 100);
}else{
progressbar.setVisibility(View.GONE);
startActivity(new Intent(ProgressbarActivity.this, TestActivity.class));
handler.removeCallbacks(runnable);
progressbar.setProgress(0);
}
}
};
}
效果图