Article source: blog.csdn.net/caesardadi/…

The use of the ProgressDialog

ProgressDialog inherits from AlertDialog, and AlertDialog inherits from Dialog and implements DialogInterface.

\

ProgressDialog is created either as a new Dialog or by calling Dialog’s static method dialog.show ().

\

      

[java]  view plain copy

  1. // New Dialog
  2.     final ProgressDialog dialog = new ProgressDialog(this);  
  3.     dialog.show();  

\

[java]  view plain copy

  1. // Mode 2: use static mode to create and display the progress bar, which can only be a circular bar, set the title and Message prompt content
  2. ProgressDialog dialog2 = progressdialog. show(this, “”,” “);

\

[java]  view plain copy

  1. // The progress bar can only be a circular bar. Boolean indeterminate is set to an undefined state
  2.     ProgressDialog dialog3 = ProgressDialog  
  3. .show(this, “prompt “,” logging in “, false);

\

[java]  view plain copy

  1. The last Boolean cancelable parameter sets whether the progress bar can be cancelled
  2. ProgressDialog dialog4 = progressdialog. show(this, “新 “,” 新 “,
  3.             false, true);  

\

[java]  view plain copy

  1. / / way five using static methods to create and display, this article progress bar can be round, here the last parameter DialogInterface. OnCancelListener
  2. CancelListener Is used to listen for the progress bar to be cancelled
  3. ProgressDialog dialog5 = progressdialog. show(this, “新 “,” 新 “, true,
  4.             true, cancelListener);  

Method 5 requires a cancelListener with the following code; \

[java]  view plain copy

  1. private OnCancelListener cancelListener = new OnCancelListener() {  
  2.   
  3.     @Override  
  4.     public void onCancel(DialogInterface dialog) {  
  5.         // TODO Auto-generated method stub  
  6. Toast.maketext (mainactivity. this, “Progress bar cancelled “, toast.length_long)
  7.                 .show();  
  8.   
  9.     }  
  10. };  

The ProgressDialog has two styles, a circular undefined state and a horizontal progress bar state

\

The first way: circular progress bar

[java]  view plain copy

  1. final ProgressDialog dialog = new ProgressDialog(this);  
  2. dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); // Set the progress bar to a circular progress bar
  3. dialog.setCancelable(true); // Set whether you can cancel by clicking the Back key
  4. dialog.setCanceledOnTouchOutside(false); // Sets whether to cancel the Dialog progress bar after clicking Dialog
  5. dialog.setIcon(R.drawable.ic_launcher); //
  6. // Set the Icon of the prompt title, default is not set, if the title is not set only Icon will not display the Icon
  7. Dialog. SetTitle (” tip “);
  8. / / dismiss listening in
  9.         dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {  
  10.   
  11.             @Override  
  12.             public void onDismiss(DialogInterface dialog) {  
  13.                 // TODO Auto-generated method stub  
  14.   
  15.             }  
  16.         });  
  17. // The listener Key event is passed to the dialog
  18.         dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {  
  19.   
  20.             @Override  
  21.             public boolean onKey(DialogInterface dialog, int keyCode,  
  22.                     KeyEvent event) {  
  23.                 // TODO Auto-generated method stub  
  24.                 return false;  
  25.             }  
  26.         });  
  27. // Listen for the Cancel event
  28.         dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {  
  29.   
  30.             @Override  
  31.             public void onCancel(DialogInterface dialog) {  
  32.                 // TODO Auto-generated method stub  
  33.   
  34.             }  
  35.         });  
  36. // Set clickable buttons, up to three (default)
  37. Dialog. setButton(DialogInterface.BUTTON_POSITIVE, “confirm “,
  38.                 new DialogInterface.OnClickListener() {  
  39.   
  40.                     @Override  
  41.                     public void onClick(DialogInterface dialog, int which) {  
  42.                         // TODO Auto-generated method stub  
  43.   
  44.                     }  
  45.                 });  
  46. Dialog. setButton(DialogInterface.BUTTON_NEGATIVE, “Cancel “,
  47.                 new DialogInterface.OnClickListener() {  
  48.   
  49.                     @Override  
  50.                     public void onClick(DialogInterface dialog, int which) {  
  51.                         // TODO Auto-generated method stub  
  52.   
  53.                     }  
  54.                 });  
  55. Dialog. setButton(DialogInterface.BUTTON_NEUTRAL,
  56.                 new DialogInterface.OnClickListener() {  
  57.   
  58.                     @Override  
  59.                     public void onClick(DialogInterface dialog, int which) {  
  60.                         // TODO Auto-generated method stub  
  61.   
  62.                     }  
  63.                 });  
  64. Dialog.setmessage (” This is a circular progress bar “);
  65.         dialog.show();  
  66.         new Thread(new Runnable() {  
  67.   
  68.             @Override  
  69.             public void run() {  
  70.                 // TODO Auto-generated method stub  
  71.                 try {  
  72.                     Thread.sleep(5000);  
  73. The cancel and dismiss methods are essentially the same, removing the Dialog from the screen, with the only difference
  74. / / call the cancel method callback DialogInterface. OnCancelListener if register, dismiss method doesn’t back off
  75.                     dialog.cancel();  
  76.                     // dialog.dismiss();  
  77.                 } catch (InterruptedException e) {  
  78.                     // TODO Auto-generated catch block  
  79.                     e.printStackTrace();  
  80.                 }  
  81.   
  82.             }  
  83.         }).start();  

