In the four major Android components (activity, Service, broadcast, ContentProvider), the most commonly used is activity. Because activity is responsible for providing intuitive pages and responding to user actions. In the activity's layout file, a colorful user page is formed through layers of nesting, layout, and view (controls) of the outermost viewgroup (layout). such as QQ, and so on. In the process of developing these pages, it is inevitable that there will be some event collisions (saying that you want to click on a layout or control and find that the response is another layout or control). Oh, how to do? This brings us to the question we'll discuss next.
When responding to a user's click, the phone responds to the specified ViewGroup or view (consuming this event) by following certain rules from the activity entrance. Only by finding out the rules and dealing with such incidents again in the future will it be handy. From a procedural point of view, Android offers three ways to handle event issues dispatchtouchevent (distribution click events), onintercepttouchevent (Intercept click events), ontouchevent (Handling click events). Each method returns TRUE or false to indicate whether it handles its corresponding responsibilities. For example, if I intercept an event, it means that I want to handle the event and not allow another control to receive the event signal. In addition to returning TRUE or FALSE, you can also execute the parent class's logic by calling the parent class's method. In short, activity, ViewGroup, view three classes handle events, the related methods are dispatchtouchevent,onintercepttouchevent, Ontouchevent and the return of each method can be true, false, and super. Note: Activity and view do not intercept event methods.
Write a test demo, a layout file with LinearLayout nested relativelayout, and finally include a button in Relativelayout. In each of the event handling methods, output the log logs sequentially. Click on the button test to analyze the event distribution process through log information. is the layout effect.
For the convenience of description, the custom naming rules: Activity shorthand a,linearlayout shorthand l,relativelayout shorthand R,button shorthand b,dispatchtouchevent shorthand D, Onintercepttouchevent shorthand I,ontouchevent T, return true shorthand T, return false abbreviated F, return super shorthand S. For example, ads indicates that the Dispatchtouchevent method in activity is return super.
Test Scenario One:ads/ATS, Lds/lis/LTS, Rds/ris/rts, BDs/bts
The logs are as follows:
04-01 12:44:44.402:d/activity (1234): Dispatchtouchevent action_down04-01 12:44:44.402:d/mylinearlayout (1234): Dispatchtouchevent action_down04-01 12:44:44.402:d/mylinearlayout (1234): Onintercepttouchevent ACTION_DOWN04-01 12:44:44.402:d/myrelativelayout (1234): Dispatchtouchevent action_down04-01 12:44:44.402:d/myrelativelayout (1234): Onintercepttouchevent action_down04-01 12:44:44.402:d/mybutton (1234): Dispatchtouchevent ACTION_DOWN04-01 12:44:44.402:d/mybutton (1234): Ontouchevent action_down04-01 12:44:44.506:d/activity (1234): DispatchTouchEvent action_up04-01 12:44:44.506:d/mylinearlayout (1234): Dispatchtouchevent action_up04-01 12:44:44.506:d/ Mylinearlayout (1234): Onintercepttouchevent action_up04-01 12:44:44.506:d/myrelativelayout (1234): Dispatchtouchevent action_up04-01 12:44:44.506:d/myrelativelayout (1234): Onintercepttouchevent ACTION_UP04-01 12:44:44.506:d/mybutton (1234): Dispatchtouchevent action_up04-01 12:44:44.506:d/mybutton (1234): OnTouchEvent ACTION _up04-0112:44:44.506:d/button (1234): click
Sort out as follows: three-color arrow solid line, indicating that the corresponding method returns a value jumps to the next method. Where button returns True and returns super will consume events, just return true to no longer respond to button click events.
Test Scenario II: Ads/ats, Lds/lis/lts, Rds/ris/rts, BDS/BTF
04-01 14:00:54.970:d/activity (1387): Dispatchtouchevent action_down04-01 14:00:54.970:d/mylinearlayout (1387): Dispatchtouchevent action_down04-01 14:00:54.970:d/mylinearlayout (1387): Onintercepttouchevent ACTION_DOWN04-01 14:00:54.970:d/myrelativelayout (1387): Dispatchtouchevent action_down04-01 14:00:54.970:d/myrelativelayout (1387): Onintercepttouchevent action_down04-01 14:00:54.970:d/mybutton (1387): Dispatchtouchevent ACTION_DOWN04-01 14:00:54.970:d/mybutton (1387): Ontouchevent action_down04-01 14:00:54.970:d/myrelativelayout (1387): OnTouchEvent action_down04-01 14:00:54.970:d/mylinearlayout (1387): Ontouchevent action_down04-01 14:00:54.970:d/activity (1387): Ontouchevent action_down04-01 14:00:55.066:d/activity (1387): Dispatchtouchevent action_up04-01 14:00:55.066:d/ Activity (1387): Ontouchevent action_up
Description: When the button's ontouchevent return is false, the Click event returns to the previous layer view. If there has been no control handling this event will be given to Activitiy's ontouchevent consumption. When re-entering the event, no longer follow the previous process, each space is processed once, but directly to the Activity of the ontouchevent consumption. Is the dotted line point.
Test Scenario Three: Ads/ats, Lds/lis/lts, Rds/ris/rts, BDT/BTF
04-01 14:12:47.918:d/activity (1444): Dispatchtouchevent action_down04-01 14:12:47.918:d/mylinearlayout (1444): Dispatchtouchevent action_down04-01 14:12:47.918:d/mylinearlayout (1444): Onintercepttouchevent ACTION_DOWN04-01 14:12:47.918:d/myrelativelayout (1444): Dispatchtouchevent action_down04-01 14:12:47.918:d/myrelativelayout (1444): Onintercepttouchevent action_down04-01 14:12:47.918:d/mybutton (1444): Dispatchtouchevent ACTION_DOWN04-01 14:12:48.030:d/activity (1444): Dispatchtouchevent action_up04-01 14:12:48.030:d/mylinearlayout (1444): Dispatchtouchevent action_up04-01 14:12:48.030:d/mylinearlayout (1444): Onintercepttouchevent ACTION_UP04-01 14:12:48.030:d/myrelativelayout (1444): Dispatchtouchevent action_up04-01 14:12:48.030:d/myrelativelayout (1444): Onintercepttouchevent action_up04-01 14:12:48.030:d/mybutton (1444): Dispatchtouchevent ACTION_UP
Description: The Button Dispatchtouchevent method returns True to indicate that it needs to consume the event. and is not passed to the Ontouchevent method.
Test Scenario Four: Ads/ats, Lds/lis/lts, Rds/ris/rts, BDF/BTF
04-01 14:19:40.014:d/activity (1502): Dispatchtouchevent action_down04-01 14:19:40.014:d/mylinearlayout (1502): Dispatchtouchevent action_down04-01 14:19:40.014:d/mylinearlayout (1502): Onintercepttouchevent ACTION_DOWN04-01 14:19:40.014:d/myrelativelayout (1502): Dispatchtouchevent action_down04-01 14:19:40.014:d/myrelativelayout (1502): Onintercepttouchevent action_down04-01 14:19:40.014:d/mybutton (1502): Dispatchtouchevent ACTION_DOWN04-01 14:19:40.014:d/myrelativelayout (1502): Ontouchevent action_down04-01 14:19:40.014:d/mylinearlayout (1502): Ontouchevent action_down04-01 14:19:40.014:d/activity (1502): Ontouchevent action_down04-01 14:19:40.022:d/activity ( 1502): Dispatchtouchevent action_up04-01 14:19:40.022:d/activity (1502): Ontouchevent ACTION_UP
Description: The Button Dispatchtouchevent method returns false and the event is passed to the parent control Ontouchevent method.
Test Plan five: Ads/ats, Lds/lis/lts, Rds/ris/rtt, BDF/BTF
04-01 14:26:03.306:d/activity (1561): Dispatchtouchevent action_down04-01 14:26:03.306:d/mylinearlayout (1561): Dispatchtouchevent action_down04-01 14:26:03.306:d/mylinearlayout (1561): Onintercepttouchevent ACTION_DOWN04-01 14:26:03.306:d/myrelativelayout (1561): Dispatchtouchevent action_down04-01 14:26:03.306:d/myrelativelayout (1561): Onintercepttouchevent action_down04-01 14:26:03.306:d/mybutton (1561): Dispatchtouchevent ACTION_DOWN04-01 14:26:03.306:d/myrelativelayout (1561): Ontouchevent action_down04-01 14:26:03.366:d/activity (1561): Dispatchtouchevent action_up04-01 14:26:03.366:d/mylinearlayout (1561): Dispatchtouchevent ACTION_UP04-01 14:26:03.366:d/mylinearlayout (1561): Onintercepttouchevent action_up04-01 14:26:03.366:d/myrelativelayout (1561): Dispatchtouchevent action_up04-01 14:26:03.366:d/myrelativelayout (1561): Ontouchevent ACTION_UP
Description: Relativelayout's Ontouchevent method return true consumes the event, and the next time the event is generated, it is not passed to the button, directly to Relativelayout's Ontouchevent method.
Test Scenario VI: Ads/ats, Lds/lis/lts, Rds/ris/rtf, BDF/BTF
04-01 14:39:20.794:d/activity (1644): Dispatchtouchevent action_down04-01 14:39:20.798:d/mylinearlayout (1644): Dispatchtouchevent action_down04-01 14:39:20.798:d/mylinearlayout (1644): Onintercepttouchevent ACTION_DOWN04-01 14:39:20.798:d/myrelativelayout (1644): Dispatchtouchevent action_down04-01 14:39:20.798:d/myrelativelayout (1644): Onintercepttouchevent action_down04-01 14:39:20.798:d/mybutton (1644): Dispatchtouchevent ACTION_DOWN04-01 14:39:20.798:d/myrelativelayout (1644): Ontouchevent action_down04-01 14:39:20.798:d/mylinearlayout (1644): Ontouchevent action_down04-01 14:39:20.798:d/activity (1644): Ontouchevent action_down04-01 14:39:20.874:d/activity ( 1644): Dispatchtouchevent action_up04-01 14:39:20.874:d/activity (1644): Ontouchevent ACTION_UP
Description: Relativelayout return False, the event is passed to the previous level of control consumption. where Relativelayout Onintercepttouchevent return false indicates that the event is also distributed to button processing.
Test Plan VII: Ads/ats, Lds/lis/lts, Rds/rit/rtf, BDF/BTF
04-01 15:02:41.594:d/activity (1739): Dispatchtouchevent action_down04-01 15:02:41.594:d/mylinearlayout (1739): Dispatchtouchevent action_down04-01 15:02:41.594:d/mylinearlayout (1739): Onintercepttouchevent ACTION_DOWN04-01 15:02:41.594:d/myrelativelayout (1739): Dispatchtouchevent action_down04-01 15:02:41.594:d/myrelativelayout (1739): Onintercepttouchevent action_down04-01 15:02:41.594:d/myrelativelayout (1739): Ontouchevent ACTION_DOWN04-01 15:02:41.594:d/mylinearlayout (1739): Ontouchevent action_down04-01 15:02:41.594:d/activity (1739): OnTouchEvent action_down04-01 15:02:41.714:d/activity (1739): Dispatchtouchevent action_up04-01 15:02:41.714:d/activity (1739): Ontouchevent action_up
Description: Relativelayout Onintercepttouchevent Returns True, indicating that the Relativelayout intercepts the event information, the button does not respond to the event, and the event is referred to Relativelayout Ontouchevent processing.
Test Scenario Eight: ads/ats, Lds/lis/lts, Rdt/rit/rtf, BDF/BTF
04-01 15:27:17.002:d/activity (1907): Dispatchtouchevent action_down04-01 15:27:17.002:d/mylinearlayout (1907): Dispatchtouchevent action_down04-01 15:27:17.002:d/mylinearlayout (1907): Onintercepttouchevent ACTION_DOWN04-01 15:27:17.002:d/myrelativelayout (1907): Dispatchtouchevent action_down04-01 15:27:17.058:d/activity (1907): Dispatchtouchevent action_up04-01 15:27:17.058:d/mylinearlayout (1907): Dispatchtouchevent ACTION_UP04-01 15:27:17.058:d/mylinearlayout (1907): Onintercepttouchevent action_up04-01 15:27:17.058:d/myrelativelayout (1907): Dispatchtouchevent action_up
Description: Relativelayout Dispatchtouchevent return True indicates consumption events and is no longer delivered
test Plan IX: ads/ats, Lds/lis/lts, Rdf/rit/rtf, BDF/BTF
04-01 15:32:12.138:d/activity (1965): Dispatchtouchevent action_down04-01 15:32:12.138:d/mylinearlayout (1965): Dispatchtouchevent action_down04-01 15:32:12.138:d/mylinearlayout (1965): Onintercepttouchevent ACTION_DOWN04-01 15:32:12.138:d/myrelativelayout (1965): Dispatchtouchevent action_down04-01 15:32:12.138:d/mylinearlayout (1965): Ontouchevent action_down04-01 15:32:12.138:d/activity (1965): Ontouchevent action_down04-01 15:32:12.250:d/activity ( 1965): Dispatchtouchevent action_up04-01 15:32:12.250:d/activity (1965): Ontouchevent ACTION_UP
Description: Relativelayout dispatchtouchevent return False indicates that the event is not handled and is assigned to the upper page processing.
test Scenario Ten: adt/ats, Lds/lis/lts, Rdf/rit/rtf, BDF/BTF
04-01 15:43:24.558:d/activity (2023): Dispatchtouchevent action_down04-01 15:43:24.686:d/activity (2023): Dispatchtouchevent action_up
Description: As with the ViewGroup and view Dispatchtouchevent methods, return True indicates consumption events. The difference is that the Activity's Dispatchtouchevent method return False also represents its own consumption event.
Test Scenario 11: ads/ats, Lds/lis/lts, Rdf/rit/rtf, BDF/BTF
04-01 15:56:51.486:d/activity (2149): Dispatchtouchevent action_down04-01 15:56:51.486:d/mylinearlayout (2149): Dispatchtouchevent action_down04-01 15:56:51.486:d/mylinearlayout (2149): Onintercepttouchevent ACTION_DOWN04-01 15:56:51.486:d/myrelativelayout (2149): Dispatchtouchevent action_down04-01 15:56:51.486:d/mylinearlayout (2149): Ontouchevent action_down04-01 15:56:51.486:d/activity (2149): Ontouchevent action_down04-01 15:56:51.490:d/activity ( 2149): Dispatchtouchevent action_up04-01 15:56:51.490:d/activity (2149): Ontouchevent ACTION_UP
Description: In fact, as long as the Ontouchevent method passed to the activity, it will be referred to it to handle the event.
Referring to the previous understanding of the idea, the whole will be perfected as follows:
Summarize:
The 1.Activity dispatchtouchevent method, when the parent class Dispatchtouchevent method is executed, distributes the event to ViewGroup, otherwise consumes the event itself.
2. When the event is passed again to the activity's Ontouchevent method, it means that all controls in the page do not respond to the event, which is handled by the activity.
The Dispatchtouchevent method of 3.ViewGroup and view, return True indicates that the event is not distributed and handled by itself. Return false passes the event to the Ontouchevent method of the upper control and no longer responds to events passed by the upper control.
The Onintercepttouchevent method of 4.ViewGroup return True indicates that the event is intercepted and the event is passed to its own Ontouchevent method for processing.
The Ontouchevent method of 5.View return false means that events are not consumed, and events are passed to the Ontouchevent method of the upper control. Otherwise, consumer events.
The Ontouchevent method of 6.ViewGroup return True indicates a consumption event, otherwise the event is passed to the upper control or the activity's Ontouchevent method.
7. When the control does not handle the event, it no longer receives the next event message, as shown in the dashed line.
Finally, I wish you all the program Ape/yuan, yards farmers, siege Lions happy April Fools ' Day.
Android Event Distribution