Event Handlers

If you're building a custom component from View, then you'll be able to define several callback methods used as default event handlers. In the document on Building Custom Components, you'll learn see some of the common callbacks used for event handling, including:

• onKeyDown(int, KeyEvent) - Called when a new key event occurs.

• onKeyUp(int, KeyEvent) - Called when a key up event occurs.

• onTrackballEvent(MotionEvent) - Called when a trackball motion event occurs.

• onTouchEvent(MotionEvent) - Called when a touch screen motion event occurs.

• onFocusChanged(boolean, int, Rect) - Called when the view gains or loses focus.

There are some other methods that you should be awere of, which are not part of the View class, but can directly impact the way you're able to handle events. So, when managing more complex events inside a layout, consider these other methods:

• Activity.dispatchTouchEvent(MotionEvent) - This allows your Activity to intercept all touch events before they are dispatched to the window.

• ViewGroup.onInterceptTouchEvent(MotionEvent) - This allows a ViewGroupto watch events as they are dispatched to child Views.

• ViewParent.requestDisallowInterceptTouchEvent(boolean) - Call this upon a parent View to indicate that it should not intercept touch events with onInterceptTouchEvent(MotionEvent) .

• onLongClick() - This returns a boolean to indicate whether you have consumed the event and it should not be carried further. That is, return true to indicate that you have handled the event and it should stop here; return false if you have not handled it and/or the event should continue to any other on-click listeners.

• onKey() - This returns a boolean to indicate whether you have consumed the event and it should not be carried further. That is, return true to indicate that you have handled the event and it should stop here; return false if you have not handled it and/or the event should continue to any other on-key listeners.

• onTouch() - This returns a boolean to indicate whether your listener consumes this event. The important thing is that this event can have multiple actions that follow each other. So, if you return false when the down action event is received, you indicate that you have not consumed the event and are also not interested in subsequent actions from this event. Thus, you will not be called for any other actions within the event, such as a fingure gesture, or the eventual up action event.

0 0

Post a comment