例子如下所示
1
package com.zhihe.xqsh.test;
2
3
import android.app.Activity;
4
import android.app.Dialog;
5
import android.app.ProgressDialog;
6
import android.os.Bundle;
7
import android.os.Handler;
8
import android.os.Message;
9
import android.util.Log;
10
import android.view.View;
11
import android.view.View.OnClickListener;
12
import android.widget.Button;
13
import android.widget.TextView;
14
15
import com.zhihe.xqsh.activity.R;
16
17
public class Sample2_19_Activity extends Activity {
18
19
final int PROGRESS_DIALOG = 0;
20
final int INCREASE = 0;
21
final int MAX_COUNTER = 100;
22
ProgressDialog pd;
23
Handler hd;
24
private TextView tv_network;
25
private Button bt_dialog;
26
27
@Override
28
public void onCreate(Bundle savedInstanceState)
29
{
30
super.onCreate(savedInstanceState);
31
setContentView(R.layout.network);
32
33
tv_network = (TextView) this.findViewById(R.id.tv_network);
34
bt_dialog = (Button)this.findViewById(R.id.bt_dialog);
35
bt_dialog.setOnClickListener(
36
new OnClickListener(){
37
@Override
38
public void onClick(View v){
39
showDialog(PROGRESS_DIALOG);
40
}
41
}
42
);
43
hd = new Handler(){
44
@Override
45
public void handleMessage(Message msg) //必須重寫此方法,用于接收數據
46
{
47
super.handleMessage(msg);
48
switch(msg.what)
49
{
50
case INCREASE:
51
pd.incrementProgressBy(1);//進度每次加1
52
if(pd.getProgress() >= MAX_COUNTER){
53
pd.dismiss();
54
}
55
break;
56
}
57
}
58
};
59
}
60
61
@Override
62
public Dialog onCreateDialog(int id){
63
switch(id){
64
case PROGRESS_DIALOG:
65
pd = new ProgressDialog(this);
66
pd.setMax(MAX_COUNTER);//設置最大值
67
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
68
pd.setTitle("hello");//設置標題
69
pd.setCancelable(false);//設置進度對話框不能用回退按鈕關閉
70
Log.d("kkk", "kkk");
71
break;
72
}
73
return pd;
74
}
75
76
//每次彈出對話框時被回調以動態更新對話框內容的方法
77
@Override
78
public void onPrepareDialog(int id, Dialog dialog){
79
super.onPrepareDialog(id, dialog);
80
switch(id){
81
case PROGRESS_DIALOG:
82
pd.incrementProgressBy(-pd.getProgress());//對話框進度清零
83
new Thread(){
84
public void run(){
85
while(true){
86
hd.sendEmptyMessage(INCREASE);
87
if(pd.getProgress() >= MAX_COUNTER){
88
break;
89
}
90
try{
91
Thread.sleep(40);
92
}catch(Exception e){
93
e.printStackTrace();
94
}
95
}
96
}
97
}.start();
98
break;
99
}
100
}
101
}
102

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102
