注意:在 Android 框架中会调用event handlers先处理事件,然后会适当的传递给二级默认的预定义handlers中;因此 如果返回true,将会停止这个事件的传递,View中默认事件处理方法的回调也会被阻止。因此,当你返回true肯定表明你是要终止这个事件的延续。(这个地方有点不懂。。。原文是:Android will call event handlers first and then the appropriate default handlers from the class definition second. As such, returning true from these event listeners will stop the propagation of the event to other event listeners and will also block the callback to the default event handler in the View. So be certain that you want to terminate the event when you return true.)
Event Handlers
如果您建立一个继承于View自定义组件,然后您可以定义一些回调方法用作默认的事件处理程序。该文件中关于Building Custom Components,您会学习一些共用的回调方法用于事件处理,其中包括:
· 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.
还有其他一些方法,这不属于View类,但可以直接影响到你处理事件的方式,所以在布局内管理更复杂的事件可以考虑到这些方法:
· 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 ViewGroup to 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).
Touch Mode
当用户在使用方向键或轨迹球浏览用户界面时,有必要给于一个焦点在可操作的组件上(如一个Button),使用户可以看到它将接受输入命令。如果设备有触摸功能,那么,当用户与界面的交互就不再需要有一个高亮在组件上,或一个焦点在view上,因此,模式的互动名为"触摸模式"。对于一个触摸设备,一旦有用户接触屏幕时,该设备将进入触摸模式.在点触某个View后,只有的它的方法isFocusableInTouchMode()返回为真时,才会有聚集焦点,如文本编辑工具。其他的界面只可以点触,但不会聚集焦点(高亮),如button 被点触时就不会聚集焦点,当它被按下时只会调用on-click监听器的回调方法。
任何时候用户接触方向键或者滚动轨迹球时,该设备将退出触摸模式,并聚集焦点,用户可以恢复与用户界面的键盘交互,而不必在屏幕上。触摸模式的状态是由整个系统来维持的(all windows and activities),要查询目前所处的状态,你可以调用isInTouchMode()方法来获得,看看设备目前是否处于触摸模式。
Handling Focus
系统框架将处理日常的焦点移动来响应用户的输入,它包刮改变焦点(当界面是被移除,隐藏,或者作为一个新的View变为可用状态),通过 isFocusable()这个方法我们可以知道view是否具有接受焦点的资格,也可以通过setFocusable().来设置view接受焦点的资 格,对应在触摸模式下,你可以调用isFocusableInTouchMode().来获知是否有焦点来响应点触,也可以通过 setFocusableInTouchMode().来设置是否有焦点来响应点触的资格.
一般来说,在这个垂直布局,浏览的焦点会从第一个按钮开始,不会是从第二个或者其他的,现在topButtont已经通过nextFocusUp (反之亦然)确定了bottom.
通常如果你想宣布用户界面具有焦点的资格 (如果这个界面在传统上是没有的),可以在xml布局里去加上的android:focusable的属性,并设置它的值,您也可以宣布在触摸模式下具有焦点的资格,同样也只在xml里添android:focusableInTouchMode.的属性,并设置它的值. 当用户请求在某个界面聚集焦点时,会调用requestFocus().这个方法。监听到焦点活动(获得焦点或失去焦点都会被通知),会调用onFocusChange(),这个方法,这也是上节所讨论的Event Listeners。