<span style="font-size:14px;">public class MainActivity extends Activity {
Button btn_exit_horizontal;
Button btn_exit_vertical;
ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_exit_horizontal = (Button) findViewById(R.id.btn_exit_horizontal);
btn_exit_horizontal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showHorizontalProgressDialog();
}
});
btn_exit_vertical = (Button) findViewById(R.id.btn_exit_vertical);
btn_exit_vertical.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showVerticalProgressDialog();
}
});
}
void showHorizontalProgressDialog(){ //显示进度条
pd = new ProgressDialog(this);
pd.setTitle("LHP的进度条"); //设置进度条的标题
pd.setMessage("退出中......"); //进度条内的显示信息
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //水平进度条
pd.setCancelable(true); //点击Back后可否取消进度条
pd.setCanceledOnTouchOutside(true); //点击进度条外部后可否取消进度条
pd.show(); //显示进度条
new Thread(new Runnable() { //增加显示进度的线程
int i = 0;
public void run() {
while(i<20){ //10次增加
try {
Thread.sleep(1000); //延时增加
pd.incrementProgressBy(5); //每次增加10%
i++;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
pd.dismiss(); //关闭进度条
}
}).start();
}
void showVerticalProgressDialog(){
pd = new ProgressDialog(this);
pd.setTitle("LHP的进度条");
pd.setMessage("退出中......");
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER); //圆形进度条
pd.setCancelable(true); //点击Back后可否取消进度条
pd.setCanceledOnTouchOutside(true); //点击进度条外部后可否取消进度条
pd.show(); //显示进度条
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(3000);
pd.dismiss(); //延时3S退出
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
}</span>