Day 4, androidday
Day 4-part 1
Android event processing mechanism-listener-based event processing mechanism
This section introduces:
Before starting this chapter, let's review the background of android and some common UI components,
The six major la s, now we can make a simple app interface, and the next step is the implementation of logic and business.
In this example, we have seen setXXXListener, Which is android event processing, and this method is based on the listener.
Android provides two powerful event processing mechanisms:
① Listener-based event processing mechanism
② Callback-based event processing mechanism
We will explain the Android event processing mechanism in several parts, and in this part we will parse it in detail
Listener-based event processing mechanism!
This section describes the road map:
Body:
Listener-based time processing mechanism model:
Process Model diagram:
Text representation:
In the event listening mechanismEvent source, event, event listenerThree types of objects
Process:
Step 1:Sets a listener for an event source (Component) to listen on user operations.
Step 2:User operations trigger the event source listener
Step 3:The corresponding event object is generated.
Step 4:Send the event source object as a parameter to the event listener.
Step 5:The event listener judges the event object and executes the corresponding event processor (corresponding event processing method)
Summary:
Event listening is a delegated event processing mechanism. Event source (Component) event processing is delegated to the event listener.
When the event SOURCE sends a specified event, it notifies the specified event listener and performs the corresponding operation.
The usage of the listener mechanism:
Here is a simple program that prompts the Toast information by clicking a button.
However, different forms are used for implementation:
:
Different implementations:
① Directly use an anonymous internal class
One of the most common features:Directly setXxxListener and rewrite the MethodYou can;
It is usually used temporarily, and the reusability is not high!
Code:
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 () @ Override public void onClick (View v) {// display Toast information Toast. makeText (getApplicationContext (), "you clicked", Toast. LENGTH_SHORT ). show ();}});}}
② Use internal classes
Different from the anonymous internal class above!
Advantages: You can reuse this class and directly access all interface components of the external class!
Code:
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); // 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 {@ Override public void onClick (View v) {Toast. makeText (getApplicationContext (), "the button is clicked", Toast. LENGTH_SHORT ). show ();}}}
③ Use external classes
Create another Java file to process events. This method is rarely used!
Because the external class cannot directly access the components in the user interface class, the components should be passed in through the constructor;
The result is that the Code is not concise enough!
Ps: to demonstrate parameter passing, use TextView instead of Toast!
Code:
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;} @ Override public 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
OnlyLet the Activity class implement the XxxListener event listening InterfaceDefine the correspondingEvent processor Method
Eg: Actitity implements the OnClickListener interface and overwrites the onClick (view) method.
SetXxx. Listener (this) is used to add the event listening object for some components.
Or the layout effect at the beginning;
Code:
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 @ Override public void onClick (View v) {Toast. makeText (getApplicationContext (), "click the button", Toast. LENGTH_SHORT ). show ();}}
⑤ Bind directly to the tag:
Is directly inThe corresponding Activity in the xml layout FileDefineEvent handling method
Eg: public void myClick (View source) source corresponds to the event source (Component)
In the layout file, set an attribute for the event to be triggered.: Onclick= "Myclick"
Which of the following is an example with the toast prompt:
Code:
MainActivity. java
Package com. jay. example. caller; import android. app. activity; import android. OS. bundle; import android. view. view; import android. widget. toast; public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} // customize a method and pass in a view component as the parameter public void myclick (View source) {Toast. makeText (getApplicationContext (), "the button is clicked", Toast. LENGTH_SHORT ). show ();}}
Main. xml layout file:
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: id = "@ + id/LinearLayout1" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Button" android: onClick = "myclick"/> </LinearLayout>
What are the best Android entry books? (42)
Crazy Android handout (version 2) is explained in version 4.2
Error Message