The progress bar allows you to know the running Progress of the program. It is also an interactive means that the waiting time is not that long. When using the progress bar, you can use the default style of the system, or you can specify the style by yourself through the XML file.
Custom scroll bar style
<? XML version = "1.0" encoding = "UTF-8"?> <Layer-list xmlns: Android = "http://schemas.android.com/apk/res/android"> <! -- Define the background of the track --> <item Android: Id = "@ Android: ID/background" Android: drawable = "@ drawable/no"/> <! -- Define the completed style of the track --> <item Android: Id = "@ Android: ID/progress" Android: drawable = "@ drawable/OK"/> </layer-List>
Layout File
<? 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: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "task completion progress"/> <! -- Define a horizontal progress bar --> <progressbar Android: Id = "@ + ID/Bar" style = "@ Android: style/widget. progressbar. horizontal "Android: layout_width =" fill_parent "Android: layout_height =" wrap_content "Android: max =" 100 "/> <! -- Define a horizontal progress bar and change the track appearance --> <progressbar Android: Id = "@ + ID/bar2" style = "@ Android: style/widget. progressbar. horizontal "Android: layout_width =" fill_parent "Android: layout_height =" wrap_content "Android: max =" 100 "Android: progressdrawable =" @ drawable/my_bar "/> <button Android: id = "@ + ID/button" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "@ string/start"/> </linearlayout>
Click the start button, start a new thread to execute the task, and then use a handler to receive messages and update the scroll bar.
Public class progressbartest extends activity {// This program simulates the array private int [] DATA = new int [100] with a length of 100; int hasdata = 0; // record the progress of ssssbar completion int status = 0; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Final progressbar bar = (progressbar) findviewbyid (R. id. bar); Final progressbar bar2 = (progressbar) findviewbyid (R. id. bar2); // create Handlerfinal handler mhandler = new handler () {@ overridepublic void handlemessage (Message MSG) {// indicates that the message is sent by the program. If (MSG. what = 0x111) {bar. setprogress (Status); bar2.setprogress (Status) ;}}; final button startbutton = (button) findviewbyid (R. id. button); startbutton. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view arg0) {// todo auto-generated method stubhasdata = 0; status = 0; // start the thread to execute the task new thread () {public void run () {While (status <100) {// get the completion percentage of time-consuming Operations Status = dowork (); // send the message to handlerm Essage M = new message (); M. what = 0x111; // send message mhandler. sendmessage (m );}}}. start () ;}}) ;}// simulate a time-consuming operation. Public int dowork () {// assign data [hasdata ++] = (INT) (math. random () * 100); try {thread. sleep (100);} catch (interruptedexception e) {e. printstacktrace () ;}return hasdata ;}}