Android ProgressDialog Usage Summary _android

Source: Internet
Author: User
Tags call back stub

ProgressDialog inherits from Alertdialog,alertdialog inherits from dialog, implements Dialoginterface interface.

There are two ways to create a progressdialog, one is the new Dialog, and one is the static method Dialog.show () that calls Dialog.

Mode one: New Dialog 
final ProgressDialog Dialog = new ProgressDialog (this); 
Dialog.show (); 
Mode two: Create and display in a static way, this progress bar can only be a circle bar, set title and message hint content 
ProgressDialog dialog2 = Progressdialog.show (this, "hint", " is in the process of landing "); 
Mode three uses a static method to create and show that the progress bar can only be a circular bar, where the last parameter Boolean indeterminate setting is ambiguous state 
progressdialog dialog3 = ProgressDialog 
. Show (This, "hint", "in-landing", false); 
Mode four creates and displays in a static way that the progress bar can only be a circular bar, where the last parameter Boolean cancelable sets whether the progress bar can be canceled 
progressdialog dialog4 = Progressdialog.show (This, "prompt", "landing in", 
false, true); 
Mode five creates and displays in a static way, this progress bar can only be a circular bar, where the last parameter Dialoginterface.oncancellistener 
//Cancellistener is used to monitor the progress bar being canceled 
ProgressDialog Dialog5 = Progressdialog.show (This, "prompt", "landing in", true, 

The way v needs a cancellistener, 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, "Progress bar is canceled", Toast.length_long) 
. Show (); 
 

There are two types of progressdialog style, one is the circular ambiguous state, the other is the horizontal progress bar state

The first way: circular progress bar

Final ProgressDialog Dialog = new ProgressDialog (this); Dialog.setprogressstyle (Progressdialog.style_spinner);//Set the progress bar in the form of a circular rotation of the progress bar dialog.setcancelable (true); Set whether you can cancel Dialog.setcanceledontouchoutside (false) by clicking the back key, or whether to cancel the dialog progress bar Dialog.seticon when clicking Dialog ( Dismiss listening Dialog.setondismisslistener (new Dialoginterface.ondismisslistener () {@Override public void Ondismiss (Dia 
Loginterface dialog) {//TODO auto-generated Method stub}}); The Listener key event is passed to dialog dialog.setonkeylistener (new Dialoginterface.onkeylistener () {@Override public boolean onkey (Dial 
Oginterface dialog, int keycode, keyevent event) {//TODO auto-generated method stub return false; 
} 
}); Listens for cancel event Dialog.setoncancellistener (new Dialoginterface.oncancellistener () {@Override public void OnCancel (Dialo 
Ginterface dialog) {//TODO auto-generated Method stub}}); Set a clickable button with a maximum of three (By default) Dialog.setbutton (Dialoginterface.button_positive, OK, new Dialoginterface.onclicklistener () {@Override Publ 
IC 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 O 
Nclick (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 dialog from the screen, and the only difference is that calling the Cancel method will call back Dialoginterface.oncancellistener if you register, 
Dismiss method does not return Dialog.cancel (); 
Dialog.dismiss (); catch (interruptedexceptIon e) {//TODO auto-generated catch block E.printstacktrace ();  }}). Start ();

The background operation is simulated by Thread.Sleep (5000).

The Cancel and dismiss methods are essentially the same, removing dialog from the screen, the only difference being that calling the Cancel method will callback the Dialoginterface.oncancellistener if registered, The dismiss method will not be returned.

The second way: horizontal progress bar

The progress bar also has the form of a level two progress bar, which does not demonstrate the final progressdialog dialog = new ProgressDialog (this); Dialog.setprogressstyle (progressdialog.style_horizontal);/Set horizontal progress bar dialog.setcancelable (true); Set whether you can cancel Dialog.setcanceledontouchoutside (false) by clicking the back key, or whether to cancel the dialog progress bar Dialog.seticon when clicking Dialog ( 
R.drawable.ic_launcher)//Set the icon for the title of the hint, default is no Dialog.settitle ("hint"); 
Dialog.setmax (100); Dialog.setbutton (dialoginterface.button_positive, OK, 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 O Nclick (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 < m) {try {thread.sleep (200); 
Update progress bar Progress, you can update progress bar progress in child threads Dialog.incrementprogressby (1); 
Dialog.incrementsecondaryprogressby (10)//two level progress bar update mode i++; 
catch (Exception e) {//Todo:handle Exception}}//Delete dialog Dialog.dismiss () at the end of the progress bar;  }). Start ();

About the use of the Android ProgressDialog summary of the relevant knowledge to introduce to you here, follow-up will continue to update, please continue to pay attention to cloud Habitat community website, thank you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.