Zookeeper Android event processing model 1 (callback-based event processing)

Source: Internet
Author: User

There are two types of event processing mechanisms on the Android platform: one is based on the callback mechanism and the other is based on the listener interface. The first one is based on the callback mechanism.
On the Android platform, each view has its own callback Method for event processing. developers can rewrite these callback methods in the view to implement the required response events. When an event is not processed by any view, the corresponding callback method in the activity is called. Android provides the following callback methods for users:
1. onkeydown:
Function: This method is an abstract method in the keyevent. Callback interface. All views implement this interface and override this method. This method is used to capture the events when the mobile phone keyboard is pressed.
Declaration: Public Boolean onkeydown (INT keycode, keyevent event)
Parameter description:
The keycode parameter. this parameter is the pressed key value, that is, the keyboard code. Each button on the mobile phone keyboard has its own keyboard code, only through the keyboard code can the app know which key the user presses.
The event parameter. this parameter is the key event object, which contains the details of the trigger event, such as the event status, event type, and event occurrence time. When you press the button, the system will automatically block the event as a keyevent object for use by the program.
Return value. The return value of this method is a Boolean variable. If true is returned, it indicates that the event has been fully processed and other callback methods are not expected to be processed again, when "false" is returned, the event is not completely processed, and other callback methods are expected to continue to process the event, such as the callback method in the activity.

2. onkeyup:
Function: This method is also the keyevent interface. an abstract method in callback, and all views also implement this interface and override this method. The onkeyup method is used to capture the events raised by the keyboard of the mobile phone.
Declaration: Public Boolean onkeyup (INT keycode, keyevent event)
Parameter description: Same as onkeydown

3. ontouchevent:

Function: This method is defined in the View class, And all view subclasses overwrite this method. The application can use this method to process touch events on the mobile phone screen.

Declaration: Public Boolean ontouchevent (motionevent event)

Parameter description:

Parameter event: the parameter event is an object of the mobile phone screen touch event encapsulation class, which encapsulates all information about the event, such as the touch position, touch type, and touch time. This object will be created when you touch the mobile phone screen.

Return Value: the return value mechanism of this method is the same as that of the keyboard response event. It also returns true if the event has been completely processed and other callback methods are not expected to be processed again. Otherwise, false is returned.

Detailed description:

This method does not handle only one event as described previously. Generally, all the events in the following three cases are handled by the ontouchevent method, but the action values are different in three cases.

Screen pressed: when the screen is pressed, this method is automatically called to handle the event. In this case, motionevent. the value of getaction () is motionevent. action_down: If you need to process the screen-pressed event in the application, you only need to re-call the callback method and then judge the action in the method.
Screen lift: The event triggered when the touch pen leaves the screen. The event also needs to be captured by the ontouchevent method, and then the action is judged in the method. When the value of motionevent. getaction () is motionevent. action_up, it indicates that the screen is lifted.
Drag on the screen: This method is also responsible for processing the event where the touch pen slides on the screen. It also calls the motionevent. getaction () method to determine whether the action value is motionevent. action_move and then process it.

4. ontrackballevent:
Function: Next we will introduce the ontrackballevent method for handling trackball in the mobile phone. All views also implement this method.
Declaration: Public Boolean ontrackballevent (motionevent event)
Details: The method is used in the same way as the callback methods described earlier. You can rewrite the method in the activity or in the Implementation classes of each view.
Parameter event: the event parameter is an object of the mobile phone trackball event encapsulation class, which encapsulates the details of the trigger event, including the event type and trigger time. Generally, this object will be created when the user controls the trackball.
Return Value: the return value of this method is exactly the same as the return value mechanism of each callback method described earlier. Due to the limited length of this book, we will not repeat it here.
The differences between trackball and mobile keyboard are as follows:
1) The trackball designed by some models of mobile phones is more beautiful than the mobile phone keyboard, which can increase the overall impression of the mobile phone.
2) trackball is easier to use. For example, it is more reasonable to use trackball control in some games.
3) when you use a trackball, it is more refined than the keyboard. That is, when you scroll the trackball, the value indicating the status in the background changes slightly and accurately.
Tip: When the simulator is running, you can use the F6 key to open the trackball of the simulator, and then move the mouse to simulate the trackball event.

5. onfocuschanged:
Function: the methods described earlier can be rewritten in view and activity. The onfocuschanged method described later can only be rewritten in view. This method is a focus change callback method. When a control overwrites this method, when the focus changes, this method is automatically called to handle the focus change events.
Declaration: protected void onfocuschanged (Boolean gainfocus, int direction, rect previously focusedrect)
Detailed description:
Gainfocus: The gainfocus parameter indicates whether the view that triggers the event gets the focus. When the control gets the focus, gainfocus is equal to true; otherwise, it is equal to false.
Direction: The direction parameter indicates the direction of Focus movement. It is expressed by numerical values. Interested readers can rewrite this method in view to print this parameter for observation.
Parameter previuslyfocusedrect: indicates the first rectangular area that obtains the focus in the view coordinate system that triggers the event, that is, where the focus comes from. If not, it is null.
Tip:
Focus: Focus describes the receiver of a key event (or screen event). Each key event occurs in a view with a focus. In an application, we can control the focus, for example, moving one view from one view to another. The following lists some common focus-related methods:
Setfocusable: sets whether a view can have a focus.
Isfocusable method: checks whether the view can have focus.
Setnextfocusdownid method: sets the focus of the view to move down to get the focus view ID.
Hasfocus method: returns whether the parent control of the view obtains the focus.
Requestfocus method: Try to get the focus of this view.
Isfocusabletouchmode: sets whether the view can obtain the focus in touch mode. By default, the view is unavailable.

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.