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
- // New Dialog
- final ProgressDialog dialog = new ProgressDialog(this);
- dialog.show();
\
[java] view plain copy
- // 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
- ProgressDialog dialog2 = progressdialog. show(this, “”,” “);
\
[java] view plain copy
- // The progress bar can only be a circular bar. Boolean indeterminate is set to an undefined state
- ProgressDialog dialog3 = ProgressDialog
- .show(this, “prompt “,” logging in “, false);
\
[java] view plain copy
- The last Boolean cancelable parameter sets whether the progress bar can be cancelled
- ProgressDialog dialog4 = progressdialog. show(this, “新 “,” 新 “,
- false, true);
\
[java] view plain copy
- / / way five using static methods to create and display, this article progress bar can be round, here the last parameter DialogInterface. OnCancelListener
- CancelListener Is used to listen for the progress bar to be cancelled
- ProgressDialog dialog5 = progressdialog. show(this, “新 “,” 新 “, true,
- true, cancelListener);
Method 5 requires a cancelListener with the following code; \
[java] view plain copy
- private OnCancelListener cancelListener = new OnCancelListener() {
- @Override
- public void onCancel(DialogInterface dialog) {
- // TODO Auto-generated method stub
- Toast.maketext (mainactivity. this, “Progress bar cancelled “, toast.length_long)
- .show();
- }
- };
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
- final ProgressDialog dialog = new ProgressDialog(this);
- dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); // Set the progress bar to a circular progress bar
- dialog.setCancelable(true); // Set whether you can cancel by clicking the Back key
- dialog.setCanceledOnTouchOutside(false); // Sets whether to cancel the Dialog progress bar after clicking Dialog
- dialog.setIcon(R.drawable.ic_launcher); //
- // Set the Icon of the prompt title, default is not set, if the title is not set only Icon will not display the Icon
- Dialog. SetTitle (” tip “);
- / / dismiss listening in
- dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
- @Override
- public void onDismiss(DialogInterface dialog) {
- // TODO Auto-generated method stub
- }
- });
- // The listener Key event is passed to the dialog
- dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
- @Override
- public boolean onKey(DialogInterface dialog, int keyCode,
- KeyEvent event) {
- // TODO Auto-generated method stub
- return false;
- }
- });
- // Listen for the Cancel event
- dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
- @Override
- public void onCancel(DialogInterface dialog) {
- // TODO Auto-generated method stub
- }
- });
- // Set clickable buttons, up to three (default)
- Dialog. setButton(DialogInterface.BUTTON_POSITIVE, “confirm “,
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- }
- });
- Dialog. setButton(DialogInterface.BUTTON_NEGATIVE, “Cancel “,
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- }
- });
- Dialog. setButton(DialogInterface.BUTTON_NEUTRAL,
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- }
- });
- Dialog.setmessage (” This is a circular progress bar “);
- dialog.show();
- new Thread(new Runnable() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- try {
- Thread.sleep(5000);
- The cancel and dismiss methods are essentially the same, removing the Dialog from the screen, with the only difference
- / / call the cancel method callback DialogInterface. OnCancelListener if register, dismiss method doesn’t back off
- dialog.cancel();
- // dialog.dismiss();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }).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
- // The progress bar has the form of a secondary progress bar, which is not demonstrated here
- final ProgressDialog dialog = new ProgressDialog(this);
- dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); // Set the horizontal progress bar
- dialog.setCancelable(true); // Set whether you can cancel by clicking the Back key
- dialog.setCanceledOnTouchOutside(false); // Sets whether to cancel the Dialog progress bar after clicking Dialog
- dialog.setIcon(R.drawable.ic_launcher); // Set the icon for the prompt title, which is not available by default
- Dialog. SetTitle (” tip “);
- dialog.setMax(100);
- Dialog. setButton(DialogInterface.BUTTON_POSITIVE, “confirm “,
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- }
- });
- Dialog. setButton(DialogInterface.BUTTON_NEGATIVE, “Cancel “,
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- }
- });
- Dialog. setButton(DialogInterface.BUTTON_NEUTRAL,
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- }
- });
- Dialog.setmessage (” This is a horizontal progress bar “);
- dialog.show();
- new Thread(new Runnable() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- int i = 0;
- while (i < 100) {
- try {
- Thread.sleep(200);
- // Update the progress bar progress. You can update the progress bar progress in child threads
- dialog.incrementProgressBy(1);
- / / / / dialog. IncrementSecondaryProgressBy (10) secondary progress bar update way
- i++;
- } catch (Exception e) {
- // TODO: handle exception
- }
- }
- // Delete Dialog when the progress bar runs out
- dialog.dismiss();
- }
- }).start();
\
More of a custom ProgressDialog is used to meet the needs of different displays.
\