Android day 10-use handler to implement the SS component

Source: Internet
Author: User

 

Main. 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: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/Hello"/>

<Progressbar
Android: Id = "@ + ID/Bar"
Style = "? Android: ATTR/progressbarstylehorizontal"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: visibility = "gone"
/>

<Button
Android: Id = "@ + ID/startbutton"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "button"

/>
</Linearlayout>

 

Progresshandleractivity. Java

Package CN. class3g. activity;

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. OS. Handler;
Import Android. OS. message;
Import Android. util. log;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Import Android. widget. progressbar;

Public class progresshandleractivity extends activity {

Private progressbar;
Private button startbutton;
 
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

Bar = (progressbar) This. findviewbyid (R. Id. bar );

Startbutton = (button) This. findviewbyid (R. Id. startbutton );

Startbutton. setonclicklistener (New buttonlistener ());

}



Class buttonlistener implements onclicklistener {

Public void onclick (view v ){

Bar. setvisibility (view. Visible );

Updatebarhandler. Post (updatethread );

}


}


Handler updatebarhandler = new handler (){

Public void handlemessage (Message MSG ){

Bar. setprogress (msg. arg1 );

Updatebarhandler. Post (updatethread );


}



};
 
 
Runnable updatethread = new runnable (){

Int I = 0;
Public void run (){

// Log. I ("tag", "begin thread ");

I = I + 10;

Message MSG = updatebarhandler. obtainmessage ();

MSG. arg1 = I;

Try {
Thread. Sleep (1000 );
} Catch (interruptedexception e ){

E. printstacktrace ();
}


Updatebarhandler. sendmessage (MSG );

If (I = 100 ){


Updatebarhandler. removecallbacks (updatethread );

}


}

};
 
 
 
 
 
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.