Android event processing mechanism (1) ----- listener-based event processing mechanism

Source: Internet
Author: User

Listener-based event processing mechanism


Preface:


When we develop more apps, We need to interact with users-that is, to respond to user operations.

This involves the android event processing mechanism;

Android provides two powerful processing mechanisms:

① Listener-based event processing mechanism

② Callback-based event processing mechanism

In this section, we will first introduce the listener-based event processing mechanism.

Okay, you don't have to talk about it!



First, let's take a look at the model of the listener processing mechanism.


Listening processing model: Processing Model diagram:




Text representation:

In the event listening mechanismEvent source, event, event listener <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Release/z1 + mzyTwvcD4KPHA + release + 1LQo1 + m8/release + vavV4rj2ysK8/release + release/release + PGJyPgo8L3A + release + example/example/rSmwO27 + example/example + tbHKwrz + example + vODM/example + c1_vcd4kpha + PGJyPgo8L3A + example + c1_vcd4kpha + example temperature + 4tKfEprT6wus8L3A + temperature = "http://www.2cto.com/uploadfile/Collfiles/20140322/2014032208272179.jpg" alt = "\">



① Directly use anonymous internal classes as Event Listeners

Ps: The most common method we usually use. After setXxxListener is used, the corresponding method in it will be rewritten.

It is usually used on a temporary basis, and the reusability is not high.



Code:

Xml is a common button.

MainActivity. java


Package com. jay. example. innerlisten; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. toast; import android. app. activity; public class MainActivity extends Activity {private Button btnshow; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); btnshow = (Button) findViewById (R. id. btnshow); btnshow. setOnClickListener (new OnClickListener () {// rewrite the onClick event processing method onClick () @ Overridepublic void onClick (View v) {// display Toast information Toast. makeText (getApplicationContext (), "you clicked", Toast. LENGTH_SHORT ). show ();}});}}


② Use internal classes as Event Listeners

This is different from the anonymous internal class above.

Advantages: You can reuse this class and directly access all interface components of the external class.


Code:

Package com. jay. example. innerlisten; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. toast; import android. app. activity; public class MainActivity extends Activity {private Button btnshow; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); btnshow = (Button) findViewById (R. id. btnshow); // directly new an internal class object as the parameter btnshow. setOnClickListener (new BtnClickListener ();} // defines an internal class to implement View. onClickListener interface, and override the onClick () method class BtnClickListener implements View. onClickListener {@ Overridepublic void onClick (View v) {Toast. makeText (getApplicationContext (), "the button is clicked", Toast. LENGTH_SHORT ). show ();}}}



③ Use external classes as Event Listeners

Is to create another Java file to process the event, this form is rare

Because external classes cannot directly access the components in the user interface class, the components must be passed in using the constructor,

The result is that the Code is not concise enough.


Ps: In order to demonstrate it, use textView instead of Toast display!


:



Code:

The layout is relatively simple, that is, a button + text box, which is not provided here

Write an external class:

MyClick. java


Package com. jay. example. innerlisten; import android. view. view; import android. view. view. onClickListener; import android. widget. textView; public class MyClick implements OnClickListener {private TextView textshow; // pass the text box as a parameter to public MyClick (TextView txt) {textshow = txt;} @ Overridepublic void onClick (View v) {// set textshow in the text box after clicking. setText ("Click! ");}}


MainActivity. java


Package com. jay. example. innerlisten; import android. OS. bundle; import android. widget. button; import android. widget. textView; import android. app. activity; public class MainActivity extends Activity {private Button btnshow; private TextView txtshow; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); btnshow = (Button) findViewById (R. id. btnshow); txtshow = (TextView) findViewById (R. id. textshow); // new an external class, and pass TextView as a parameter to btnshow. setOnClickListener (new MyClick (txtshow ));}}



④ Directly use Activity as the event listener

You only need to implement the Activity class ~ Listener event listening interface, which defines to override the corresponding event processor method in Activity

Eg: Activity implements the OnclickListener interface and overwrites the onClick (view) method.

When you add an event listener object to a component, you can directly setXxxListener (this ).


:



Layout file omitted


MainActivity. java

Package com. jay. example. innerlisten; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. toast; import android. app. activity; // Let the Activity Method Implement the OnClickListener interface public class MainActivity extends Activity implements OnClickListener {private Button btnshow; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); btnshow = (Button) findViewById (R. id. btnshow); // write this btnshow directly. setOnClickListener (this);} // rewrite the abstract method in the interface @ Overridepublic void onClick (View v) {Toast. makeText (getApplicationContext (), "click the button", Toast. LENGTH_SHORT ). show ();}}



Important OnTouchEvent event Parsing


The OnTouchEvent is not just an example of the event listening mechanism.

In addition, the calling process is special, and this event is used in actual development.


All view components have rewritten this method, and applications can use this method to process touch events on the mobile phone screen.


Method declaration:

Public boolean onTouchEvent (MotionEvent event );


Procedure:

① For a view (Component)SetOnTouchListener (new OnTouchListener ())

② Rewrite the onTouch () method

③ There are three commonly used touch statuses:

MotionEvent. ACTION_DOWN: Press

MotionEvent. ACTION_MOVE: Move

MotionEvent. ACTION_UP: Release

Therefore, we usually perform classification by switch,Switch (event. getAction)And then each case ~ : Process corresponding events



Method call sequence parsing:

The method declaration knows that onTouchEvent returns a boolean value, either true or false.

The two differences are: whether to call an external method after calling the method in onTouch ()


Here is a simple example:

We have set onTouch (), onClick (), and onLongClick () methods for a button. What is their call order?


A: There are two scenarios:

① Return true: Call the ACTION_DOWN method when you press it. After the call, call the ACTION_MOVE method. As long as your fingers keep pressing the system, the system will continuously respond to this

The reason is that (android is sensitive to touch events, even though our fingers are still, but our fingers are shaking); when our fingers leave

The screen is finished. Call the MotionEvent. ACTION_UP method at this time.


② False is returned: it is the same as the above process, but after leaving the screen, the other two methods will be called:

If you press short, The onClick () method is called.

If you press long, the onLongClick () method is called;

Then the game is finished.



The above two situations are not difficult to understand

The reader can write the code to verify the call, and then view the call sequence information through log. I ().


Finally, let's demonstrate it to you.

Call the OnTouchEvent () onMove method.


The MoveTo () method uses an instance:



This is a lot of use, and the example is still old. It is given in the frame layout section.

Here is a brief introduction to usage:

We only need to use the parameter event to get the current Click location.

X = event. getX (); // obtain the X coordinate of the click.

Y = event. getY (); // obtain the Y coordinate of the click.


For details, see the code:

Use an instance using the MoveTo Method


Original article:

Click Open Link

That's the cute girl who moves with her fingers!




Summary:

Okay. Here is the listener-based event processing mechanism.

If there are any omissions, errors, or other comments

Thank you for choosing O (∩ _ ∩) O.


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.