1, Activity_progressbar.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Vertical" >
<textview
Android:id= "@+id/textview1"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:text= "Two Progress bars Demo:"/>
<progressbar
Android:id= "@+id/progressbar_horizontal"
Style= "? android:attr/Progressbarstylehorizontal"
Android:layout_width= "200DP"
android:layout_height= "Wrap_content"
android:visibility= "Gone"/>
<progressbar
Android:id= "@+id/progressbar_large"
Style= "? android:attr/Progressbarstylelarge"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:max= "
android:progress= "
android:secondaryprogress= "
android:visibility= "Gone"/>
<button
Android:id= "@+id/btn_pb"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Start"/>
</LinearLayout>
2, Progressbaractivity.java
public class Progressbaractivity extends Activity {
Declaring ProgressBar objects
Private ProgressBar Progressbar_horizontalbar;
Private ProgressBar Progressbar_largebar;
Private Button BTN_PB;
protected static final int gui_stop_notifier = 0x108;
protected static final int gui_threading_notifier = 0x109;
public int counter = 0;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
//Set window mode because the progress bar needs to be displayed in the title bar
requestwindowfeature (window.feature_progress);
setprogressbarvisibility (true);
Setcontentview (R.layout.activity_progressbar);
Progressbar_horizontalbar = (ProgressBar) Findviewbyid (r.id.progressbar_horizontal);
Progressbar_largebar = (ProgressBar) Findviewbyid (R.id.progressbar_large);
BTN_PB = (Button) Findviewbyid (R.ID.BTN_PB);
/*
* Appearance is not clear that the current value of the scrollbar automatically moves back and forth between the minimum and maximum values, creating an animated effect that tells others "I am working," but does not indicate to which stage the work progresses
* 。 It is primarily a reminder of tasks that do not determine the operating time. and "Clear" is based on your progress can set the current progress value.
*/
Progressbar_horizontalbar.setindeterminate (false);
Progressbar_largebar.setindeterminate (false);
Btn_pb.setonclicklistener (New Button.onclicklistener () {
@Override
public void OnClick (View v) {
Set ProgressBar to visible state
Progressbar_horizontalbar.setvisibility(view.visible);
Progressbar_largebar.setvisibility (view.visible);
Set the maximum value of the ProgressBar
Progressbar_horizontalbar.Setmax(100);
Set the current value of the ProgressBar
Progressbar_horizontalbar.setprogress(0);
Progressbar_largebar.setprogress (0);
Changing the value of ProgressBar by thread
New Thread (New Runnable (){
@Override
public void Run () {
for (int i = 0; i < i++) {
try {
Counter = (i + 1) * 20;
Thread.Sleep (1000);
if (i==4) {
message M=new message ();
M.what=progressbaractivity.gui_stop_notifier;
Progressbaractivity.this.Handler.sendmessage (m);
Break
}else {
Message m=new message ();
M.what=progressbaractivity.gui_threading_notifier;
ProgressbarActivity.this.handler.sendMessage (m);
}
} catch (Exception e) {
E.printstacktrace ();
}
}
}
}). Start ();
}
});
}
HandlerHandler=new Handler () {
public void Handlemessage (Message msg) {
Switch (msg.what) {
Case Progressbaractivity.gui_stop_notifier:
Progressbar_horizontalbar.setvisibility (View.gone);
Progressbar_largebar.setvisibility (View.gone);
//reached maximum termination thread run
Thread.CurrentThread (). interrupt ();
Break
Case Progressbaractivity.gui_threading_notifier:
if (! Thread.CurrentThread (). isinterrupted ()) {
Change the value of the progress bar
Progressbar_horizontalbar.setprogress(counter);
Progressbar_largebar.setprogress (counter);
Set uptitle barProgress value for a progress bar in the foreground
Setprogress (counter*100);
Set the progress value of a progress bar later in the title bar
Setsecondaryprogress (counter*100);
}
Break
}
Super.Handlemessage(msg);
}
};
}
android--progress bar ProgressBar