Code
Packagecom.lxt008;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.view.View;ImportAndroid.view.Window;ImportAndroid.widget.Button;ImportAndroid.widget.ProgressBar; Public classActivity01extendsactivity{//declaring ProgressBar objects PrivateProgressBar M_progressbar; PrivateProgressBar m_progressbar2; PrivateButton MButton01; protected Static Final intGui_stop_notifier = 0x108; protected Static Final intGui_threading_notifier = 0x109; Public intIntcounter=0; /**Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //Set the window mode, because the progress bar needs to be displayed in the title barrequestwindowfeature (window.feature_progress); Setprogressbarvisibility (true); Setcontentview (R.layout.main); //Get ProgressBarM_progressbar =(ProgressBar) Findviewbyid (R.ID.PROGRESSBAR01); M_progressbar2=(ProgressBar) Findviewbyid (R.ID.PROGRESSBAR02); MButton01=(Button) Findviewbyid (R.ID.BUTTON01); M_progressbar.setindeterminate (false); M_progressbar2.setindeterminate (false); //when the button is pressed, start execution,Mbutton01.setonclicklistener (NewButton.onclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method Stub//set ProgressBar to visible statem_progressbar.setvisibility (view.visible); M_progressbar2.setvisibility (view.visible); //set the maximum value of the ProgressBarM_progressbar.setmax (100); //set ProgressBar Current valueM_progressbar.setprogress (0); M_progressbar2.setprogress (0); //changing the value of ProgressBar by thread NewThread (NewRunnable () { Public voidrun () { for(inti = 0; I < 10; i++) { Try{intcounter= (i + 1) * 20; Thread.Sleep (1000); if(i = = 4) {Message m=NewMessage (); M.what=Activity01.gui_stop_notifier; Activity01. This. Mymessagehandler.sendmessage (m); Break; } Else{Message m=NewMessage (); M.what=Activity01.gui_threading_notifier; Activity01. This. Mymessagehandler.sendmessage (m); } } Catch(Exception e) {e.printstacktrace (); }}}). Start (); } }); } Handler Mymessagehandler=NewHandler () {//@Override Public voidhandlemessage (Message msg) {Switch(msg.what) {//ProgressBar is already on the big value CaseActivity01.GUI_STOP_NOTIFIER:m_ProgressBar.setVisibility (View.gone); M_progressbar2.setvisibility (View.gone); Thread.CurrentThread (). interrupt (); Break; CaseActivity01.gui_threading_notifier:if(!Thread.CurrentThread (). isinterrupted ()) { //change the current value of the ProgressBarm_progressbar.setprogress (intcounter); M_progressbar2.setprogress (intcounter); //set a progress bar progress value for a foreground in the title barSetprogress (intcounter*100); //set the progress value of a progress bar later in the title barSetsecondaryprogress (intcounter*100);// } Break; } Super. Handlemessage (msg); } };}
Layout
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" ><TextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "@string/hello" /> <ProgressBarAndroid:id= "@+id/progressbar01"style= "? Android:attr/progressbarstylehorizontal"Android:layout_width= "200DP"Android:layout_height= "Wrap_content"android:visibility= "Gone" /> <ProgressBarAndroid:id= "@+id/progressbar02"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"style= "? Android:attr/progressbarstylelarge"Android:max= "+"android:progress= " the"android:secondaryprogress= "The "android:visibility= "Gone" /> <ButtonAndroid:id= "@+id/button01"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Start" /></LinearLayout>
Android-Advanced UI Control _progressbar progress bar