网上一般对进度条的示例都是如何显示,没有在任务结束如何关闭的文章,参考其他文章经过试验之后把整套进度条显示的简单示例如下:

建立android工程等工作都略去,Google一下就可以了。

下面来介绍主要的Activity
ProgressBarDemo.java

​​



1. package
2.
3. import
4. import
5. import
6. import
7. import
8. import
9. import
10. import
11.
12. public class ProgressBarDemo extends
13.
14. private
15. private
16. private
17.
18. @Override
19. public void
20. super.onCreate(savedInstanceState);
21. setContentView(R.layout.main);
22. statusTextView = (TextView)findViewById(R.id.status);
23. beginBtn = (Button)findViewById(R.id.beginBtn);
24. setListener();
25. }
26.
27. /**
28. * 用Handler来更新UI
29. */
30. private Handler handler = new
31.
32. @Override
33. public void
34.
35. //关闭ProgressDialog
36. progressDialog.dismiss();
37.
38. //更新UI
39. "Completed!");
40. }};
41.
42.
43. /**
44. * 点击按钮事件listener
45. */
46. private void
47. new
48.
49. @Override
50. public void
51.
52. //显示ProgressDialog
53. this, "Loading...", "Please wait...", true, false);
54.
55. //新建线程
56. new
57.
58. @Override
59. public void
60. //需要花时间计算的方法
61. 4);
62.
63. //向handler发消息
64. 0);
65. }}.start();
66. }
67. });
68. }
69.
70. }
package com.lveyo.android.demo.progressbar;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ProgressBarDemo extends Activity {

private TextView statusTextView;
private Button beginBtn;
private ProgressDialog progressDialog;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
statusTextView = (TextView)findViewById(R.id.status);
beginBtn = (Button)findViewById(R.id.beginBtn);
setListener();
}

/**
* 用Handler来更新UI
*/
private Handler handler = new Handler(){

@Override
public void handleMessage(Message msg) {

//关闭ProgressDialog
progressDialog.dismiss();

//更新UI
statusTextView.setText("Completed!");
}};


/**
* 点击按钮事件listener
*/
private void setListener(){
beginBtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

//显示ProgressDialog
progressDialog = ProgressDialog.show(ProgressBarDemo.this, "Loading...", "Please wait...", true, false);

//新建线程
new Thread(){

@Override
public void run() {
//需要花时间计算的方法
Calculation.calculate(4);

//向handler发消息
handler.sendEmptyMessage(0);
}}.start();
}
});
}

}
package com.lveyo.android.demo.progressbar;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ProgressBarDemo extends Activity {

private TextView statusTextView;
private Button beginBtn;
private ProgressDialog progressDialog;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
statusTextView = (TextView)findViewById(R.id.status);
beginBtn = (Button)findViewById(R.id.beginBtn);
setListener();
}

/**
* 用Handler来更新UI
*/
private Handler handler = new Handler(){

@Override
public void handleMessage(Message msg) {

//关闭ProgressDialog
progressDialog.dismiss();

//更新UI
statusTextView.setText("Completed!");
}};


/**
* 点击按钮事件listener
*/
private void setListener(){
beginBtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

//显示ProgressDialog
progressDialog = ProgressDialog.show(ProgressBarDemo.this, "Loading...", "Please wait...", true, false);

//新建线程
new Thread(){

@Override
public void run() {
//需要花时间计算的方法
Calculation.calculate(4);

//向handler发消息
handler.sendEmptyMessage(0);
}}.start();
}
});
}

}