progressBar进度条
1./TestProgressBar/res/layout/main.xml:
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="vertical"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent"
6 >
7 <TextView
8 android:layout_width="fill_parent"
9 android:layout_height="wrap_content"
10 android:text="@string/hello"
11 />
12 <ProgressBar
13 android:id="@+id/progressbar01"
14 style="?android:attr/progressBarStyleHorizontal"
15 android:layout_width="200dp"
16 android:layout_height="wrap_content"
17 android:visibility="gone"
18 />
19 <ProgressBar
20 android:id="@+id/progressbar02"
21 style="?android:attr/progressBarStyleLarge"
22 android:layout_width="wrap_content"
23 android:layout_height="wrap_content"
24 android:max="100"
25 android:progress="50"
26 android:secondaryProgress="70"
27 android:visibility="gone"
28 />
29 <Button
30 android:id="@+id/button01"
31 android:layout_width="wrap_content"
32 android:layout_height="wrap_content"
33 android:text="开始"
34 />
35 </LinearLayout>
2.TestProgressBarActivity:
1 import android.app.Activity;
2 import android.os.Bundle;
3 import android.os.Handler;
4 import android.os.Message;
5 import android.view.View;
6 import android.view.Window;
7 import android.view.View.OnClickListener;
8 import android.widget.Button;
9 import android.widget.ProgressBar;
10
11 public class TestProgressBarActivity extends Activity
12 {
13 private Button button;
14 private ProgressBar progressBar01;
15 private ProgressBar progressBar02;
16 private static final int GUI_STOP_NOTIFIER = 0x108;
17 private static final int GUI_THREADING_NOTIFIER = 0x109;
18 public int intCounter = 0;
19
20 @Override
21 public void onCreate(Bundle savedInstanceState)
22 {
23 super.onCreate(savedInstanceState);
24 // 设置窗口模式,因为需要显示进度条在标题栏
25 requestWindowFeature(Window.FEATURE_PROGRESS);
26 setProgressBarVisibility(true);
27 setContentView(R.layout.main);
28
29 // 取得ProgressBar
30 progressBar01 = (ProgressBar) findViewById(R.id.progressbar01);
31 progressBar02 = (ProgressBar) findViewById(R.id.progressbar02);
32 button = (Button) findViewById(R.id.button01);
33
34 progressBar01.setIndeterminate(false);
35 progressBar02.setIndeterminate(false);
36
37 // 当按下按钮时开始执行
38 button.setOnClickListener(new OnClickListener()
39 {
40 @Override
41 public void onClick(View arg0)
42 {
43 // 设置为可见状态
44 progressBar01.setVisibility(View.VISIBLE);
45 progressBar02.setVisibility(View.VISIBLE);
46 // 设置最大值
47 progressBar02.setMax(100);
48 // 设置初始值
49 progressBar01.setProgress(0);
50 progressBar02.setProgress(0);
51
52 // 通过线程来改变ProgressBar的值
53 new Thread(new Runnable()
54 {
55 @Override
56 public void run()
57 {
58 for (int i = 0; i < 10; i++)
59 {
60 try
61 {
62 intCounter = (i + 1) * 10;
63 Thread.sleep(2000);
64 if (i == 4)
65 {
66 Message m = new Message();
67 m.what = TestProgressBarActivity.GUI_STOP_NOTIFIER;
68 TestProgressBarActivity.this.myMessageHandler.sendMessage(m);
69 }
70 else
71 {
72 Message m = new Message();
73 m.what = TestProgressBarActivity.GUI_THREADING_NOTIFIER;
74 TestProgressBarActivity.this.myMessageHandler.sendMessage(m);
75 }
76 }
77 catch (InterruptedException e)
78 {
79 e.printStackTrace();
80 }
81 }
82 }
83 }).start();
84 }
85 });
86 }
87
88 Handler myMessageHandler = new Handler()
89 {
90
91 @Override
92 public void handleMessage(Message msg)
93 {
94 switch (msg.what)
95 {
96 case TestProgressBarActivity.GUI_STOP_NOTIFIER:
97 progressBar01.setVisibility(View.GONE);
98 progressBar02.setVisibility(View.GONE);
99 Thread.currentThread().interrupt();
100 break;
101 case TestProgressBarActivity.GUI_THREADING_NOTIFIER:
102 if (!Thread.currentThread().isInterrupted())
103 {
104 // 改变ProgressBar的当前值
105 progressBar01.setProgress(intCounter);
106 progressBar02.setProgress(intCounter);
107 // 设置标题栏中的前景的一个进度条进度值
108 setProgress(intCounter * 100);
109 // 设置标题栏中后面的一个进度条进度值
110 setSecondaryProgress(intCounter * 100);
111 }
112 break;
113 }
114 super.handleMessage(msg);
115 }
116
117 };
118 }
3.效果图: