Android Event delivery mechanism

Source: Internet
Author: User

The basis of the event: press, move, lift.

The first thing to do is to press, and the second may be to move, and finally perform the lift.

Android Event delivery mechanism:

Android Event delivery is delivered hierarchically, with event handling from activity to ViewGroup to view.

View and ViewGroup response methods to events (that is, delivery methods):


Dispatchtouchevent (): Distribution of the event. Return true to not continue distribution.

return FALSE to continue distribution at the next level

Ontouchevent (): Handling of the event. Return True indicates consumption handles the current event,

Return false is not processed and is handed to child controls for continued distribution

Onintercepttouchevent (): Interception of events (note: ViewGroup only)
Return true intercepts the current event and does not continue to distribute it to its own ontouchevent for processing.

return false will not intercept, continue to pass.

1.onTouch event to be executed before the OnClick event
2.onTouch is called in the event distribution method Dispatchtouchevent,
3. The onclick is called in the event-handling method Ontouchevent
4.onTouchEvent () After the call to the Dispatchtouchevent method

Small case: Double-click to exit

/*
* Double Exit Principle:
* Judging by a variable,
* handler.postdelayed (): How long after the delay to restore the original state of the variable
*/
Boolean is2callback=false;
@Override
public boolean onKeyDown (int keycode, keyevent event) {

if (keycode==keyevent.keycode_back) {
if (!is2callback) {
Is2callback=true;
Toast.maketext (Mainactivity.this, "Click again to exit", Toast.length_short). Show ();
New Handler (). postdelayed (New Runnable () {
public void Run () {
Is2callback=false;
}
}, 3*1000);
}
else{
Android.os.Process.killProcess (Android.os.Process.myPid ());
}
}
return true;
}

Android Event delivery 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.