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 ~