Androidダイアログボックスのプログレスバー(ProgressDialog)
5693 ワード
ダイアログボックスの進捗バーを定義するにはProgressDialogを使用する必要があります.まず、ProgressDialogオブジェクトを作成する必要があります.もちろん、ここではスレッドを使用して進捗バーの表示を制御します.また、以下の方法でProgressDialogを設定できます.setProgressStyle:プログレスバースタイルを設定し、スタイルは円形で、回転します.settittlt:ProgressDialogタイトルsetMessageを設定する:ProgressDialogプロンプト情報を設定する;setIcon:ProgressDialogタイトルアイコンを設定します.setIndeterminate:ProgressDialogの進捗バーが不明かどうかを設定します.setCancelable:ProgressDialogが戻るキーを押してキャンセルできるかどうかを設定します.setButton:ProgressDialogのButtonを設定します(Buttonイベントをリスニングする必要があります).show:ProgressDialogが表示されます.
1.package xiaohang.zhimeng;
2.
3.import android.app.Activity;
4.import android.app.ProgressDialog;
5.import android.content.DialogInterface;
6.import android.os.Bundle;
7.import android.view.View;
8.import android.view.View.OnClickListener;
9.import android.widget.Button;
10.
11.public class Activity01 extends Activity {
12.
13. private Button xhButton01, xhButton02;
14.
15. int xh_count = 0;
16. //
17. ProgressDialog xh_pDialog;
18.
19. @Override
20. public void onCreate(Bundle savedInstanceState) {
21. super.onCreate(savedInstanceState);
22. setContentView(R.layout.main);
23.
24. //
25. xhButton01 = (Button) findViewById(R.id.Button01);
26. xhButton02 = (Button) findViewById(R.id.Button02);
27.
28. // xhButton01
29. xhButton01.setOnClickListener(new OnClickListener() {
30. @Override
31. public void onClick(View v) {
32. // ProgressDialog
33. xh_pDialog = new ProgressDialog(Activity01.this);
34.
35. // , ,
36. xh_pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
37.
38. // ProgressDialog
39. xh_pDialog.setTitle(" ");
40.
41. // ProgressDialog
42. xh_pDialog.setMessage(" ");
43.
44. // ProgressDialog
45. xh_pDialog.setIcon(R.drawable.img1);
46.
47. // ProgressDialog false
48. xh_pDialog.setIndeterminate(false);
49.
50. // ProgressDialog
51. xh_pDialog.setCancelable(true);
52.
53. // ProgressDialog Button
54. xh_pDialog.setButton(" ", new Bt1DialogListener());
55.
56. // ProgressDialog
57. xh_pDialog.show();
58. }
59.
60. });
61.
62. // xhButton02
63. xhButton02.setOnClickListener(new Button.OnClickListener() {
64.
65. @Override
66. public void onClick(View v) {
67.
68. xh_count = 0;
69.
70. // ProgressDialog
71. xh_pDialog = new ProgressDialog(Activity01.this);
72.
73. // , ,
74. xh_pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
75.
76. // ProgressDialog
77. xh_pDialog.setTitle(" ");
78.
79. // ProgressDialog
80. xh_pDialog.setMessage(" ");
81.
82. // ProgressDialog
83. xh_pDialog.setIcon(R.drawable.img2);
84.
85. // ProgressDialog false
86. xh_pDialog.setIndeterminate(false);
87.
88. // ProgressDialog
89. xh_pDialog.setProgress(100);
90.
91. // ProgressDialog
92. xh_pDialog.setCancelable(true);
93.
94. // ProgressDialog
95. xh_pDialog.show();
96.
97. new Thread() {
98. @Override
99. public void run() {
100. try {
101. while (xh_count <= 100) {
102. //
103. xh_pDialog.setProgress(xh_count++);
104. Thread.sleep(100);
105. }
106. xh_pDialog.cancel();
107. } catch (Exception e) {
108. xh_pDialog.cancel();
109. }
110. }
111. }.start();
112.
113. }
114.
115. });
116. }
117.
118. // xhButton01
119. class Bt1DialogListener implements DialogInterface.OnClickListener {
120. @Override
121. public void onClick(DialogInterface dialog, int which) {
122. // “ ”
123. dialog.cancel();
124. }
125. }
126.}