Android提供的onKey()、OnKeyDown()和dispatchKeyEvent()方法有什么区别?
我想知道这些都可以在何时何地使用。
请对此进行一些说明。
追踪源代码5.1 视图类的源码 http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/view/View.java/。看来dispatchKeyEvent() http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/view/View.java/#View.dispatchKeyEvent%28android.view.KeyEvent%29是系统调用的第一个方法。重载它将阻止调用任何和所有关键事件,除非调用基本版本。
dispatchKeyEvent()
的第一步是尝试将事件传递给onKeyListener http://developer.android.com/reference/android/view/View.OnKeyListener.html如果有的话。这是当onKey()
叫做。如果onKey()
实施回报true
, dispatchKeyEvent()
将返回那里并且其他事件将不会被调用。
如果没有onKeyListener
or the onKeyListener
's onKey()
方法返回false
, dispatchKeyEvent()
然后会调用KeyEvent
's dispatch() http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/view/KeyEvent.java#KeyEvent.dispatch%28android.view.KeyEvent.Callback%2Candroid.view.KeyEvent.DispatcherState%2Cjava.lang.Object%29方法。然后将依次调用中的所有方法KeyEvent.Callback http://developer.android.com/reference/android/view/KeyEvent.Callback.html界面在您的视图中。这包括onKeyDown() http://developer.android.com/reference/android/view/View.html#onKeyDown%28int,%20android.view.KeyEvent%29 and onKeyUp() http://developer.android.com/reference/android/view/View.html#onKeyUp%28int,%20android.view.KeyEvent%29.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)