Android handle
1 <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 Android:paddingbottom= "@dimen/activity_vertical_margin"6 Android:paddingleft= "@dimen/activity_horizontal_margin"7 Android:paddingright= "@dimen/activity_horizontal_margin"8 Android:paddingtop= "@dimen/activity_vertical_margin"9 Tools:context=". Mainactivity " >Ten One <TextView A Android:id= "@+id/text_id" - Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" the Android:text= "@string/hello_world" /> - <Button - Android:id= "@+id/btn" - Android:layout_width= "Wrap_content" + Android:layout_height= "Wrap_content" - Android:text= "button" + Android:layout_below= "@id/text_id" A at /> - - </Relativelayout>
1 Packagecom.ibox365.s0725001;2 3 ImportAndroid.os.Bundle;4 ImportAndroid.os.Handler;5 ImportAndroid.os.Message;6 Importandroid.app.Activity;7 ImportAndroid.view.Menu;8 ImportAndroid.view.View;9 ImportAndroid.view.View.OnClickListener;Ten ImportAndroid.widget.Button; One A Public classMainactivityextendsActivity { - - Privatebutton button; the PrivateHandler Handler; - - @Override - protected voidonCreate (Bundle savedinstancestate) { + Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); + Abutton=(Button) Findviewbyid (R.ID.BTN); atButton.setonclicklistener (NewButtonlisten ()); - -Handler=NewMyhandle (); - - } - in classButtonlistenImplementsOnclicklistener - { to + /*(Non-javadoc) - * @see Android.view.view.onclicklistener#onclick (Android.view.View) the */ * @Override $ Public voidOnClick (View v) {Panax Notoginseng //TODO auto-generated Method Stub - theMessage msg=handler.obtainmessage (); +msg.what=2; A handler.sendmessage (msg); the } + - $ } $ - classMyhandleextendshandler{ - the /*(Non-javadoc) - * @see android.os.handler#handlemessage (android.os.Message)Wuyi */ the @Override - Public voidhandlemessage (Message msg) { Wu //TODO auto-generated Method Stub - Super. Handlemessage (msg); About $ intwhat =Msg.what; - System.out.println (what); - - } A + the } - $ @Override the Public BooleanOncreateoptionsmenu (Menu menu) { the //inflate the menu; This adds items to the action bar if it is present. the getmenuinflater (). Inflate (R.menu.main, menu); the return true; - } in the}
Android Handle Exercises