The. XML code is as follows:
<?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:o rientation= "vertical" > <progressbar android:id= "@+id/bar" android:layout_width= "Fill_parent" android:layout_height= "wrap_content" style= "Android:attr/progressbarstylehorizontal"/> < TextView android:id= "@+id/info" android:layout_width= "fill_parent" android:layout_height= "Wrap_ Content " android:text=" "/></linearlayout>
. Java code is as follows:
Package Org.lxh.demo;import Android.app.activity;import Android.app.alertdialog;import android.app.Dialog;import Android.content.dialoginterface;import Android.os.asynctask;import Android.os.bundle;import Android.view.View; Import Android.view.view.onclicklistener;import Android.view.view.onfocuschangelistener;import Android.widget.button;import Android.widget.edittext;import Android.widget.progressbar;import Android.widget.textview;public class Hello extends Activity {private ProgressBar bar = null;private TextView info = null;p ublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);//life cycle Method Super.setcontentview ( R.layout.main); Set the layout manager to use This.bar = (ProgressBar) Super.findviewbyid (r.id.bar); this.info = (TextView) Super.findviewbyid ( R.id.info); Childupdate child = new Childupdate ();//Sub-Task Object Child.execute (100);//Set sleep time}private class Childupdate extends Asynctask <integer, Integer, string> {//Overwrite the following method @overrideprotected String doinbackground (integer ... params) {//Handle background task for (int x = 0; x < + +) {Hello.this.bar.setProgress (x);//progress bar set this.publishprogress (x);//pass each update content try {Thread.Sleep (params[0]);} catch (Interruptedexception e) {e.printstacktrace ();}} Return "Execution is complete! ";} @Overrideprotected void OnPostExecute (String result) {//The task executes after execution Hello.this.info.setText (result);} @Overrideprotected void Onprogressupdate (Integer ... progress) {//value after each update Hello.this.info.setText ("Current progress is:" + String.valueof (Progress[0]));}}
Android--Asynchronous Processing tool Class (Asynctask)