Android basic getting started tutorial-3.2 callback-based event processing mechanism, android getting started tutorial

Source: Internet
Author: User

Android basic getting started tutorial-3.2 callback-based event processing mechanism, android getting started tutorial
Android basic tutorial-3.2 callback-based event processing

Tags (separated by spaces): basic Android tutorial

This section introduces

In Android 3.1, we learned an event processing mechanism in Android-listener-based event processing mechanism.
Add a listener for the event source (component), and send it to the listener for processing after the event is triggered.
Perform different operations. What is the principle of callback-based event processing? Well, there is another question: you know
What isMethod callback? Do you know? I believe many of my friends are familiar with it, but I cannot tell it! Okay, with these questions
Parse the callback event processing mechanism in the android event processing mechanism!

1. What is method callback?

Text representation:

A: It is a means to separate function definitions from functions. It is a design concept of decoupling. in Java, callback is implemented through interfaces,
As a system architecture, you must have your own runtime environment and provide the Implementation interface for the user. The implementation depends on the customer, so that you can
To achieve unified interfaces and different implementations, the system calls back our implementation classes in different States to achieve separation of interfaces and implementations!

For example:

For example:When you go home from school on Friday, you ask your mom if she has prepared a good meal. Then you tell her:
Mom, I have a look at Pleasant goat. If you cook well, please call me!
Analysis:You have an interface with your mom. You can use this interface to ask your mom to cook. When the meal is ready, your mom
You can use this interface to get feedback. "The meal has been cooked "!

2. Android callback event handling mechanism:

In Android, callback-based event processing has two scenarios:

1) custom view

When a user fires an event on a GUI component, the component has its own method to handle the event.
Common usage: inherits the basic GUI Component and overwrites the event processing method of the component, that is, the custom view.
Note: When using a custom view in the xml layout, you must use the "fully qualified class name"

Common View component callback methods:

Android provides some event processing callback methods for GUI components. Taking View as an example, the following methods are provided:

① Trigger a screen event on this component: booleanOnTouchEvent(MotionEvent event );
② When a button is pressed on the component: booleanOnKeyDown(Int keyCode, KeyEvent event );
③ When a button on the component is released: booleanOnKeyUp(Int keyCode, KeyEvent event );
④ When a widget button is long: booleanOnKeyLongPress(Int keyCode, KeyEvent event );
⑤ Occurrence of keyboard shortcut events: booleanOnKeyShortcut(Int keyCode, KeyEvent event );
6. Trigger the trackball event on the component: booleanOnTrackballEvent(MotionEvent event );
* 7 when the focus of the component changes, this method can only be rewritten in View, unlike the previous six!
Protected voidOnFocusChanged(Boolean gainFocus, int direction, Rect previusly FocusedRect)

In addition, this explains what a trackball is, but it is of little use. It can be seen on a blackberry phone. When we browse the Web page
You can think of trackball as a mouse. However, we can use onTouchEvent to solve this problem, and it is not beautiful enough.
It is easy to use. You can click f6 on the original Android simulator if you are interested!

Sample Code:
We customize a MyButton class to inherit the Button class, and then override the onKeyLongPress method;
Then, in the xml file, use the fully qualified class name to call the custom view.

As follows:

Click a simple button to trigger the onTouchEvent event. When we press the keyboard on the simulator,
Press onKeyDown to trigger the onKeyUp event when you exit the keyboard! We can view it through Logcat!

Implementation Code:
MyButton. java

Public class MyButton extends Button {private static String TAG = ""; public MyButton (Context context, AttributeSet attrs) {super (context, attrs );} // rewrite the event triggered by pressing the keyboard @ Override public boolean onKeyDown (int keyCode, KeyEvent event) {super. onKeyDown (keyCode, event); Log. I (TAG, "onKeyDown method called"); return true;} // rewrite the keyboard-triggered event @ Override public boolean onKeyUp (int keyCode, KeyEvent event) {super. onKeyUp (keyCode, event); Log. I (TAG, "onKeyUp method called"); return true;} // The component is touched @ Override public boolean onTouchEvent (MotionEvent event) {super. onTouchEvent (event); Log. I (TAG, "onTouchEvent method called"); return true ;}}

Layout file:

<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" tools: context = ". myActivity "> <example.jay.com. mybutton. myButton android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "button"/> </RelativeLayout>

Code parsing:

Because we directly overwrite the three callback methods of the Button, when a click event occurs, we do not need to perform this in the Java file.
The event listener can be bound to complete the callback, that is, the component will process the corresponding event, that is, the event is handled by the event source (Component) itself!

2) callback-based event propagation:

In summary, whether or not to spread out depends on whether the return value of the method is true or false;

Sample Code:

MyButton. java:

Public class MyButton extends Button {private static String TAG = ""; public MyButton (Context context, AttributeSet attrs) {super (context, attrs );} // rewrite the event triggered by pressing the keyboard @ Override public boolean onKeyDown (int keyCode, KeyEvent event) {super. onKeyDown (keyCode, event); Log. I (TAG, "The onKeyDown method of the Custom button is called"); return false ;}}

Main. xml:

<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" tools: context = ". myActivity "> <example.jay.com. mybutton. myButton android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Custom button" android: id = "@ + id/btn_my"/> </LinearLayout>

MainActivity. java:

Public class MyActivity extends ActionBarActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_my); Button btn = (Button) findViewById (R. id. btn_my); btn. setOnKeyListener (new View. onKeyListener () {@ Override public boolean onKey (View v, int keyCode, KeyEvent event) {if (event. getAction () = KeyEvent. ACTION_DOWN) {Log. I ("Haha", "listener onKeyDown method called") ;}return false ;}}) ;}@ Override public boolean onKeyDown (int keyCode, KeyEvent event) {super. onKeyDown (keyCode, event); Log. I ("Haha", "The onKeyDown method of Activity is called"); return false ;}}

Run:

Result Analysis:
From the above running results, we can know that the propagation order is:
Listener->Callback method of the view component->Activity callback Method;

Summary

This section describes the callback-based event processing mechanism in the Android event processing mechanism! The core is the sequence of event propagation.
The listener takes precedence over the View component and then the Activity. If the returned value is false, the propagation continues. If the value is true, the propagation ends ~!

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.