Little did not think that such a small program took a long time to get done.
This program looks very simple, such as:
A progress bar is constantly increasing, accumulating to more than 100%, hiding the loading progress bar, and the text changing to a "countdown to 3 seconds" to continue execution.
After the number of three seconds, the progress bar will continue to accumulate.
First, because the label text is dynamic, through the Java file control, in Res\values\string.xml, only need to change the program name to "progress bar", nothing Special:
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "App_name" > Progress bar </string > <string name= "Action_settings" >Settings</string></resources>
After that, the layout was nothing special, as thought:
In Res\layout\activity_main.xml, the following code can be modified:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <linearlayout Android:layout_widt H= "Match_parent" android:layout_height= "wrap_content" android:orientation= "Horizontal" > <Progr Essbar android:id= "@+id/progressbar1" style= "@android: Style/widget.progressbar.large" and Roid:layout_width= "Wrap_content" android:layout_height= "Wrap_content"/> <textview and Roid:id= "@+id/textview1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content"/&G T </LinearLayout> <progressbar android:id= "@+id/progressbar2" style= "? Android:attr/progressbarstyl Ehorizontal "android:layout_width=" match_parent "android:layout_height=" wrap_content "android:max=" 10 0 "/></linearlayOut>
Among them, here slender progress bar ProgressBar2 style may be more special, but Android is so required no way, specify its maximum value is 100%. Progress bar 1, looks like a constantly rotating loading image, but is actually a progress bar, it does not have the maximum value, and can not set its current progress through the Java file, in the Mainactivity.java can only set its display or not.
This sets the ID for each component and uses a nested linear layout.
The point is Mainactivity.java this file got me for a long time, the code is as follows:
Package Com.progressbar;import Java.util.timer;import Java.util.timertask;import android.os.bundle;import Android.os.handler;import Android.os.message;import Android.app.activity;import Android.view.Menu;import Android.view.view;import Android.widget.progressbar;import Android.widget.textview;public class MainActivity Extends Activity {private ProgressBar progressbar1;private ProgressBar progressbar2;private TextView textview1;// Defines a message handler. Private Handler Handler; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main);//Get individual components, nothing special. ProgressBar1 = (ProgressBar) Findviewbyid (R.ID.PROGRESSBAR1); ProgressBar2 = (ProgressBar) Findviewbyid (R.ID.PROGRESSBAR2); TextView1 = (TextView) Findviewbyid (R.ID.TEXTVIEW1);//Set a timer new timer () that runs every 1000 milliseconds, that is, 1 seconds. Schedule (New TimerTask () {int i = 0;//here stands for progress Int J = 3;//here stands for the countdown @overridepublic Void Run () {Message msg = new Message (), or the message declaration here must be placed in run ( ) method, or else the program, because the old message does not perish will cardDead//Put the new method of message in run () here, the timer executes every time, it kills the old information and creates new information. if (I <) {msg.what = I;handler.sendmessage (msg);//To the message processor i + = Math.random () * 20;//i each increment 20* (0) to the what class member that I store in Msg. XXX) Progress} else {if (J > 0) {msg.what = I;msg.arg1 = J;handler.sendmessage (msg);//pass J into the Arg1 class member of MSG to the message processor j--;} else {///pour The number is complete, restart i = 0;j = 3;}}}, 0, 1000);//Keep on receiving the timer message, according to the parameters of the message, processing Handler = new Handler (new Handler.callback () {//So write, do not eject what leaks Warning @overridepublic boolean handlemessage (Message msg) {if (Msg.what < 100) {//If the message has less than 100 of what parameter, Set the progress bar progressbar2.setprogress (msg.what),//Set the progress of the slender progress bar ProgressBar2 if (msg.what = = 0) {//Only when the What parameter equals 0, set the label text and the progress bar , do not load every time the read progress is made. Textview1.settext ("Running ..."); Progressbar1.setvisibility (view.visible);}} else {if (msg.arg1 = = 3) {progressbar1.setvisibility (view.gone); Progressbar2.setprogress (0);} Textview1.settext ("Run finished, wait for" + Msg.arg1 + "seconds to continue running the next program ...");} return false;}}); @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if IT is Present.getmenuinflater (). Inflate (R.menu.main, menu); return true;}} It may be puzzling, why not set the text directly in the timer, but also very complex appearance of the entire message processor handle, and the leakage of the processor.
I'm not like this in the first place, all because the timer is a new thread, Android does not allow to set the value of the label text TextView1 in other threads, if you do not set the label text in the Android process, the error message will pop up:
Therefore, you must take advantage of the message delivery in Android thread, let the original thread in Android set the value of the label text, and process the progress bar. I've been doing this for a long time.
At the same time, the setting of the timer here uses the anonymous inner class in Java, see "java" Timer, thread and anonymous inner class (click to open link). We don't have to repeat it here.
At last casually say, here the label text can set its textsize= "24SP", the default font is too small, not very good-looking.
Message processing between the "Android" progress bar and the thread