1. Add a button to the Activity_main.xml file using the same project you created in the previous article:
<button android:id= "@+id/btn_dialog3" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" android:onclick= "OnClick3" android:text= "Click to display a detailed progress dialog"/>
2, add the specific progress bar implementation code in the Mainactivity.java file:
First add the OnClick3 () method:
public void OnClick3 (View v) {showDialog (1);//ID 1, when the callback method Oncreatedialog () is invoked, the ID is passed in to select Case 1. Progressdialog.setprogress (0);//starting from 0 new Thread (new Runnable () {@Overridepublic void Run () {//TODO auto-generated Method stubfor (int i = 1; i <=; i++) {try {thread.sleep ();p Rogressdialog.incrementprogressby (100/10);//Step 10 } catch (Interruptedexception e) {//Todo:handle exceptione.printstacktrace ();}} Progressdialog.dismiss ();//Destroy Dialog}}). Start ();
Then add the code in the Oncreatedialog () callback method to the id=1 (that is, the code that implements the show progress dialog box):
Case 1:progressdialog = new ProgressDialog (this);p Rogressdialog.seticon (R.drawable.ic_launcher); Progressdialog.settitle ("Downloading files ...");p Rogressdialog.setprogressstyle (progressdialog.style_horizontal )///Set the progress bar style Progressdialog.setbutton (dialoginterface.button_positive, "OK",//Set OK button new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {//TODO Auto-generated Method Stubtoast.maketext (Getbasecontext (), "OK clicked!", Toast.length_short). Show ();}); Progressdialog.setbutton (Dialoginterface.button_negative, "Cancel",//Set 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 a bit, the effect is as follows:
Click here to download the full code ~