Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
696 views
in Technique[技术] by (71.8m points)

events - Difference between onKey(), OnKeyDown() and dispatchKeyEvent() methods provided by Android?

What is the difference between onKey(), OnKeyDown() and dispatchKeyEvent() methods provided by Android?
I Would like to know when and where each of these can be used.
Please shed some light into this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Tracing the source code of the 5.1 Source for the View Class. It would seem that dispatchKeyEvent() is the first method called by the system. Overloading it will prevent any and all key events from being called unless the base version is called.

dispatchKeyEvent()'s first move is to attempt to pass the event to an onKeyListener if there is one. This is when onKey() is called. If the onKey() implementation returns true, dispatchKeyEvent() will return there and other events will not be called.

If there is no onKeyListener or the onKeyListener's onKey() method returned false, dispatchKeyEvent() will then call the KeyEvent's dispatch() method. Which will then in turn call all the methods in the KeyEvent.Callback interface on your view. This includes onKeyDown() and onKeyUp().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...