1. In order to test, we also set the view OnTouch ontouchevent OnClick onlongclick four events, after the printing test found that the key distribution process is this if the short press:ontouch-->> Ontouchevent--->>onclick. Long press: ontouch-->>ontouchevent--->>onlongclick-->>onclick. Why would that be?
We look at the view source
public boolean dispatchtouchevent (Motionevent event) { if (minputeventconsistencyverifier! = null) { Minputeventconsistencyverifier.ontouchevent (event, 0); } if (onfiltertoucheventforsecurity (event)) { //noinspection simplifiableifstatement listenerinfo li = Mlistenerinfo; if (Li! = null && Li.montouchlistener! = null && (mviewflags & enabled_mask) = = ENABLED && Li.mOnTouchListener.onTouch (this, event)) { return true; } if (Ontouchevent (event)) { return true; } } if (minputeventconsistencyverifier! = null) { minputeventconsistencyverifier.onunhandledevent (event, 0); } return false; }
Analysis: In the event distribution method, first execute
if (Li! = null && Li.montouchlistener! = null && (mviewflags & enabled_mask) = = ENABLED && Li.mOnTouchListener.onTouch (this, event)) { return true; }
If the OnTouch return value is true, then the event is consumed here. The event is not distributed, the Ontouchevent event is not executed, and subsequent events are not executed. If the Ontouch return value is False, the event continues to be distributed, executing
if (Ontouchevent (event)) { return true; }
The Onlongclick event is then executed before the onclick event is executed. As long as the previous key event is not consumed, the event will continue to be distributed. Until the end of consumption.
2. Similarly to the view set OnKey OnKeyDown OnClick and other events, the same distribution process is: OnKey-->>onkeydown--->>onclick, originally ibid.
Records are for ease of reference only.
Android View key event distribution process OnTouch ontouchevent onclick Onlongclick and OnKey onKeyDown onclick