Progress bar is a commonly used component. It is easy to use. You only need to display the progress bar on the front end, start a thread in the background, and modify the progress bar status as needed. Let's look at an example. The program running effect is shown in:
The main. xml content of the main layout file of the program is as follows:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ProgressBar android:id="@+id/progressBar1" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ProgressBar android:id="@+id/progressBar2" android:layout_width="wrap_content" android:layout_height="wrap_content" style="?android:attr/progressBarStyleLarge"/> <ProgressBar android:id="@+id/progressBar3" android:layout_width="wrap_content" android:layout_height="wrap_content" style="?android:attr/progressBarStyleSmall"/> <ProgressBar android:id="@+id/progressBar4" android:layout_width="match_parent" android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal" android:max="100" android:progress="0" android:secondaryProgress="70"/> <ProgressBar android:id="@+id/progressBar5" style="@android:style/Widget.ProgressBar.Large" android:layout_width="match_parent" android:layout_height="wrap_content"/> <ProgressBar android:id="@+id/progressBar6" style="@android:style/Widget.ProgressBar.Small" android:layout_width="match_parent" android:layout_height="wrap_content"/> <ProgressBar android:id="@+id/progressBar7" android:max="100" android:progress="0" android:secondaryProgress="70" style="@android:style/Widget.ProgressBar.Horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"/></LinearLayout>
The content of the main Activity file is as follows:
Package com. liuhaoyu; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. view. view; import android. widget. progressBar; import android. widget. toast; public class MainActivity extends Activity {private ProgressBar pb1, PBS, pb3, pb4, pb5, pb6, pb7; private Handler PbHandler; private int progress = 0; /** Called when the activity is first Created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); pb1 = (ProgressBar) findViewById (R. id. progressBar1); PBS = (ProgressBar) findViewById (R. id. progressBar2); pb3 = (ProgressBar) findViewById (R. id. progressBar3); pb4 = (ProgressBar) findViewById (R. id. progressBar4); pb5 = (ProgressBar) findViewById (R. id. progressBar5); p B6 = (ProgressBar) findViewById (R. id. progressBar6); pb7 = (ProgressBar) findViewById (R. id. progressBar7); PbHandler = new Handler () {public void handleMessage (Message msg) {if (msg. what = 0x0) {pb4.setProgress (progress); pb7.setProgress (progress);} else {Toast. makeText (MainActivity. this, "the task is successfully executed! ", Toast. LENGTH_SHORT ). show (); pb1.setVisibility (View. GONE); pb2.setVisibility (View. GONE); pb3.setVisibility (View. GONE); pb4.setVisibility (View. GONE); pb5.setVisibility (View. GONE); pb6.setVisibility (View. GONE); pb7.setVisibility (View. GONE) ;}}; new Thread (new Runnable () {public void run () {while (true) {progress ++ = Math. random () * 10; try {Thread. sleep (200);} catch (InterruptedException e) {e. printStackTrace ();} Message m = new Message (); if (progress <100) {m. what = 0x0; PbHandler. sendMessage (m);} else {m. what = 0x1; PbHandler. sendMessage (m); break ;}}}}). start ();}}