Android (callback-based) Event Processing

Source: Internet
Author: User

Android (callback-based) Event Processing
Callback-based event model: the event source and event listening are unified, and the event is handled by the event source. Callback-based event processing is suitable for views with fixed event processing logic. Android first triggers the event listener bound to the control. To use the callback mechanism to process events on GUI components, You need to customize the component class to inherit the GUI component class and override the event processing method of this class. Use custom component classes in XML files. Android provides some event processing callback methods for all GUI components, such as View class: 1. boolean onKeyDown (int keyCode, keyEvent event): This method is triggered when you press a key on the component. // Key codes and events are applicable to keyboards. 2. boolean onKeyLongPress (int keyCode, keyEvent event): This method is triggered when you press a key on the component. 3. boolean onKeyShortcut (int keyCode, KeyEvent event): This method is triggered when a keyboard shortcut event occurs. 4. boolean onKeyUp (int keyCode, KeyEvent event): This method is triggered when you release a key on this component. 5. boolean onTouchEvent (MotionEvent event): This method is triggered when a user triggers a touch screen event on the component. // Applicable to touch screens 6. boolean onTrackballEvent (MotionEvent event): This method is triggered when you trigger a trackball event on this component. When the callback method returns true, it indicates that the method has completely handled the event and the event will not be propagated. When the callback method returns false, it indicates that the method does not fully process the event and the event will be propagated. For callback-based event propagation, what happens on a component will not only stimulate the callback method on the component, it will also trigger the callback method of the Activity where the component is located-as long as the time can be propagated to the Activity. Example: activity_main

<LinearLayout 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" android: orientation = "vertical"> <com. example. huidiao. myButton android: id = "@ + id/bn" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "touch screen on the button"/>

 

</LinearLayout> MyButton. java
Package com. example. huidiao; public class MyButton extends Button {// AttributeSet receives the attribute information defined in xml, which is not necessarily a custom layout, otherwise, the property information defined in xml cannot be received. Public MyButton (Context context, AttributeSet set) {super (context, set);} // rewrite the callback method of the touch screen event public boolean onTouchEvent (MotionEvent event) {// event super. onTouchEvent (event); Log. v ("touch-screen events on the button", "Touch-screen events on the button are handled by the event source"); return false; // ② }}

 

MainActivity. java
Package com. example. huidiao; public class MainActivity extends Activity {Button bn; protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); bn = (Button) findViewById (R. id. bn); // bind event listener bn to bn. setOnTouchListener (new OnTouchListener () {public boolean onTouch (View v, MotionEvent event) {// TODO Auto-generated method stub Log. v ("touch-screen events on the listener button", "Touch-screen events on the listener processing button"); return false; // ①}); // rewrite the onTouchEvent method, this method can be used to monitor all the controls it contains. The touch-screen event is public boolean onTouchEvent (MotionEvent event) {// event super. onTouchEvent (event); Log. v ("transfer a touch-screen event to Activity", "Touch-screen event on a Custom button, processing an event in the Activity where the button is located"); return false; // ③ }}

 

 

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.