Android callback-based event processing

Source: Internet
Author: User

Android callback-based event processing

The callback-based event processing model is simpler:

If the event listening mechanism is a delegated event processing mechanism, the callback mechanism is exactly the opposite: For the callback-based time processing model, the event sources and Event Listeners are unified, or the event listener disappears completely. When a user fires an event on a GUI component, the component's own method is responsible for handling the event.

Therefore, the callback listening mechanism only needs to inherit the component and rewrite the event processing method of the component class to implement

For Event Callback, Android provides some event processing callback methods for all GUI components. Take View as an example:

Boolean onKeyDown (int keyCode, KeyEvent event); this method is triggered when you press a key on this component.

Boolean onKeyShortcut (int keyCode, KeyEvent event); this method is triggered when a keyboard shortcut event occurs.

Boolean onKeyLongPress (int keyCode, KeyEvent event). This method is triggered when you press a key on the component.

Boolean onTouchEvent (int keyCode, KeyEvent event); this method is triggered when a user triggers a touch screen event on this component.

... And so on

MainActivity. java

 

public class MainActivity extends Activity{@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);}}
Main. xml
MyButton. java
Public class MyButton extends Button {public MyButton (Context context, AttributeSet set) {super (context, set) ;}@ Overridepublic boolean onKeyDown (int keyCode, KeyEvent event) {super. onKeyDown (keyCode, event); Log. v ("-crazyit.org-", "the onKeyDown in MyButton"); // returns true, indicating that the event will not spread outward and return true ;}}

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.