Android Application Development details (vi)--------------event handling mechanism (continued)

Source: Internet
Author: User
Tags event listener

1. Engineering Catalogue



2, Testeventactivity.java


Package Com.example.test_event;import Android.os.bundle;import Android.app.activity;import android.view.KeyEvent; Import Android.view.menu;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.view.onfocuschangelistener;import Android.view.view.onkeylistener;import Android.widget.Button; Import Android.widget.checkbox;import Android.widget.compoundbutton;import Android.widget.compoundbutton.oncheckedchangelistener;import Android.widget.edittext;//import Android.widget.textview;import Android.widget.toast;public class Testeventactivity extends Activity {//private TextView MyTextview1, MyTextview2, Mytextview3;private Button myButton1, mybutton2;private EditText myEditText1, Myedittext2;private CheckBox myCheckbox1; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.test_event);//MyTextview1 = (TextView) Findviewbyid ( R.ID.TEXTVIEWLAYOUT01);//MyTextview2 = (TextView) Findviewbyid (r.id.textVIEWLAYOUT02);//myTextView3 = (TextView) Findviewbyid (r.id.textviewlayout03); myButton1 = (Button) Findviewbyid ( r.id.buttonlayout01) MyButton2 = (Button) Findviewbyid (r.id.buttonlayout02); myEditText1 = (EditText) Findviewbyid ( R.ID.EDITTEXTLAYOUT01); myEditText2 = (EditText) Findviewbyid (r.id.editviewlayout02); myCheckbox1 = (CheckBox) Findviewbyid (r.id.checkboxlayout01);//Edit the text box's key event Myedittext1.setonkeylistener (new Onkeylistener () {@ Overridepublic boolean OnKey (View arg0, int arg1, keyevent arg2) {//TODO auto-generated method Stubmyedittext1.settext ("" ); return false;}); Myedittext2.setonkeylistener (New Onkeylistener () {@Overridepublic Boolean onKey (View arg0, int arg1, keyevent arg2) {//T ODO auto-generated Method Stubmyedittext2.settext (""); return false;}); /Edit the Focus event of the text box Myedittext1.setonfocuschangelistener (new Onfocuschangelistener () {@Overridepublic void Onfocuschange ( View arg0, Boolean arg1) {//TODO auto-generated method Stubtoast.maketext (Getapplicationcontext (), Myedittext1.gettexT (), Toast.length_long). Show ();}}); Myedittext2.setonfocuschangelistener (New Onfocuschangelistener () {@Overridepublic void Onfocuschange (View arg0, Boolean arg1) {//TODO auto-generated method Stubtoast.maketext (Getapplicationcontext (), Myedittext2.gettext (), Toast.length_long). Show ();}}); /Multi Box Selection event Mycheckbox1.setoncheckedchangelistener (new Oncheckedchangelistener () {@Overridepublic void OnCheckedChanged (Compoundbutton arg0, Boolean arg1) {//TODO auto-generated method Stubtoast.maketext ( Getapplicationcontext (), mycheckbox1.ischecked () + "", Toast.length_long). Show ();}}); /Button Selection Event Mybutton1.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View arg0) {//TODO Auto-generated Method Stubtoast.maketext (Getapplicationcontext (), Mybutton1.gettext (), Toast.length_long). Show ();}); Mybutton2.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View arg0) {//TODO auto-generated Method Stubtoast.maketext (Getapplicationcontext (), Mybutton2.gettext (), Toast.lengtH_long). Show ();}}); @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (r.menu.test_event, menu); return true;}}

3. layout file

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <tablelayout android:id= "@+id/myT Ablelayout "android:layout_width=" wrap_content "android:layout_height=" wrap_content "> <TableRo w> <textview android:id= "@+id/textviewlayout01" android:layout_width= "Wrap_ Content "android:layout_height=" wrap_content "android:text=" user name "/> <editt Ext android:id= "@+id/edittextlayout01" android:layout_width= "Fill_parent" and            Roid:scrollhorizontally= "true" android:text= "admin"/> </TableRow> <TableRow> <textview android:id= "@+id/textviewlayout02" android:layout_width= "Wrap_conten T "Android:layout_height= "wrap_content" android:text= "User password"/> <edittext android:id= "@+i                d/editviewlayout02 "android:layout_width=" Fill_parent "android:password=" true " Android:scrollhorizontally= "true" android:text= "123"/> </TableRow> &LT;TABLEROW&G            T                <textview android:id= "@+id/textviewlayout03" android:layout_width= "Wrap_content"                android:layout_height= "Wrap_content" android:text= "Automatic login"/> <checkbox Android:id= "@+id/checkboxlayout01" android:layout_width= "Wrap_content" Android:layout_heig ht= "Wrap_content"/> </TableRow> <TableRow> <button android:id= " @+id/buttonlayout01 "android:layout_width=" wrap_content "android:layout_height=" wrap_content         "       android:text= "Login"/> <button android:id= "@+id/buttonlayout02" Androi        D:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Cancel"/> </TableRow> </TableLayout></LinearLayout>

Knowledge

Common event listeners in Android:

(1) Click event View.onclicklistener: This event occurs when the user touches a component or the arrow key is pressed, and the process is onclick ()

(2) Focus event View.onfocuschangelistener: This event occurs when the component Gets or loses focus, and the processing method is Onfocuschange ()

(3) Key event View.onkeylistener: Generated when the user presses or releases a key on the device, processing method OnKey ()

(4) Touch event View.ontouchlistener: When the device has touch function, touch the screen to produce the event, processing Method Ontouch ()

(5) Create context menu event View.oncreatecontextmenulistener: The event is generated when the context menu is created, and the processing method is Oncreatecontextmenu ()

Event Processing steps:

(1) Creating an event listener

(2) Registering event listeners for the component to respond to events

(3) Write implementation code in the event handling method

Android Application Development details (vi)--------------event handling mechanism (continued)

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.