1 PackageCom.example.myhandlertest3;2 3 ImportAndroid.os.Bundle;4 ImportAndroid.os.Handler;5 ImportAndroid.os.Message;6 Importandroid.support.v7.app.ActionBarActivity;7 ImportAndroid.view.View;8 ImportAndroid.view.View.OnClickListener;9 ImportAndroid.widget.Button;Ten ImportAndroid.widget.TextView; One /** A * Android Handler - * @authorshaobn - * @date 2015/9/9 the */ - Public classMainactivityextendsactionbaractivity { - Privatebutton button; - PrivateTextView TV; + PrivateHandler Handler; - @Override + protected voidonCreate (Bundle savedinstancestate) { A Super. OnCreate (savedinstancestate); at Setcontentview (r.layout.activity_main); -Button = (Button) This. Findviewbyid (r.id.button1); -Handler =NewHandler () { - - Public voidhandlemessage (android.os.Message msg) { - inBundle bundle =Msg.getdata (); -String data = bundle.getstring ("Color"); toMainactivity. This. Button.append (data); + }; - }; the FinalRunnable Runnable =NewRunnable () { * $ @OverridePanax Notoginseng Public voidrun () { - //TODO auto-generated Method Stub theString color = "Red"; +Message message =NewMessage (); ABundle bundle =NewBundle (); theBundle.putcharsequence ("Color", color); + Message.setdata (bundle); -Mainactivity. This. Handler.sendmessage (message); $ Try { $Thread.Sleep (3000); -}Catch(Exception e) { - //Todo:handle Exception the e.printstacktrace (); - }Wuyi } the }; -Button.setonclicklistener (NewOnclicklistener () { Wu - @Override About Public voidOnClick (View arg0) { $ //TODO auto-generated Method Stub - NewThread (runnable). Start (); - } - }); A } + the -}
Write your own random demo, record it.
Android handler Simple to use