Progress dialogs are common in normal applications, such as when downloading, when opening pages, transferring files, and so on. Having a circular, elongated shape. Basically, these two kinds of
There are two ways to create a progress dialog box:
1. Create the ProgressDialog instance, and then call the static Show () method provided by ProgressDialog to display the instance.
2. Create the ProgressDialog instance, and then call the method to set the progress bar and then display it.
The bar bar is also divided into two types, the first is to show progress, the second is not show progress.
In the following example, we will see three types: a loop progress bar, a bar bar showing progress, a bar bar that does not show progress
First, we create an Android project and then write the Main.xml file:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayoutxmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" vertical "android:layout_width=" match_parent "android:layout_height=" Match_parent " Android:gravity= "Center_horizontal" ><buttonandroid:layout_width= "Match_parent" android:layout_height= " Wrap_content "android:text=" Ring progress Bar "android:onclick=" Showspinner "/><buttonandroid:layout_width=" Match_ Parent "android:layout_height=" wrap_content "android:text=" does not show progress bar "android:onclick=" Showindeterminate "/>" <buttonandroid:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:text= "progress bar showing progress" android:onclick= "ShowProgress"/></linearlayout>
There are three buttons defined here, three buttons corresponding to the corresponding OnClick property
Next, is 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;//The program simulates filling an array of length 100 private int[] data = new int[50];//records the percent complete of the progress dialog box int Progr Essstatus = 0;int HasData = 0; ProgressDialog pd1,pd2;//defines a handlerhandler that is responsible for the progress of the update handler = new Handler () {@Overridepublic void Handlemessage ( Message msg) {//indicates that the messages were 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 static method to display the Ring progress bar progressdialog.show (this, "Task execution," "Task execution, wait", false, True);//①} public void Showindeterminate (View source) {PD1 = new ProgressDialog (progressdialogtest.this);//Set the caption of the dialog box Pd1.settitle ( "Task is in progress");//The Settings dialog box displays the content pd1.setmessage ("task is executing, please wait ...");//SetThe Pd1.setcancelable dialog box can be closed with the "Cancel" button (true);//Settings dialog box progress bar style Pd1.setprogressstyle (progressdialog.style_horizontal);// Sets whether the progress bar of the dialog box shows Progress Pd1.setindeterminate (true);pd 1.show (); ②}public void ShowProgress (View source) {//Resets the progress bar's completion progress to 0progressStatus = 0;//to start populating the array again. HasData = 0;PD2 = new ProgressDialog (progressdialogtest.this);pd 2.setMax (max_progress);//Set the caption of the dialog box Pd2.settitle (" % Task complete ");//The Settings dialog box displays the contents of pd2.setmessage (" percent of Time to complete ");//The settings dialog cannot be closed with the" Cancel "button pd2.setcancelable (false);// Sets the progress bar style of the dialog Pd2.setprogressstyle (progressdialog.style_horizontal);//sets whether the progress bar of the dialog box shows Progress Pd2.setindeterminate (false); Pd2.show (); ③new Thread () {public void run () {while (Progressstatus < max_progress) {//Get percent complete for time-consuming operation Progressstatus = Max_progress * DoWork ()/data.length;//send an empty message to Handlerhandler.sendemptymessage (0x123);} If the task has completed if (progressstatus >= max_progress) {//Close dialog Pd2.dismiss ();}}. Start ();} Simulates a time-consuming operation. public int doWork () {//array element assignment data[hasdata++] = (int) (Math.random () * +); try{thread.sleep (100);} catch (interruptedexception e) {e.printstacktrace ();} return hasData;}}We used the thread thread to simulate a time-consuming operation when we were working on the third: progress bar showing progress
This is:
Android Development Series (27): Create a Progress dialog box with ProgressDialog