Thread. Sleep (5000) is used to simulate the background operation.

Cancel and dismiss the essence is the same, both are removed from the screen Dialog, the only difference is: call the cancel method will callback DialogInterface. OnCancelListener if register, dismiss method doesn’t back off. \

\

Second way: horizontal progress bar

[java]  view plain copy

  1. // The progress bar has the form of a secondary progress bar, which is not demonstrated here
  2.     final ProgressDialog dialog = new ProgressDialog(this);  
  3. dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); // Set the horizontal progress bar
  4. dialog.setCancelable(true); // Set whether you can cancel by clicking the Back key
  5. dialog.setCanceledOnTouchOutside(false); // Sets whether to cancel the Dialog progress bar after clicking Dialog
  6. dialog.setIcon(R.drawable.ic_launcher); // Set the icon for the prompt title, which is not available by default
  7. Dialog. SetTitle (” tip “);
  8.     dialog.setMax(100);  
  9. Dialog. setButton(DialogInterface.BUTTON_POSITIVE, “confirm “,
  10.             new DialogInterface.OnClickListener() {  
  11.   
  12.                 @Override  
  13.                 public void onClick(DialogInterface dialog, int which) {  
  14.                     // TODO Auto-generated method stub  
  15.   
  16.                 }  
  17.             });  
  18. Dialog. setButton(DialogInterface.BUTTON_NEGATIVE, “Cancel “,
  19.             new DialogInterface.OnClickListener() {  
  20.   
  21.                 @Override  
  22.                 public void onClick(DialogInterface dialog, int which) {  
  23.                     // TODO Auto-generated method stub  
  24.   
  25.                 }  
  26.             });  
  27. Dialog. setButton(DialogInterface.BUTTON_NEUTRAL,
  28.             new DialogInterface.OnClickListener() {  
  29.   
  30.                 @Override  
  31.                 public void onClick(DialogInterface dialog, int which) {  
  32.                     // TODO Auto-generated method stub  
  33.   
  34.                 }  
  35.             });  
  36. Dialog.setmessage (” This is a horizontal progress bar “);
  37.     dialog.show();  
  38.     new Thread(new Runnable() {  
  39.   
  40.         @Override  
  41.         public void run() {  
  42.             // TODO Auto-generated method stub  
  43.             int i = 0;  
  44.             while (i < 100) {  
  45.                 try {  
  46.                     Thread.sleep(200);  
  47. // Update the progress bar progress. You can update the progress bar progress in child threads
  48.                     dialog.incrementProgressBy(1);  
  49. / / / / dialog. IncrementSecondaryProgressBy (10) secondary progress bar update way
  50.                     i++;  
  51.   
  52.                 } catch (Exception e) {  
  53.                     // TODO: handle exception  
  54.                 }  
  55.             }  
  56. // Delete Dialog when the progress bar runs out
  57.             dialog.dismiss();  
  58.   
  59.         }  
  60.     }).start();  


\

More of a custom ProgressDialog is used to meet the needs of different displays.

\