[Android] progressdialog

Source: Internet
Author: User

Package com. Shao. Android;

Import java. util. Random;

Import Android. App. activity;
Import Android. App. progressdialog;
Import Android. content. dialoginterface;
Import Android. OS. Bundle;
Import Android. OS. Handler;
Import Android. OS. message;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;

Public class main extends activity implements onclicklistener {
/** Called when the activity is first created .*/
Private Static final int max_progress = 100;
Private progressdialog;
Private handler progresshandler;
Private int progress;

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Button button1 = (button) findviewbyid (R. Id. btn1 );
Button button2 = (button) findviewbyid (R. Id. btn2 );
Button1.setonclicklistener (this );
Button2.setonclicklistener (this );
}

@ Override
Public void onclick (view v ){
// Todo auto-generated method stub
Switch (V. GETID ())
{
Case R. Id. btn1:
Showprogressdialog (progressdialog. style_horizontal );
Break;
Case R. Id. btn2:
Showprogressdialog (progressdialog. style_spinner );
Break;
}
}
// Display the progress dialog box. style indicates the style of the Progress dialog box.
Private void showprogressdialog (INT style)
{
Progressdialog = new progressdialog (this );
Progressdialog. seticon (R. drawable. Wait );
Progressdialog. settitle ("processing data ...");
Progressdialog. setmessage ("Please wait ...");
// Set the style of the Progress dialog box
Progressdialog. setprogressstyle (style );
// Set the maximum progress value in the Progress dialog box.
Progressdialog. setmax (max_progress );
// Set the pause button in the Progress dialog box
Progressdialog. setbutton ("pause", new dialoginterface. onclicklistener (){


@ Override
Public void onclick (dialoginterface dialog, int whichbutton ){
// Todo auto-generated method stub
// Stop the timer execution by deleting the message code
Progresshandler. removemessages (1 );
// Initial recovery progress Value
Progress = 0;
Progressdialog. setprogress (0 );
}
});
Progressdialog. Show ();
Progresshandler = new handler (){

@ Override
Public void handlemessage (Message MSG ){
// Todo auto-generated method stub
Super. handlemessage (MSG );
// If (progressdialog. getprogress ()> = max_progress)
If (Progress> = max_progress)
{
// The Progress reaches the maximum value. Close the dialog box.
Progress = 0;
Progressdialog. Dismiss ();
}
Else {
Progress ++;
// Increase the progress by 1
Progressdialog. incrementprogressby (1 );
// Set the time for the next increment (call the handlemessage method) at random
Progresshandler. sendemptymessagedelayed (1,50 + new random (). nextint (500 ));

}
}


};

// Set the initial progress Value
Progress = (Progress> 0 )? Progress: 0;
Progressdialog. setprogress (Progress );
Progresshandler. sendemptymessage (1 );
}

}

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.