Hi, everyone. Today I will share with you the onintercepttouchevent and ontouchevent in Android. I remember when I first started looking for a job, I was asked about the event transfer in Android, at that time, I was a cainiao. Of course I couldn't answer the question. I was so heartless. Let's get down to the point.
Onintercepttouchevent:
Onintercepttouchevent is defined in viewgroup. The layout class in Android generally inherits this class. Onintercepttouchevent is used to intercept gesture events. Each gesture event first calls onintercepttouchevent.
Ontouchevent:
Ontouchevent is also a method defined in view. Process the gesture events passed to the view. The types of gesture events include action_down, action_move, action_up, and action_cancel.
The default value of onintercepttouchevent in layout is false, so that the touch event is passed to the view control. The default value of ontouch in layout is false, and the default value of ontouch in view is true, when we click the screen with our fingers, we call the action_down event first. When the return value in ontouch is true, ontouch continues to call the action_up event. If the return value in ontouch is false, ontouch only calls action_down instead of action_up.
In order to make it easier for the House to understand, I wrote a simple demo and customized layout and view. The Android project directory is as follows:
The code for creating a new mylayout. Java is as follows:
[Java]View plaincopyprint?
- Package com. Tutor. Touch;
- Import Android. content. context;
- Import Android. util. attributeset;
- Import Android. util. log;
- Import Android. View. motionevent;
- Import Android. widget. framelayout;
- Public class mylayout extends framelayout {
- Public mylayout (context ){
- Super (context );
- }
- Public mylayout (context, attributeset attrs ){
- Super (context, attrs );
- // Todo auto-generated constructor stub
- }
- @ Override
- Public Boolean onintercepttouchevent (motionevent eV ){
- Log. E (touchdemoactivity. Tag, "mylayout onintercepttouchevent .");
- Log. E (touchdemoactivity. Tag, "mylayout onintercepttouchevent default return"
- + Super. onintercepttouchevent (EV ));
- Return super. onintercepttouchevent (EV );
- }
- @ Override
- Public Boolean ontouchevent (motionevent event ){
- Log. E (touchdemoactivity. Tag, "mylayout ontouchevent .");
- Log. E (touchdemoactivity. Tag, "mylayout ontouchevent default return"
- + Super. ontouchevent (event ));
- Return super. ontouchevent (event );
- }
- }
Package COM. tutor. touch; </P> <p> Import android. content. context; <br/> Import android. util. attributeset; <br/> Import android. util. log; <br/> Import android. view. motionevent; <br/> Import android. widget. framelayout; </P> <p> public class mylayout extends framelayout {</P> <p> Public mylayout (context) {<br/> super (context ); <br/>}</P> <p> Public mylayout (context, attributeset attrs) {<br/> super (context, attrs ); <br/> // todo auto-generated constructor stub <br/>}</P> <p> @ override <br/> Public Boolean onintercepttouchevent (motionevent eV) {<br/> log. E (touchdemoactivity. tag, "mylayout onintercepttouchevent. "); <br/> log. E (touchdemoactivity. tag, "mylayout onintercepttouchevent default return" <br/> + super. onintercepttouchevent (EV); <br/> return Super. onintercepttouchevent (EV); <br/>}</P> <p> @ override <br/> Public Boolean ontouchevent (motionevent event) {<br/> log. E (touchdemoactivity. tag, "mylayout ontouchevent. "); <br/> log. E (touchdemoactivity. tag, "mylayout ontouchevent default return" <br/> + super. ontouchevent (event); <br/> return Super. ontouchevent (event); <br/>}< br/>
Create a new myview. Java code as follows:
[Java]View plaincopyprint?
- Package com. Tutor. Touch;
- Import Android. content. context;
- Import Android. util. attributeset;
- Import Android. util. log;
- Import Android. View. motionevent;
- Import Android. widget. Button;
- Public class myview extends button {
- Public myview (context ){
- Super (context );
- }
- Public myview (context, attributeset attrs ){
- Super (context, attrs );
- }
- @ Override
- Public Boolean ontouchevent (motionevent event ){
- Log. E (touchdemoactivity. Tag, "myview ontouchevent .");
- Log. E (touchdemoactivity. Tag, "myview ontouchevent default return"
- + Super. ontouchevent (event ));
- Return super. ontouchevent (event );
- }
- }
Package COM. tutor. touch; </P> <p> Import android. content. context; <br/> Import android. util. attributeset; <br/> Import android. util. log; <br/> Import android. view. motionevent; <br/> Import android. widget. button; </P> <p> public class myview extends button {</P> <p> Public myview (context) {<br/> super (context ); <br/>}</P> <p> Public myview (context, attributeset attrs) {<br/> super (context, attrs ); <br/>}</P> <p> @ override <br/> Public Boolean ontouchevent (motionevent event) {<br/> log. E (touchdemoactivity. tag, "myview ontouchevent. "); <br/> log. E (touchdemoactivity. tag, "myview ontouchevent default return" <br/> + super. ontouchevent (event); <br/> return Super. ontouchevent (event); <br/>}</P> <p >}< br/>
The touchdemoactivity code is as follows:
[Java]View plaincopyprint?
- Package com. Tutor. Touch;
- Import Android. App. activity;
- Import Android. OS. Bundle;
- Public class touchdemoactivity extends activity {
- Public static final string tag = "touchdemoactivity ";
- @ Override
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. Main );
- }
- }
Package COM. tutor. touch; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; </P> <p> public class touchdemoactivity extends activity {<br/> Public static final string tag = "touchdemoactivity "; <br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/>}< br/>}
The main. XML Code of all the above layout files is as follows:
[Java]View plaincopyprint?
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Com. Tutor. Touch. mylayout xmlns: Android = "http://schemas.android.com/apk/res/android"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent"
- >
- <Com. Tutor. Touch. myview
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content"
- Android: text = "@ string/Hello"/>
- </COM. Tutor. Touch. mylayout>
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <COM. tutor. touch. mylayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> </P> <p> <COM. tutor. touch. myview <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "@ string/Hello"/> </P> <p> </COM. tutor. touch. mylayout>
The following figure shows the effects of running the android project:
Click the red area to trigger the ontouch event in myview to view logcat, for example:
Click the green area to trigger the ontouch event in mylayout and view logcat, as shown in:
Both of the above use the system default value. It can be concluded that the default value of onintercepttouchevent is false, and the default value of ontouchevent in mylayout is false. Therefore, only action_down events are consumed, in myview, the default value returned by ontouch is true. It is called twice: action_dow and action_up.
Next we will change the return value of onintercepttouchevent in mylayout. Java to true. The Code is as follows:
[Java]View plaincopyprint?
- @ Override
- Public Boolean onintercepttouchevent (motionevent eV ){
- Log. E (touchdemoactivity. Tag, "mylayout onintercepttouchevent .");
- Log. E (touchdemoactivity. Tag, "mylayout onintercepttouchevent default return"
- + Super. onintercepttouchevent (EV ));
- Return true;
- }
@ Override <br/> Public Boolean onintercepttouchevent (motionevent eV) {<br/> log. E (touchdemoactivity. tag, "mylayout onintercepttouchevent. "); <br/> log. E (touchdemoactivity. tag, "mylayout onintercepttouchevent default return" <br/> + super. onintercepttouchevent (EV); <br/> return true; <br/>}
Run the project, continue to click the red area, view logcat, and find that the ontouch event of myview is not called, that is, it is intercepted, as shown in:
Let's continue the experiment and set the returned value of onintercepttouchevent to false. Change the returned value of ontouchevent in myview to false, that is, the ontouchevent in myview is modified as follows:
[Java]View plaincopyprint?
- @ Override
- Public Boolean ontouchevent (motionevent event ){
- Log. E (touchdemoactivity. Tag, "myview ontouchevent .");
- Log. E (touchdemoactivity. Tag, "myview ontouchevent default return"
- + Super. ontouchevent (event ));
- Return false;
- }
@ Override <br/> Public Boolean ontouchevent (motionevent event) {<br/> log. E (touchdemoactivity. tag, "myview ontouchevent. "); <br/> log. E (touchdemoactivity. tag, "myview ontouchevent default return" <br/> + super. ontouchevent (event); <br/> return false; <br/>}
Run the project and click the red area to view logcat, for example:
We can see that the ontouchevent in myview only consumes one click event (action_down), does not execute action_up, and then runs to mylayout to execute the ontouchevent event.
Therefore, the summary is as follows:
The default value of onintercepttouchevent in viewgroup is false to pass the event to ontouchevent in view.
The default ontouchevent value in viewgroup is false.
The default value of ontouchevent returned in the view is true. In this way, multiple touch events can be executed.
Okay. The above content is shared today. Thank you! What do you not understand? Please leave a message !!! In addition, the title of the article is just an empty title for two minutes. If you have understood it for more than two minutes, don't spray it on me. Hahaha !!!