Android handles UI Events

Source: Internet
Author: User

I. Overview

In Java and Android, events are communicated to various input devices.

Events are messages generated by the OS, and when a UI element is manipulated by the user in a particular way, the OS sends the event message to the appropriate view object.

Second, monitoring and event processing

"Processing" (Handle) and "handlers" (Handler)

Third, the view class handles UI events

In the application, each UI element is an instantiated view object that is different from each other, and each object contains events that are specifically owned by that view object.

In a layout, a View object communicates with the rest of the application through a public callback (Callback) method, and when a particular action occurs in the view object of that UI, Android invokes the specified callback method.

A set of event processing interfaces is available in Android. These event interfaces are nested within the view class and can be used to define callback methods. Because they are part of the OS, they make up the view class and all of its methods. The interface nested within the view class is the event listener.

1. Event callback method:

The event listener is a Java interface in the view class that contains a separate callback method. Here are the callback methods that are commonly used in Android:

    • OnClick (): From the View.onclicklistener class, triggered when the screen is touched or when a UI element is clicked.
    • Onlongclick (): From the View.onlongclicllistener class, Touch or button A UI element is triggered more than 1 seconds.
    • OnKey (): From the View.onkeylistener class, triggered when the keyboard is pressed or released.
    • OnTouch (): From the View.ontouchlistener class, triggered when touch, release, gesture events occur.
    • Onfocuschange (): From the View.onfocuschange class, triggered when focus changes.
    • OnContextMenu (): From the View.ontouchlistener class, context menu.
Iv. Event Binding

1. Add the onclick (...) for the specified UI element Event Listener

1  Public classMainactivityextendsActivity2 {3     PrivateContext Mcontext;4 5 @Override6      Public voidonCreate (Bundle savedinstancestate)7     {8         Super. OnCreate (savedinstancestate);9 Setcontentview (r.layout.activity_main);Ten  OneMcontext =Getapplicationcontext (); A  -         //Get button object, bind event listener -Button syncbtn =(Button) Findviewbyid (R.ID.MAIN_SYNC_BTN1); the         //To bind a button control to an event listener -Syncbtn.setonclicklistener (NewView.onclicklistener () -         { - @Override +              Public voidOnClick (View v) -             { +                 //Select the UI element that triggers the Listener event view A                 Switch(V.getid ()) at                 { -                      CaseR.ID.MAIN_SYNC_BTN1: -                     { -EditText EditText =(EditText) Findviewbyid (R.ID.MAIN_INPUT_TEXT1); -TextView Showtext =(TextView) Findviewbyid (R.ID.MAIN_INPUT_TEXT2); - Showtext.settext (Edittext.gettext ()); in                          Break; -                     } to                     default: + Toast.maketext (Mcontext, Mcontext.getresources (). getString (r.string.disable_event), Toast.leng Th_short). Show (); -                          Break; the                 } *             } $         });Panax Notoginseng     } -  the}

2. Add onclick (...) to the activity Listener

1 /**2 * @Classname mainactivity3 * @abstract inherit activity class4 * @description This class is used to test adding an onclick listener to an activity, inheriting an interface View.onclicklistener class5  * @throws6  */7  Public classMainactivityextendsActivityImplementsView.onclicklistener8 {9     PrivateContext Mcontext;Ten  One @Override A      Public voidonCreate (Bundle savedinstancestate) -     { -         Super. OnCreate (savedinstancestate); the Setcontentview (r.layout.activity_main); -  -Mcontext =Getapplicationcontext (); -  +         //Get button object, bind event listener -Button syncbtn =(Button) Findviewbyid (R.ID.MAIN_SYNC_BTN1); +         //To bind a button control to an event listener ASyncbtn.setonclicklistener ( This); at     } -  -     //overrides the onclick (...) of the View.onclicklistener interface class. Method - @Override -      Public voidOnClick (view view) -     { in         //Select the UI element that triggers the Listener event view -         Switch(View.getid ()) to         { +              CaseR.ID.MAIN_SYNC_BTN1: -             { theEditText EditText =(EditText) Findviewbyid (R.ID.MAIN_INPUT_TEXT1); *TextView Showtext =(TextView) Findviewbyid (R.ID.MAIN_INPUT_TEXT2); $ Showtext.settext (Edittext.gettext ());Panax Notoginseng                  Break; -             } the             default: + Toast.maketext (Mcontext, Mcontext.getresources (). getString (r.string.disable_event), Toast.length_shor T). Show (); A                  Break; the         } +     } -  $}

PS: Note that in inheriting the View.onclicklistener interface class, you want to overwrite the onclick (...) Method. Also, event listener bindings are made to the control (Button) that triggered the event.

1 // Get button object, bind event listener 2 Button syncbtn = (Button) Findviewbyid (R.ID.MAIN_SYNC_BTN1); 3 // To bind a button control to an event listener 4 Syncbtn.setonclicklistener (this);

Android handles UI Events

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.