ProgressDialog对话框中的进度条
1./TestProgerssDialog/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 <Button
13 android:id="@+id/button01"
14 android:layout_width="wrap_content"
15 android:layout_height="wrap_content"
16 android:text="圆形进度条"
17 />
18 <Button
19 android:id="@+id/button02"
20 android:layout_width="wrap_content"
21 android:layout_height="wrap_content"
22 android:text="长形进度条"
23 />
24 </LinearLayout>
2.TestProgerssDialogActivity:
1 import android.app.Activity;
2 import android.app.ProgressDialog;
3 import android.content.DialogInterface;
4 import android.os.Bundle;
5 import android.view.View;
6 import android.view.View.OnClickListener;
7 import android.widget.Button;
8
9 public class TestProgerssDialogActivity extends Activity
10 {
11 private Button mButton01;
12
13 private Button mButton02;
14
15 private int m_count;
16
17 @Override
18 public void onCreate(Bundle savedInstanceState)
19 {
20 super.onCreate(savedInstanceState);
21 setContentView(R.layout.main);
22 mButton01 = (Button) findViewById(R.id.button01);
23 mButton02 = (Button) findViewById(R.id.button02);
24 mButton01.setOnClickListener(listener);
25 mButton02.setOnClickListener(listener);
26 }
27
28 OnClickListener listener = new OnClickListener()
29 {
30 @Override
31 public void onClick(View v)
32 {
33 switch (v.getId())
34 {
35 case R.id.button01:
36 ProgressDialog mProgressDialog = new ProgressDialog(TestProgerssDialogActivity.this);
37 // 设置mProgressDialog风格
38 mProgressDialog.setProgress(ProgressDialog.STYLE_SPINNER);
39 // 设置mProgressDialog标题
40 mProgressDialog.setTitle("提示");
41 // 设置mProgressDialog提示
42 mProgressDialog.setMessage("这是一个圆形进度条对话框");
43 // 设置mProgressDialog进度条的图标
44 mProgressDialog.setIcon(R.drawable.flag);
45 // 设置mProgressDialog的进度条是否不明确
46 mProgressDialog.setIndeterminate(false);
47 // 是否可以按回退键取消
48 mProgressDialog.setCancelable(true);
49 // 设置mProgressDialog的一个Button
50 mProgressDialog.setButton("确定", new DialogInterface.OnClickListener()
51 {
52 @Override
53 public void onClick(DialogInterface dialog, int which)
54 {
55 dialog.cancel();
56 }
57 });
58 // 显示mProgressDialog
59 mProgressDialog.show();
60 break;
61 case R.id.button02:
62 m_count = 0;
63
64 final ProgressDialog mProgressDialog2 = new ProgressDialog(TestProgerssDialogActivity.this);
65 // 设置mProgressDialog风格为长形
66 mProgressDialog2.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
67 // 设置mProgressDialog标题
68 mProgressDialog2.setTitle("提示");
69 // 设置mProgressDialog提示
70 mProgressDialog2.setMessage("这是一个圆形进度条对话框");
71 // 设置mProgressDialog进度条的图标
72 mProgressDialog2.setIcon(R.drawable.flag2);
73 // 设置mProgressDialog的进度条是否不明确
74 mProgressDialog2.setIndeterminate(false);
75 // 是否可以按回退键取消
76 mProgressDialog2.setCancelable(true);
77 new Thread()
78 {
79
80 @Override
81 public void run()
82 {
83 while (m_count <= 100)
84 {
85 try
86 {
87 mProgressDialog2.setProgress(m_count++);
88 Thread.sleep(100);
89 }
90 catch (InterruptedException e)
91 {
92 e.printStackTrace();
93 }
94 }
95 }
96
97 }.start();
98 // 设置mProgressDialog的一个Button
99 mProgressDialog2.setButton("确定", new DialogInterface.OnClickListener()
100 {
101 @Override
102 public void onClick(DialogInterface dialog, int which)
103 {
104 dialog.cancel();
105 }
106 });
107 // 显示mProgressDialog
108 mProgressDialog2.show();
109 break;
110 }
111 }
112 };
113 }