Progressdialog usage Summary

Source: Internet
Author: User

Use of progressdialog

Progressdialog inherits from alertdialog and alertdialog inherits from dialog to implement the dialoginterface interface.


You can create progressdialog in either the new dialog method or the static dialog. Show () method ().


// Method 1: New dialogfinal progressdialog dialog = new progressdialog (this); dialog. Show ();

// Method 2: Create and display a progress bar in static mode. The progress bar can only be a circular bar. Set the title and message content progressdialog dialog2 = progressdialog. show (this, "prompt", "logging in ");

// Method 3: Use the static method to create and display the progress bar. This progress bar can only be a circular bar. Here, the last vertex count Boolean indeterminate is used to set whether it is an ambiguous status progressdialog dialog3 = progressdialog. show (this, "prompt", "logging in", false );

// Method 4: use static mode to create and display a progress bar. This progress bar can only be a circular bar. Here, the last segment number Boolean cancelable sets whether the progress bar can be canceled. progressdialog dialog4 = progressdialog. show (this, "prompt", "logging in", false, true );


// Method 5: Use the static method to create and display the progress bar. This progress bar can only be a circular bar. Here, the last vertex number dialoginterface. oncancellistener // cancellistener is used to listen on progress bars canceled progressdialog dialog5 = progressdialog. show (this, "prompt", "logging in", true, true, cancellistener );

Method 5 requires a cancellistener. The Code is as follows;

Private oncancellistener cancellistener = new oncancellistener () {@ overridepublic void oncancel (dialoginterface DIALOG) {// todo auto-generated method stubtoast. maketext (mainactivity. this, "progress bar canceled", toast. length_long ). show ();}};



There are two types of progressdialog styles: the circular shape and the horizontal progress bar.


Method 1: 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 progress bar dialog. setcancelable (true); // you can click back to cancel the dialog box. setcanceledontouchoutside (false); // you can specify whether to cancel the dialog progress bar dialog. seticon (R. drawable. ic_launcher); // set the title icon of the prompt. The default value is none. If the title is not set, only setting the icon will not display the dialog of the icon. settitle ("prompt"); // dismiss listens to the dialog. setondismisslistener (New dialoginterface. ondismisslistener () {@ overridepublic void ondismiss (dialoginterface DIALOG) {// todo auto-generated method stub}); // The Listener key event is passed to dialogdialog. setonkeylistener (New dialoginterface. onkeylistener () {@ overridepublic Boolean onkey (dialoginterface dialog, int keycode, keyevent event) {// todo auto-generated method stubreturn false ;}}); // listens to the cancel event dialog. setoncancellistener (New dialoginterface. oncancellistener () {@ overridepublic void oncancel (dialoginterface DIALOG) {// todo auto-generated method stub}); // you can set the clickable button, there are up to three (by default) dialog. setbutton (dialoginterface. button_positive, "OK", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {// todo auto-generated method stub}); dialog. setbutton (dialoginterface. button_negative, "cancel", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {// todo auto-generated method stub}); dialog. setbutton (dialoginterface. button_neutral, "neutral", new dialoginterface. onclicklistener () {@ overridepublic 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 () {@ overridepublic void run () {// todo auto-generated method stubtry {thread. sleep (5000); // The cancel and dismiss methods are essentially the same. They all Delete the dialog from the screen. The only difference is that // calls the Cancel Method to callback dialoginterface. if the oncancellistener is added to the listener, The dismiss method will not return the dialog. cancel (); // dialog. dismiss ();} catch (interruptedexception e) {// todo auto-generated catch blocke. printstacktrace ();}}}). start ();

Use thread. Sleep (5000) to simulate background operations.

The cancel and dismiss methods are essentially the same. They are all used to delete the dialog from the screen. The only difference is that when the cancel method is called, The dialoginterface. oncancellistener is called and the dismiss method will not return.


Another way: horizontal progress bar

// The progress bar also has the second-level progress bar. The final progressdialog dialog = new progressdialog (this); dialog is not demonstrated here. setprogressstyle (progressdialog. style_horizontal); // sets the horizontal progress bar dialog. setcancelable (true); // you can click back to cancel the dialog box. setcanceledontouchoutside (false); // you can specify whether to cancel the dialog progress bar dialog. seticon (R. drawable. ic_launcher); // set the title icon of the prompt. The default value is no dialog. settitle ("prompt"); dialog. setmax (100); dialog. setbutton (dialoginterface. button_positive, "OK", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {// todo auto-generated method stub}); dialog. setbutton (dialoginterface. button_negative, "cancel", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {// todo auto-generated method stub}); dialog. setbutton (dialoginterface. button_neutral, "neutral", new dialoginterface. onclicklistener () {@ overridepublic 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 () {@ overridepublic void run () {// todo auto-generated method stubint I = 0; while (I <100) {try {thread. sleep (200); // update the progress bar, which can be used to update the progress bar dialog in the Child thread. incrementprogressby (1); // dialog. incrementsecondaryprogressby (10) // second-level progress bar update method I ++;} catch (exception e) {// todo: handle exception }}// Delete dialogdialog when the progress bar is complete. dismiss ();}}). start ();


Many others use their own progressdialog to meet different display needs.


 

Progressdialog usage Summary

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.