Android Summary of ProgressDialog Usage

  • 2021-01-22 05:22:31
  • OfStack

ProgressDialog inherits from AlertDialog, and AlertDialog inherits from Dialog, implementing the DialogInterface interface.

ProgressDialog can be created either by new Dialog or by calling Dialog's static method Dialog.show ().


//  way 1 : new Dialog 
final ProgressDialog dialog = new ProgressDialog(this); 
dialog.show(); 
//  way 2 : Statically created and displayed, this progress bar can only be a circular bar , Set up the title and Message Prompt content  
ProgressDialog dialog2 = ProgressDialog.show(this, " prompt ", " On the way "); 
//  way 3  When created and displayed statically, the progress bar can only be a circular bar , Here is the final 1 A parameter boolean indeterminate Sets whether the state is ambiguous  
ProgressDialog dialog3 = ProgressDialog 
.show(this, " prompt ", " On the way ", false); 
//  way 4  When created and displayed statically, the progress bar can only be a circular bar , Here is the final 1 A parameter boolean cancelable  Sets whether the progress bar is cancellable  
ProgressDialog dialog4 = ProgressDialog.show(this, " prompt ", " On the way ", 
false, true); 
//  way 5  When created and displayed statically, the progress bar can only be a circular bar , Here is the final 1 A parameter  DialogInterface.OnCancelListener 
// cancelListener The progress bar used to monitor progress has been cancelled  
ProgressDialog dialog5 = ProgressDialog.show(this, " prompt ", " On the way ", true, 
true, cancelListener); 

One cancelListener is required in Option 5, and the code is as follows.


private OnCancelListener cancelListener = new OnCancelListener() { 
@Override 
public void onCancel(DialogInterface dialog) { 
// TODO Auto-generated method stub 
Toast.makeText(MainActivity.this, " The progress bar has been cancelled ", Toast.LENGTH_LONG) 
.show(); 
} 
}; 

ProgressDialog has two styles, one is a circular ambiguous state, one is a horizontal progress bar state

The first way: circular progress bar


final ProgressDialog dialog = new ProgressDialog(this); 
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);//  Sets the form of the progress bar to be a circular rotating progress bar  
dialog.setCancelable(true);//  Sets whether it can be clicked Back Key to cancel  
dialog.setCanceledOnTouchOutside(false);//  Settings in click Dialog Whether to cancel or not Dialog The progress bar  
dialog.setIcon(R.drawable.ic_launcher);// 
//  Set prompt title The icon, by default, is not set if not title If you just set it Icon The icon is not displayed  
dialog.setTitle(" prompt "); 
// dismiss Listening to the  
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() { 
@Override 
public void onDismiss(DialogInterface dialog) { 
// TODO Auto-generated method stub 
} 
}); 
//  Listening to the Key The event is passed to dialog 
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() { 
@Override 
public boolean onKey(DialogInterface dialog, int keyCode, 
KeyEvent event) { 
// TODO Auto-generated method stub 
return false; 
} 
}); 
//  Listening to the cancel The event  
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() { 
@Override 
public void onCancel(DialogInterface dialog) { 
// TODO Auto-generated method stub 
} 
}); 
// Sets the number of clickable buttons, up to one 3 a ( By default ) 
dialog.setButton(DialogInterface.BUTTON_POSITIVE, " determine ", 
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, " neutral ", 
new DialogInterface.OnClickListener() { 
@Override 
public void onClick(DialogInterface dialog, int which) { 
// TODO Auto-generated method stub 
} 
}); 
dialog.setMessage(" This is a 1 A round progress bar "); 
dialog.show(); 
new Thread(new Runnable() { 
@Override 
public void run() { 
// TODO Auto-generated method stub 
try { 
Thread.sleep(5000); 
// cancel and dismiss The method is essentially 1 All of these are deleted from the screen Dialog, only 1 Is the difference between the  
//  call cancel Method is called back DialogInterface.OnCancelListener If you sign up ,dismiss Method does not fall back  
dialog.cancel(); 
// dialog.dismiss(); 
} catch (InterruptedException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 
} 
}).start(); 

Thread.sleep(5000) is used to simulate background operation.

Both cancel and dismiss methods are essentially 1, deleting Dialog from the screen. The only difference is that calling cancel will call back DialogInterface.OnCancelListener will not drop back dismiss if registered.

Method 2: Horizontal progress bar


//  The progress bar also has 2 The form of a level progress bar is not shown here  
final ProgressDialog dialog = new ProgressDialog(this); 
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//  Set the horizontal progress bar  
dialog.setCancelable(true);//  Sets whether it can be clicked Back Key to cancel  
dialog.setCanceledOnTouchOutside(false);//  Settings in click Dialog Whether to cancel or not Dialog The progress bar  
dialog.setIcon(R.drawable.ic_launcher);//  Set prompt title The default is none  
dialog.setTitle(" prompt "); 
dialog.setMax(100); 
dialog.setButton(DialogInterface.BUTTON_POSITIVE, " determine ", 
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, " neutral ", 
new DialogInterface.OnClickListener() { 
@Override 
public void onClick(DialogInterface dialog, int which) { 
// TODO Auto-generated method stub 
} 
}); 
dialog.setMessage(" This is a 1 Horizontal progress bars "); 
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 of the progress bar , The progress bar progress can be updated in child threads  
dialog.incrementProgressBy(1); 
// dialog.incrementSecondaryProgressBy(10)//2 Level progress bar update mode  
i++; 
} catch (Exception e) { 
// TODO: handle exception 
} 
} 
//  Delete the progress bar when it runs out Dialog 
dialog.dismiss(); 
} 
}).start(); 

About Android ProgressDialog use summary of the relevant knowledge is introduced here, the follow-up will continue to update, please continue to pay attention to this site, thank you!


Related articles: