The event handling mechanism of Android (i)------event handling based on callback mechanism

Source: Internet
Author: User

There are two kinds of event handling mechanism for Android platform, one is based on callback mechanism, one is based on listener interface, and the first is: Event processing based on callback mechanism.
In the Android platform, each view has its own callback method for handling events, and developers can implement the required response events by overriding these callback methods in view. When an event is not processed by any view, the corresponding callback method in the activity is invoked. Android provides the following callback methods for use by the user:
1. OnKeyDown:
Function: This method is an abstract method in interface Keyevent.callback, all view implements the interface and re-writes the method, which is used to catch the event that the phone keyboard is pressed.
Declaration: Public boolean OnKeyDown (int keycode, keyevent event)
Parameter description:
Parameter keycode, this parameter is the key value that is pressed is the keyboard code, each button in the mobile phone keyboard has its own keyboard code, in the application all through the keyboard code to know which key the user presses.
The parameter event, which is the object of the key events that contains the details of the triggering event, such as the state of the event, the type of event, the time the event occurred, and so on. When the user presses the key, the system automatically encapsulates the event as an KeyEvent object for use by the application.
return value, the return value of the method is a variable of type Boolean, when True indicates that the event has been fully processed, does not want the other callback methods to be processed again, and when it returns false, indicates that the event is not fully processed. It is more desirable that other callback methods continue to process it, such as the callback method in activity.

2. OnKeyUp:
Function: This method is also an abstract method in interface Keyevent.callback, and all view also implements the interface and re-writes the method, and the OnKeyUp method is used to capture the events raised by the keyboard keys of the phone.
Declaration: Public boolean onKeyUp (int keycode, keyevent event)
Parameter description: Same onkeydown

3. Ontouchevent:

Function: The method is defined in the view class, and all of the view subclasses override this method, which the application can use to handle touch events on the phone screen.

Declaration: Public boolean Ontouchevent (Motionevent event)

Parameter description:

Parameter event: The event encapsulates the object for the mobile screen touch events wrapper class, which encapsulates all the information about the event, such as the location of the touch, the type of touch, and the time of the touch. The object is created when the user touches the screen of the phone.

Return value: The mechanism of the return value of the method is the same as the keyboard response event, which returns True when the event has been completely processed and does not want the other callback methods to be processed again, otherwise false.

Detailed Description:

This method does not deal with only one event as described previously, and in general the following three cases are all handled by the Ontouchevent method, except that the action values in three cases are different.

The screen is pressed: when the screen is pressed, the method is automatically called to handle the event, at which point the value of Motionevent.getaction () is Motionevent.action_down, and if the event that the screen is pressed is required to be processed in the application, simply re-call the callback method , then the action in the method can be judged.
The screen is lifted: The event that is triggered when the stylus leaves the screen, the event also requires the Ontouchevent method to capture, and then the action is judged in the method. When the value of Motionevent.getaction () is motionevent.action_up, it is the event that the screen is lifted.
Drag in the screen: This method also handles the event that the stylus slides on the screen, and also calls the Motionevent.getaction () method to determine whether the action value is motionevent.action_move and then processed.

4. Ontrackballevent:
Function: Next will be introduced is the mobile phone in the processing method of the Trackball ontrackballevent. All of the view also implements the method.
Declaration: Public boolean Ontrackballevent (Motionevent event)
This method is used in the same way as the previous callback methods described above, can be overridden in the activity of the method, can also be overridden in the individual view implementation class.
Parameter event: The parameter event is the object of the mobile Trackball event encapsulation class, which encapsulates the details of the triggering event, including the type of event, the trigger time, and so on, in general, the object is created when the user manipulates the trackball.
Return value: The return value of the method is exactly the same as the return value mechanism for each callback method described earlier, because the book is limited in length and is not covered.
The difference between the trackball and the phone keypad is as follows:
1) Some models of mobile phone design of the trackball will be more beautiful than the only phone keyboard, can add users to the overall impression of the phone.
2) It is easier to use trackball, for example, it is more reasonable to use trackball control in some games.
3) The use of trackball will be more granular than the keyboard, that is, when scrolling the trackball, the background of the value of the representation state will change more subtle, more accurate.
Tip: In the emulator running state, you can open the simulator's trackball through the F6 key, and then you can simulate the Trackball event by moving the mouse.

5. Onfocuschanged:
Functions: Each of the methods described above can be overridden in view and activity, and the following onfocuschanged are only rewritten in view. This method is a callback method that changes focus, and when a control overrides the method, the method is automatically called when the focus changes to handle the event of focus change.
Declaration: protected void Onfocuschanged (Boolean gainfocus, int direction, Rect previously Focusedrect)
Detailed Description:
Parameter gainfocus: The parameter gainfocus indicates whether the view that triggered the event has the focus, and Gainfocus equals True when the control receives the focus, otherwise equals false.
Parameter direction: The parameter direction represents the direction of the focus movement, expressed numerically, and interested readers can override the method in view to print the parameter for observation.
Parameter Previouslyfocusedrect: Represents the field in the coordinate system of the view that triggered the event, the previous rectangle that gets the focus, that is, where the focus is coming from. Null if it is not available.
Tips:
Focus: The focus describes the subject of key events (or screen events, etc.), and each keystroke event occurs on the view that has the focus. In the application, we can control the focus, such as moving another view from one view. Here are some common approaches to focus:
Setfocusable method: Sets whether the view can have focus.
Isfocusable Method: Monitor Whether this view can have focus.
Setnextfocusdownid method: Set the view's focus down to get the ID of the focus view.
Hasfocus method: Returns whether the parent control of the view has the focus.
Requestfocus method: Try to get the focus for this view.
Isfocusabletouchmode method: Sets whether the view can get focus in touch mode, which is not available by default.

The event handling mechanism of Android (i)------event handling based on callback mechanism

Related Article

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.