以下ProgressBar跟Handle结合,模拟进度条的使用,当进度条完成时会跳转到TestActivity

main.xml布局文件

<?
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
"
>


<!--
长方形进度条,一开始不可见,直到点击按钮时才出现进度条
-->


<
ProgressBar android:id
=
"
@+id/progressBar
"

style
=
"
?android:attr/progressBarStyleHorizontal
"

mce_style
=
"
?android:attr/progressBarStyleHorizontal
"

android:layout_width
=
"
fill_parent
"

android:layout_height
=
"
wrap_content
"

android:visibility
=
"
gone
"

android:max
=
"
100
"

/>


<!--
圆形进度条
-->


<!--<
ProgressBar android:id
=
"
@+id/progressBar
"

style
=
"
?android:attr/progressBarStyleLarge
"

mce_style
=
"
?android:attr/progressBarStyleLarge
"

android:layout_width
=
"
wrap_content
"

android:layout_height
=
"
wrap_content
"

/>-->


<
Button android:id
=
"
@+id/start
"

android:text
=
"
启动进度条
"

android:layout_width
=
"
wrap_content
"

android:layout_height
=
"
wrap_content
"

/>


<
Button android:id
=
"
@+id/stop
"

android:text
=
"
停止进度条
"

android:layout_width
=
"
wrap_content
"

android:layout_height
=
"
wrap_content
"

/>


</
LinearLayout
>
PbActivity类
package
com.ljq.pb;


import
android.app.Activity;

import
android.content.Intent;

import
android.os.Bundle;

import
android.os.Handler;

import
android.view.View;

import
android.widget.Button;

import
android.widget.ProgressBar;


public

class
PbActivity
extends
Activity {

private
ProgressBar progressBar
=

null
;

private
Button start
=

null
, stop
=

null
;

//
定义Handler对象



private
Handler handler
=

new
Handler();


@Override

public

void
onCreate(Bundle savedInstanceState) {

super
.onCreate(savedInstanceState);
setContentView(R.layout.main);

progressBar
=
(ProgressBar) findViewById(R.id.progressBar);
progressBar.setProgress(
0
);

start
=
(Button) findViewById(R.id.start);
start.setOnClickListener(
new
View.OnClickListener() {


public

void
onClick(View v) {
handler.post(runnable);
//
开始执行


}

});
stop
=
(Button)findViewById(R.id.stop);
stop.setOnClickListener(
new
View.OnClickListener() {


public

void
onClick(View v) {
handler.removeCallbacks(runnable);
//
停止执行


progressBar.setProgress(
0
);
}

});
}


int
pro
=
0
;
Runnable runnable
=
new
Runnable(){

public

void
run() {
progressBar.setVisibility(View.VISIBLE);
pro
=
progressBar.getProgress()
+
10
;
progressBar.setProgress(pro);

//
如果进度小于100,,则延迟1000毫秒后重复执行runnable



if
(pro
<
100
){
handler.postDelayed(runnable,
1000
);
}
else
{
progressBar.setVisibility(View.GONE);
startActivity(
new
Intent(PbActivity.
this
, TestActivity.
class
));
handler.removeCallbacks(runnable);
progressBar.setProgress(
0
);
}
}
};
}

运行结果