Android Development Series (27th): Use ProgressDialog to create a progress dialog box

Source: Internet
Author: User

Android Development Series (27th): Use ProgressDialog to create a progress dialog box

The progress dialog box is common in common applications, such as downloading, opening a page, transferring a file, and so on. It has a circular shape and a long shape. These two types

You can create a progress dialog box in either of the following ways:

1. Create a ProgressDialog instance and call the static show () method provided by ProgressDialog to display the instance.

2. Create a ProgressDialog instance, call the method to set the progress bar, and display it.

 

The progress bar can be divided into two types: the first is the display progress, and the second is the display progress.

In the following example, we will see three types of progress bars: The Ring progress bar, the progress bar, and the progress bar.

 

First, create an Android project and write the main. xml file:

 

Three buttons are defined here. The three buttons correspond to the corresponding onClick attribute.

 

Next, ProgressDialogTest. java:

 

Package org. crazyit. ui; import android. app. activity; import android. app. progressDialog; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. view. view; public class ProgressDialogTest extends Activity {final static int MAX_PROGRESS = 100; // This program simulates an array with a length of 100 private int [] data = new int [50]; // progress record Dialog Box Completion percentage int progressStatus = 0; int hasData = 0; ProgressDialog pd1, pd2; // Define a HandlerHandler handler that is responsible for updating the progress = new Handler () {@ Overridepublic void handleMessage (Message msg) {// indicates that the Message is sent by the program. If (msg. what = 0x123) {pd2.setProgress (progressStatus) ;}};@ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main);} public void showSpinner (View source) {// call the static method to display the circular progress bar ProgressDialog. show (this, "task in progress", "task in progress, please wait", false, true); // ①} public void showIndeterminate (View source) {pd1 = new ProgressDialog (ProgressDialogTest. this); // set the Dialog The title of the box pd1.setTitle ("the task is being executed"); // The content displayed in the Setting Dialog Box pd1.setMessage ("the task is being executed. Please wait... "); // In the Setting dialog box, you can use the" cancel "button to close pd1.setCancelable (true); // you can set the progress bar style pd1.setProgressStyle (ProgressDialog. STYLE_HORIZONTAL); // set whether the progress bar in the dialog box displays pd1.setIndeterminate (true); pd1.show (); // ②} public void showProgress (View source) {// reset the progress bar to 0 progressStatus = 0; // refill the array. HasData = 0; pd2 = new ProgressDialog (ProgressDialogTest. this); pd2.setMax (MAX_PROGRESS); // set the title of the dialog box pd2.setTitle ("task completion percentage "); // set the content displayed in the dialog box pd2.setMessage ("percentage of completed time-consuming tasks"); // The "cancel" button cannot be used to close pd2.setCancelable (false ); // set the progress bar style pd2.setProgressStyle (ProgressDialog. STYLE_HORIZONTAL); // set whether the progress bar in the dialog box shows pd2.setIndeterminate (false); pd2.show (); // ③ new Thread () {public void run () {while (progressStatus <MAX_PROG RESS) {// get the percentage of completed time-consuming operations progressStatus = MAX_PROGRESS * doWork ()/data. length; // send an empty message to Handlerhandler. sendEmptyMessage (0x123);} // if the task has been completed if (progressStatus >=max_progress) {// close the dialog box pd2.dismiss ();}}}. start () ;}// simulate a time-consuming operation. Public int doWork () {// assign data [hasData ++] = (int) (Math. random () * 100); try {Thread. sleep (100);} catch (InterruptedException e) {e. printStackTrace () ;}return hasData ;}}
In the third step: display the progress bar, we use the Thread to simulate a time-consuming operation.

 

 

This is:

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.