Both the android keyboard and the touch event processing activity and view can receive the touch and button. If you want to respond to an event, you only need to re-write the event function in the inheritance class.
However, for a view, if we do not change draw, we do not need to inherit it. Therefore, if we want to respond to events, we need
When a view (such as a button) is touched, The ontouchevent () method on the object is called. However, to listen for this event, you must extend this class and override this method. Obviously, extending each view object you want to use (only processing an event) is absurd. This is why the View class also contains a set of nested interfaces, which contain callback functions that are much simpler to implement. These interfaces are called event listeners, which are used to intercept the "tickets" of user interaction with your interface ".
1. There is only one activity:
When the mouse key is pressed (touch)
First trigger dispatchtouchevent
Then, onuserinteraction is triggered.
Ontouchevent again
If you click, Followed by the following events (click in two steps, action_down, action_up)
Trigger dispatchtouchevent
Ontouchevent again
Onuserinteraction is not triggered when action_up event (you can viewSource code)
When the keyboard is pressed
First trigger dispatchkeyevent
Then, onuserinteraction is triggered.
Onkeydown again
If you press and then releaseIs two steps
Trigger dispatchkeyevent
Then, onuserinteraction is triggered.
Onkeyup again
Note that, unlike the touch, onuserinteraction is triggered when the button is released.
Activity. dispatchtouchevent (motionevent)-This allows your activity to capture all touch events before being distributed to the window.
(Similarly, dispatchkeyevent)
Onuserinteraction: called whenever a key, touch, or trackball event is dispatched to
* Activity.
2. There is a layout in the activity and a button in the layout.
If a click event is triggered on a button
First, trigger the dispatchtouchevent of the activity.
Then, the onuserinteraction of the activity is triggered.
Then, the dispatchtouchevent of layout is triggered.
Then, the onintercepttouchevent of layout is triggered.
Then trigger the ontouch of the button (this is an action_down event)
Followed by an action_up event
Trigger the dispatchtouchevent OF THE ACTIVITY
Note that the onuserinteraction of the activity will not be triggered because it does not work for action_up.
Then, the dispatchtouchevent of layout is triggered.
Then, the onintercepttouchevent of layout is triggered.
Then, the ontouch of the button is triggered.
Finally, The onclick of the button is triggered.
If you return true in the ontouch event and consume the event, onclick will not be responded
However, if you do not write an onclick event, the ontouch event returns flase
Then the final event sequence:
11-23 17:19:44. 313: INFO/activity (803): dispatchtouchevent
11-23 17:19:44. 313: INFO/activity (803): onuserinteraction
17:19:44 11-23. 322: INFO/linearlayout (803): dispatchtouchevent
11-23 17:19:44. 333: INFO/linearlayout (803): onintercepttouchevent
11-23 17:19:44. 341: INFO/button (803): ontouch
11-23 17:19:44. 441: INFO/activity (803): dispatchtouchevent
17:19:44 11-23. 451: INFO/linearlayout (803): dispatchtouchevent
11-23 17:19:44. 451: INFO/linearlayout (803): onintercepttouchevent
11-23 17:19:44. 461: INFO/button (803): ontouch
That is, the event will not be passed up. It is estimated that the onclick has a default response, but the onclick does not return a value.
However, if a view is inherited and ontouchevent is overwritten, false is returned.
When a touch event occurs
11-23 17:25:59. 691: INFO/activity (831): dispatchtouchevent
11-23 17:25:59. 691: INFO/activity (831): onuserinteraction
17:25:59 11-23. 701: INFO/linearlayout (831): dispatchtouchevent
11-23 17:25:59. 701: INFO/linearlayout (831): onintercepttouchevent
11-23 17:25:59. 701: INFO/button (831): ontouch
11-23 17:25:59. 701: INFO/button (831): ontouchevent
11-23 17:25:59. 701: INFO/linearlayout (831): ontouchevent
11-23 17:25:59. 701: INFO/activity (831): ontouchevent
11-23 17:25:59. 822: INFO/activity (831): dispatchtouchevent
11-23 17:25:59. 822: INFO/activity (831): ontouchevent
The strange thing is that the onclick event does not happen again.
Both view and activity have the onkeyup and onkeydown of corresponding Keyboard Events.
For view, you can setonkeylistener (New onkeylistener (){
@ Override
Public Boolean onkey (
To respond to Keyboard Events. If you write this listener to the view, overwrite the onkeyup and down events, first
Onkey
For example, you can use the reverse key to switch the focus to the button.
Event Sequence:
11-23 17:23:14. 392: INFO/activity (803): dispatchkeyevent
11-23 17:23:14. 404: INFO/activity (803): onuserinteraction
11-23 17:23:14. 412: INFO/linearlayout (803): dispatchkeyevent
11-23 17:23:14. 412: INFO/button (803): onkey
11-23 17:23:14. 422: INFO/activity (803): onkeyup
When press ENTER
11-23 17:35:55. 692: INFO/activity (831): dispatchkeyevent
11-23 17:35:55. 713: INFO/activity (831): onuserinteraction
11-23 17:35:55. 722: INFO/linearlayout (831): dispatchkeyevent
11-23 17:35:55. 732: INFO/button (831): onkey
11-23 17:35:55. 813: INFO/activity (831): dispatchkeyevent
11-23 17:35:55. 824: INFO/activity (831): onuserinteraction
11-23 17:35:55. 831: INFO/linearlayout (831): dispatchkeyevent
11-23 17:35:55. 831: INFO/button (831): onkey
11-23 17:35:55. 953: INFO/button (831): onclick
Finally, I want to add: it does not need to be so complicated. If the control wants to respond to its touch event, if it inherits
The ontouchevent is overwritten. If the event is not overwritten, The ontouch listener is listened.
If ontouchevent is overwritten, The onclick event is no longer returned.
If onclick is written, do not engage in ontouchevent any more.
So onclick and ontouchevent take one. In addition, if ontouch listens, this function must be executed before other functions.
For onclick, ontouch should be taken from the beginning, one is down, and the other is up
For the same onkey, as well as onkeyup and down
If both onkeyuodown and onkey are overwritten, the onkey should be executed first. When a carriage return key is triggered
11-24 09:55:18. 601: INFO/activity (951): dispatchkeyevent
11-24 09:55:18. 611: INFO/activity (951): onuserinteraction
11-24 09:55:18. 621: INFO/linearlayout (951): dispatchkeyevent
11-24 09:55:18. 641: INFO/button (951): onkey
11-24 09:55:18. 711: INFO/activity (951): dispatchkeyevent
11-24 09:55:18. 756: INFO/activity (951): onuserinteraction
11-24 09:55:18. 775: INFO/linearlayout (951): dispatchkeyevent
11-24 09:55:18. 791: INFO/button (951): onkey
11-24 09:55:18. 822: INFO/button (951): onclick