:
See the following code:
public class Progressactivity extends Activity implements Onclicklistener {//MAX progress private static final int max_progress = 100;//progress bar Private ProgressDialog progressdialog;//Current progress value private int progress = 0;//control Private Button Btn_progress_1;priva Te Button btn_progress_2;private Handler Handler = new Handler () {@Overridepublic void Handlemessage (Message msg) {Super.h Andlemessage (msg); switch (msg.what) {case 1:if (Progress > Max_progress) {//reach maximum progress = 0;progressdialog.dismiss ();} else {progress++;//Progress increment 1progressdialog.incrementprogressby (1); handler.sendemptymessagedelayed (1,50 + new Random (). Nextint (500));} Break;default:break;}}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( r.layout.activity_progress); Initviews (); Initlisteners ();} private void Initviews () {btn_progress_1 = (button) Findviewbyid (r.id.btn_progress_1); btn_progress_2 = (Button) Findviewbyid (r.id.btn_progress_2);} private void Initlisteners () {Btn_progresS_1.setonclicklistener (this); Btn_progress_2.setonclicklistener (this);} /** * Show dialog box */@SuppressWarnings ("deprecation") private void Showprogressdialog (int style) {ProgressDialog = new ProgressDialog (This);p Rogressdialog.seticon (r.drawable.ic_launcher);p rogressdialog.settitle ("Processing data ..."); Progressdialog.setmessage ("Please later ...");//Set the style of the progress dialog box Progressdialog.setprogressstyle (style);// Sets the progress maximum of the progress dialog box Progressdialog.setmax (max_progress);//Set the pause button for the Progress dialog box Progressdialog.setbutton ( Dialoginterface.button_positive, "Pause", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick ( Dialoginterface dialog, int which) {handler.removemessages (1);}}); /Set Progress Dialog "Cancel" button Progressdialog.setbutton (dialoginterface.button_neutral, "Cancel", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {handler.removemessages (1);//Reset progress = 0; Progressdialog.setprogress (progress);}}); Progressdialog.show ();p rogress = (Progress > 0)? Progress:0;progressdialog.setprogress(progress); Handler.sendemptymessage (1);} @Overridepublic void OnClick (View v) {switch (V.getid ()) {Case r.id.btn_progress_1://Progress dialog Box Showprogressdialog ( progressdialog.style_horizontal) break;case r.id.btn_progress_2://Rotation dialog showprogressdialog (ProgressDialog.STYLE_ SPINNER); break;default:break;}}}
Reprint Please specify source:http://blog.csdn.net/hai_qing_xu_kong/article/details/45024997 Emotional Control _
Learn about Android together create simple ProgressDialog (30)