Onintercepttouchevent and ontouchevent in Android

Source: Internet
Author: User

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:

Create a new mylayout. Java Code As follows: Package Com. Tutor. Touch;

ImportAndroid. content. context;
ImportAndroid. util. attributeset;
ImportAndroid. util. log;
ImportAndroid. View. motionevent;
ImportAndroid. widget. framelayout;

Public ClassMylayoutExtendsFramelayout {


PublicMylayout (context ){
Super(Context );
}

PublicMylayout (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 );
}
} Then create a new myview. Java code as follows: Package Com. Tutor. Touch;

ImportAndroid. content. context;
ImportAndroid. util. attributeset;
ImportAndroid. util. log;
ImportAndroid. View. motionevent;
ImportAndroid. widget. Button;

Public ClassMyviewExtendsButton {

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 );
}

}

The touchdemoactivity code is as follows: Package Com. Tutor. Touch;

ImportAndroid. App. activity;
ImportAndroid. 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 );
}
} The main. XML Code of all the above layout files is as follows: <? 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>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:

@ 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 ;
}

 

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: @ 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 ;
} 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.

URL: http://greatverve.cnblogs.com/archive/2012/01/12/onInterceptTouchEvent-onTouchEvent.html

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.