Android Event Delivery (delivery of a view event)

Source: Internet
Author: User

First of all, let's talk about the mechanism of the transfer of the view event, first we customize a class myview inherit from view and then replicate the parent class in the MyView two methods Dispatchtouchevent (Motionevent event) and ontouchevent (Motionevent Event) method and then in the experiment, let me first say that the result of my experiment is that if it inherits from the view class, the main entrance of the event is dispatchtouchevent (Motionevent event), an event that determines whether an event is passed through the return result in the Dispatchtouchevent (Motionevent event) method. Returns true if the event is consumed and returns false, which means that no interception is performed, and the system defaults to return FALSE.

View the source code of the Dispatch method system

if (onfiltertoucheventforsecurity (event)) {            //noinspection simplifiableifstatement            listenerinfo li = Mlistenerinfo;            if (Li! = null && Li.montouchlistener! = null                    && (mviewflags & enabled_mask) = = ENABLED                    && Amp Li.mOnTouchListener.onTouch (this, event)) {                result = true;            }            if (!result && ontouchevent (event)) {                result = true;            }        }

can find out that he has if (li! = null && Li.montouchlistener! = null&& (mviewflags & enabled_mask) = = ENABLED && Li.mOnTouchListener.onTouch (this, event)) in this sentence only Montouchlistener.ontouch (this, Event) returns false by default so the default is that events are not intercepted and only four variables are true when they return True .

The last time you look at it, you find that Montouchlistener.ontouch (this, event) calls a PerformClick class. An onclick () in the class, an event that responds to a button's click

Here is the code for my own test

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent " >    <com.chaoli_chen.ontouchdemo.mybutton         android:onclick= "Bt_click"        android:layout_height= " Wrap_content "        android:layout_width=" wrap_content "        android:text=" experiment button "        /></relativelayout>

Customizing a button to replicate one of the Dispatchtouchevent (Motionevent event) and Ontouchevent (Motionevent event)

Package Com.chaoli_chen.ontouchdemo;import Android.content.context;import Android.util.attributeset;import Android.view.motionevent;import Android.widget.button;public class Mybutton extends Button {public Mybutton (Context Context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr);} Public Mybutton (context context, AttributeSet Attrs) {Super (context, attrs);} Public Mybutton (Context context) {super (context);} @Overridepublic boolean dispatchtouchevent (Motionevent event) {switch (event.getaction ()) {case Motionevent.action_ DOWN:System.out.println (". Button.dispatchTouchEvent.ACTION_DOWN ..."); Break;case Motionevent.action_move: System.out.println (". Button.dispatchTouchEvent.ACTION_MOVE ..."); Break;case motionevent.action_up: System.out.println (". Button.dispatchTouchEvent.ACTION_UP ..."); break;} Return Super.dispatchtouchevent (event);} @Overridepublic boolean ontouchevent (Motionevent event) {switch (event.getaction ()) {case Motionevent.action_down: System.out.println (". ButtoN.ontouchevent.action_down "); Break;case MotionEvent.ACTION_MOVE:System.out.println (". Button.onTouchEvent.ACTION_MOVE "); Break;case MotionEvent.ACTION_UP:System.out.println (". Button.onTouchEvent.ACTION_UP ... "); break;} Return Super.ontouchevent (event);}}

The same replication in Mainactivity two methods Dispatchtouchevent (Motionevent event) andontouchevent (Motionevent event) also set a Click event for button buttons to monitor Setontouchlistener (i)

Package Com.chaoli_chen.ontouchdemo;import Android.app.activity;import Android.os.bundle;import Android.view.motionevent;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.view.ontouchlistener;import Android.widget.button;public class Mainactivity extends Activity {private Button bt, @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); bt = (Button) Findviewbyid (R.ID.BT); Bt.setontouchlistener (new Ontouchlistener () {@Overridepublic Boolean onTouch (View V, motionevent event) {switch (event.getaction ()) {case MotionEvent.ACTION_DOWN:System.out.println (". Bt.. Touchevent.action_down ") Break;case MotionEvent.ACTION_MOVE:System.out.println (". Bt. Touchevent.action_move ") Break;case MotionEvent.ACTION_UP:System.out.println (". Bt. Touchevent.action_up ... "); break;} return false;}}); public void Bt_click (view view) {System.out.println ("... bt_click ..."); @Overridepublic Boolean disPatchtouchevent (motionevent event) {switch (event.getaction ()) {Case MotionEvent.ACTION_DOWN:System.out.println (". Main.dispatchTouchEvent.ACTION_DOWN "); Break;case MotionEvent.ACTION_MOVE:System.out.println (". Main.dispatchTouchEvent.ACTION_MOVE "); Break;case MotionEvent.ACTION_UP:System.out.println (". Main.dispatchTouchEvent.ACTION_UP ... "); break;} Return Super.dispatchtouchevent (event);} @Overridepublic boolean ontouchevent (Motionevent event) {switch (event.getaction ()) {case Motionevent.action_down: System.out.println (". Main.onTouchEvent.ACTION_DOWN ..."); Break;case MotionEvent.ACTION_MOVE:System.out.println (" . Main.onTouchEvent.ACTION_MOVE "); Break;case MotionEvent.ACTION_UP:System.out.println (". Main.onTouchEvent.ACTION_UP ... "); break;} Return Super.ontouchevent (event);}}


The printed results are as follows


Production look log I can buy know the main entrance of the program is the activity of the dispatchtouchevent method, and then pass to our button buttons dispatchTouchEvent method The Ontouch () method passed to our button and then the Ontouchevent method. And finally the button of my door. The corresponding Click event method.

By analyzing the above structure we can intercept the click event of the custom button and not let it execute the onclick method, just return True in Ontouch ().

Bt.setontouchlistener (New Ontouchlistener () {@Overridepublic Boolean onTouch (View V, motionevent event) {switch ( Event.getaction ()) {Case MotionEvent.ACTION_DOWN:System.out.println (". Bt.. Touchevent.action_down ") Break;case MotionEvent.ACTION_MOVE:System.out.println (". Bt. Touchevent.action_move ") Break;case MotionEvent.ACTION_UP:System.out.println (". Bt. Touchevent.action_up ... "); break;} return true;}});


The test results are as follows


By analyzing the results we are the OnClick event that can intercept the click of a button.

Android Event Delivery (delivery of a view event)

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.