When we develop more apps, We need to interact with users-that is, to respond to user operations.
In this section, we will first introduce the listener-based event processing mechanism.
First, let's take a look at the model of the listener processing mechanism.
① Directly use anonymous internal classes as Event ListenersPs: 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 ListenersThis 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 ListenersIs 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 listenerYou 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.