<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" > <ButtonAndroid:id= "@+id/progress"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Bar progress bar" /> <ButtonAndroid:id= "@+id/circle"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Round progress bar" /></LinearLayout>
Packagecom.example.yanlei.my;ImportAndroid.app.ProgressDialog;ImportAndroid.content.DialogInterface;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportJava.util.Calendar;Importandroid.app.Activity;ImportAndroid.app.DatePickerDialog;ImportAndroid.app.Dialog;ImportAndroid.app.TimePickerDialog;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.DatePicker;ImportAndroid.widget.EditText;ImportAndroid.widget.TimePicker; Public classMainactivityextendsappcompatactivity {//Claim Button PrivateButton btncircle=NULL; PrivateButton btnprogress=NULL; //Declaring a Progress bar dialog box PrivateProgressDialog pddialog=NULL; //Progress Count intICount = 0; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Btncircle=(Button) Findviewbyid (r.id.circle); Btnprogress=(Button) Findviewbyid (r.id.progress); //setting up Btncircle for event monitoringBtncircle.setonclicklistener (NewButton.onclicklistener () {@Override Public voidOnClick (View v) {ICount= 0; //Create a ProgressDialog objectPddialog =NewProgressDialog (mainactivity. This); //set the style of the progress bar, the style is round, the rotation of thePddialog.setprogressstyle (Progressdialog.style_spinner); //Set ProgressDialog titlePddialog.settitle ("Round progress bar")); //Set ProgressDialog prompt informationPddialog.setmessage ("Downloading in ..."); //set ProgressDialog title iconPddialog.seticon (R.drawable.ic_launcher); //Set ProgressDialog progress bar ProgressPddialog.setprogress (100); //sets whether the ProgressDialog progress bar is ambiguousPddialog.setindeterminate (false); //sets whether ProgressDialog can be canceled by pressing the return buttonPddialog.setcancelable (true); //set a button for ProgressDialogPddialog.setbutton ("Cancel",NewDialoginterface.onclicklistener () { Public voidOnClick (Dialoginterface Dialog,inti) {//Click "Cancel" button to cancel the dialog boxDialog.cancel (); } }); //Let ProgressDialog showpddialog.show (); //creating a Thread instance NewThread () { Public voidrun () {Try{ while(ICount <= 100) { //the progress is controlled by the thread. Pddialog.setprogress (ICount + +)); Thread.Sleep (50); } pddialog.cancel (); } Catch(interruptedexception e) {pddialog.cancel (); }}}.start (); } }); //setting up btnprogress for event monitoringBtnprogress.setonclicklistener (NewButton.onclicklistener () {@Override Public voidOnClick (View v) {ICount= 0; //Create a ProgressDialog objectPddialog =NewProgressDialog (mainactivity. This); //set the style of the progress bar, the style is the long shapePddialog.setprogressstyle (progressdialog.style_horizontal); //Set ProgressDialog titlePddialog.settitle ("Bar progress bar"); //Set ProgressDialog prompt informationPddialog.setmessage ("Downloading in ..."); //set ProgressDialog title iconPddialog.seticon (R.drawable.ic_launcher); //Set ProgressDialog progress bar ProgressPddialog.setprogress (100); //sets whether the ProgressDialog progress bar is ambiguousPddialog.setindeterminate (false); //sets whether ProgressDialog can be canceled by pressing the return buttonPddialog.setcancelable (true); //Let ProgressDialog showpddialog.show (); //creating a Thread instance NewThread () { Public voidrun () {Try{ while(ICount <= 100) { //the progress is controlled by the thread. Pddialog.setprogress (ICount + +)); Thread.Sleep (50); } pddialog.cancel (); } Catch(interruptedexception e) {pddialog.cancel (); }}}.start (); } }); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.menu_main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml. intID =Item.getitemid (); //noinspection simplifiableifstatement if(id = =r.id.action_settings) { return true; } return Super. onoptionsitemselected (item); }}
Android Progress Bar dialog Box ProgressDialog