Android Study Notes (9)-more complex progress dialog box

Source: Internet
Author: User

Show operation progress dialog box


1. Use the same project created in the previous article to add a Button in the activity_main.xml file:

    

2. Add the specific progress bar implementation code to the MainActivity. java file:

First, add the onClick3 () method:

Public void onClick3 (View v) {showDialog (1); // id is 1. When the callback method onCreateDialog () is called, the id is passed in so that case 1 is selected. ProgressDialog. setProgress (0); // new Thread (new Runnable () {@ Overridepublic void run () {// TODO Auto-generated method stubfor (int I = 1; I <= 10; I ++) {try {Thread. sleep (1000); progressDialog. incrementProgressBy (100/10); // The step is 10} catch (InterruptedException e) {// TODO: handle implements tione. printStackTrace () ;}} progressDialog. dismiss (); // destruction dialog box }}). start ();}
Then add the Code with id = 1 in the onCreateDialog () callback method (that is, the code used to display the operation progress dialog box ):

Case 1: progressDialog = new ProgressDialog (this); progressDialog. setIcon (R. drawable. ic_launcher); progressDialog. setTitle ("Downloading files... "); progressDialog. setProgressStyle (ProgressDialog. STYLE_HORIZONTAL); // sets the progress bar style progressDialog. setButton (DialogInterface. BUTTON_POSITIVE, "OK", // set the OK button to new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {// Todd O Auto-generated method stubToast. makeText (getBaseContext (), "OK clicked! ", Toast. LENGTH_SHORT ). show () ;}}); progressDialog. setButton (DialogInterface. BUTTON_NEGATIVE, "Cancel", // sets the Cancel button new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stubToast. makeText (getBaseContext (), "Cancel clicked! ", Toast. LENGTH_SHORT). show () ;}}); return progressDialog;

3. Run the command. The result is as follows:

Download the complete code ~

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.