2013年8月11日星期日

android dialog progress bar (ProgressDialog)

 

from: http://byandby.iteye.com/blog/817214

 

progress bar dialog to define clearly necessary to use ProgressDialog, first of all we need to create ProgressDialog object, of course, here also uses threads to control the progress of the show, in addition you can use the following methods to set ProgressDialog.
setProgressStyle: Setting progress bar style, the style is round, rotating.
setTitlt: Set ProgressDialog title
setMessage: Set ProgressDialog message;
setIcon: Set ProgressDialog title icon;
setIndeterminate: Set ProgressDialog progress bar is not clear;
setCancelable: Set ProgressDialog can press the return key to cancel;
setButton: ProgressDialog set a Button (need to listen Button event);
show: Show ProgressDialog.

First, let us look at the effect it run





Here take a look at the layout file


< span> android: orientation = "vertical"

android: layout_width = "fill_parent" < br /> android: layout_height = "fill_parent"
>

android: layout_width = "fill_parent"
android: layout_height = "wrap_content"
android: text = "@ string / hello"
/>

android: id = "@ + id/Button01 "
android: layout_width =" wrap_content "
android: layout_height =" wrap_content "
android: text = "circular progress bar" />

< br /> android: id = "@ + id/Button02"
android: layout_width = "wrap_content"
< span> android: layout_height = "wrap_content"

android: text = "elongated progress bar" />


Activity01

 
  
   
    Java code         
  
  
       
          
    1. package xiaohang.zhimeng;
    2.     
    3.     
    4. import android.app.Activity;
    5.     
    6. import android.app.ProgressDialog;
    7.     
    8. import android.content.DialogInterface;
    9.     
    10. import android.os.Bundle;
    11.     
    12. import android.view.View;
    13.     
    14. import android.view.View.OnClickListener;
    15.     
    16. import android.widget.Button;
    17.     
    18.     
    19. public class Activity01 extends Activity {
    20.     
    21.     
    22. private Button xhButton01, xhButton02;
    23.     
    24.     
    25. int xh_count = 0 ;
    26.     
    27. / / declare the progress bar dialog
    28.     
    29. ProgressDialog xh_pDialog;
    30.     
    31.     
    32. @ Override
    33.     
    34. public void onCreate (Bundle savedInstanceState) {
    35.     
    36. super . onCreate (savedInstanceState); < / li>     
    37. setContentView (R.layout.main);
    38.     
    39.     
    40. / / get the button object < / li>     
    41. xhButton01 = (Button) findViewById (R.id.Button01); < / li>     
    42. xhButton02 = (Button) findViewById (R.id.Button02); < / li>     
    43.     
    44. / / Set xhButton01 event listener
    45.     
    46. xhButton01.setOnClickListener ( new OnClickListener () { ;
    47.     
    48. @ Override
    49.     
    50. public void onClick (View v) {
    51.     
    52. / / Create ProgressDialog object
    53.     
    54. xh_pDialog = new ProgressDialog (Activity01. this );
    55.     
    56.     
    57. / / Set the progress bar style, it is round, rotating
    58.     
    59. xh_pDialog.setProgressStyle (ProgressDialog.STYLE_SPINNER); ;
    60.     
    61.     
    62. / / Set ProgressDialog title
    63.     
    64. xh_pDialog.setTitle ( "Prompt" );
    65.     
    66.     
    67. / / Set ProgressDialog message
    68.     
    69. xh_pDialog.setMessage ( "This is a circular progress bar dialog" );
    70.     
    71.     
    72. / / Set ProgressDialog Title Icon
    73.     
    74. xh_pDialog.setIcon (R.drawable.img1); ;
    75.     
    76.     
    77. / / Set ProgressDialog is not clear whether the progress bar is not set to false is not clear
    78.     
    79. xh_pDialog.setIndeterminate ( false );
    80.     
    81.     
    82. / / Set ProgressDialog can press the return key to cancel
    83.     
    84. xh_pDialog.setCancelable ( true );
    85.     
    86.     
    87. / / Set a ProgressDialog Button
    88.     
    89. xh_pDialog.setButton ( "OK" , new Bt1DialogListener ());
    90.     
    91.     
    92. / / Let ProgressDialog show
    93.     
    94. xh_pDialog.show ();
    95.     
    96. }
    97.     
    98.     
    99. });
    100.     
    101.     
    102. / / Set xhButton02 event listener
    103.     
    104. xhButton02.setOnClickListener ( new Button.OnClickListener () { ;
    105.     
    106.     
    107. @ Override
    108.     
    109. public void onClick (View v) {
    110.     
    111.     
    112. xh_count = 0 ;
    113.     
    114.     
    115. / / Create ProgressDialog object
    116.     
    117. xh_pDialog = new ProgressDialog (Activity01. this );
    118.     
    119.     
    120. / / Set the progress bar style, it is round, rotating
    121.     
    122. xh_pDialog.setProgressStyle (ProgressDialog.STYLE_HORIZONTAL); ;
    123.     
    124.     
    125. / / Set ProgressDialog title
    126.     
    127. xh_pDialog.setTitle ( "Prompt" );
    128.     
    129.     
    130. / / Set ProgressDialog message
    131.     
    132. xh_pDialog.setMessage ( "This is a long-shaped progress bar dialog" );
    133.     
    134.     
    135. / / Set ProgressDialog Title Icon
    136.     
    137. xh_pDialog.setIcon (R.drawable.img2); ;
    138.     
    139.     
    140. / / Set ProgressDialog is not clear whether the progress bar is not set to false is not clear
    141.     
    142. xh_pDialog.setIndeterminate ( false );
    143.     
    144.     
    145. / / Set ProgressDialog Progress bar
    146.     
    147. xh_pDialog.setProgress ( 100 );
    148.     
    149.     
    150. / / Set ProgressDialog can press the return key to cancel
    151.     
    152. xh_pDialog.setCancelable ( true );
    153.     
    154.     
    155. / / Let ProgressDialog show
    156.     
    157. xh_pDialog.show ();
    158.     
    159.     
    160. new Thread () {
    161.     
    162. @ Override
    163.     
    164. public void run () {
    165.     
    166. try {
    167.     
    168. while (xh_count <= 100 ) ; {
    169.     
    170. / / by the thread to control the progress < / li>     
    171. xh_pDialog.setProgress (xh_count + +);
    172.     
    173. Thread.sleep ( 100 );
    174.     
    175. }
    176.     
    177. xh_pDialog.cancel ();
    178.     
    179. } catch (Exception e) {
    180.     
    181. xh_pDialog.cancel ();
    182.     
    183. }
    184.     
    185. } ;
    186.     
    187. }. start ();
    188.     
    189.     
    190. }
    191.     
    192.     
    193. });
    194.     
    195. }
    196.     
    197.     
    198. / / xhButton01 listener class
    199.     
    200. class Bt1DialogListener implements DialogInterface.OnClickListener {
    201.     
    202. @ Override
    203.     
    204. public void onClick (DialogInterface dialog, int which) {
    205.     
    206. / / Click the "OK" button to cancel the dialog
    207.     
    208. dialog.cancel ();
    209.     
    210. }
    211.     
    212. }
    213.     
    214. }
    215.    
      
 

没有评论:

发表评论