Android Learning event distribution mechanism

Source: Internet
Author: User

Blog Source: http://blog.csdn.net/sinyu890807/article/details/9097463

There is currently a very simple project with only one activity, and there is only one button in the activity. You may already know that if you want to register a click event with this button, you only need to call:

    Button.setonclicklistener (new  Onclicklistener () {          @Override          publicvoid  onclick (View v) {              log.d ("TAG""onclick Execute");          }      });  

  Button.setontouchlistener (new   Ontouchlistener () {@Override  public  boolean OnTouch (View  V, motionevent event    " tag  " ,  ontouch Execute, Action  "  + event  .getaction ());  return  false  ;  }      }); 

The Ontouch method can do more things than the onclick, such as judging the finger presses, lifts, moves and other events. So if I have two events registered, which one will be executed first? Let's try it out, run the program click on the button and print the results as follows:

As you can see, Ontouch takes precedence over the onclick execution, and Ontouch executes two times, one is Action_down, one is action_up (and there may be several action_move executions, if you shake your hand a bit). So the order in which the events are passed is Ontouch first and then passed to the onclick.

The Ontouch method has a return value, and here we return false, if we try to change the return value in the Ontouch method to the True,onclick method, it is no longer executed. We can regard this as a touch event that was intercepted by the OnTouch method and did not continue to be passed to the onclick.

So how does the click event of a View pass? :

The source code of Dispatchtouchevent is as follows:
     Public Event ) {          ifnull && (mviewflags & enabled_mask) = = ENABLED &&                  Montouchlistener.ontouch (Thisevent)) {              returnTrue ;          }           return ontouchevent (event);      }  

Android Learning event distribution mechanism

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.