I. Introduction
After a few days of learning, become familiar with a few basic layout and some great use common controls, currently studying how to implement a basic login register interface and its features, and functionality we need to achieve our event-handling mechanism to call the event handler method. The following is my summary of some event handling mechanism.
Second, the common listener interfaces
- View.OnClickListener click event listener must implement
- View.OnCreateContextMenuListener create a context menu event
- View.OnFocusChangeListener focus change event
- View.OnKeyListener key event listeners
- View.OnLongClickListener press event listeners
- View.OnTouchListener touch screen event listeners
Third, based on event handling mechanism listening
(A) First of all, the event listener mechanism consists event source, event, event listeners three types of objects.
Event listener process flow :
- Setting a listener: (button, for example), for monitoring a user's operation (tap operation, etc.) to an event source
- Operation made by the user triggers an event listener source
- Automatically generate the corresponding event object
- The event source object as a parameter to event listeners
- Event listener objects event determination, performs the corresponding event handler (processing method)
(B) five different ways:
Click the button to add to the incident as an example of information generated Toast
1. anonymous inner classes
This method is the most commonly used method, but not high reusability
. 1 the Button BT1 = (the Button) the findViewById (R.id.bt1); 2 . 3 bt1.setOnclickListener ( new new View.OnClickListener () { . 4 . 5 // override click event processing method onClick () . 6 . 7 @Override . 8 . 9 public void the onClick (View V) { 10 . 11 // display Toast information 12 is 13 is Toast.makeText (getApplicationContext (), "click", Toast.LENGTH_SHORT) the .Show (); 14 15 } 16 . 17 }
2. Use inner classes
The above anonymous inner classes and different, can be an advantage in the class multiplexing, interface components can be direct access to all of the outer class.
. 1 public class the MainActivity the extends the Activity { 2 . 3 Private the Button BT1; . 4 . 5 @Override . 6 . 7 protected void the onCreate (the Bundle savedInstanceState) { . 8 . 9 super.onCreate (saveInstanceState); 10 . 11 setContentView (R.layout.activity); 12 is 13 is Bt1 = (the Button) the findViewById (R.id.bt1); 14 15 // directly inside a new class object as an argument 16 . 17 Bt1.setOnClickListener ( new BtnClickListener ()); 18 is . 19 } 20 is 21 is // define an internal class that implements the interface View.OnClickListener, and rewrite the onClick () Method 22 is 23 is Class BtnClickListener the implements View.OnClickListener 24 25 { 26 is 27 @Override 28 29 Public void the onClick (View V) { 30 31 is Toast.makeText (getApplicationContext (), "click", Toast.LENGTH_SHORT) the .Show (); 32 33 is } 34 is 35 } 36 37 [ }
3. An external class
Also create a Java file processing time, this form of use is relatively small, because the external class can not directly access the components of the user interface class, through constructor will use the incoming assembly, which Aung result is that code is simple enough.
Create an external class:
. 1 public class MyClick the implements View.OnClickListener { 2 Private the TextView the textView; . 3 // the text box as a parameter . 4 public MyClick (the TextView V) { . 5 the textView = V; . 6 } . 7 @Override . 8 public void the onClick (View V) { 9 // reset the text box clicking the button text 10 textView.setText ( "clicked button" ); 11 } 12 }
MainActivity.java:
1 public class MainActivity extends Activity{ 2 private Button btn1; 3 private TextView tv1; 4 @Override 5 protected void onCreate(Bundle savadInstanceState){ 6 super.onCreate(savadInstanceState); 7 setContentView(R.layout.activty_main); 8 btn1 = (Button) findViewById(R.id.btn1); 9 tv1 = (TextView) findViewById(R.id.tv1); 10 //new一个外部类,并把TEXTVIEW作为参数传入 11 btn1.setOnClickListener(new Myclick(tv1)); 12 } 13 }
4. Direct use as an event listener Activity
Let Actiity XxxListener event listener class that implements an interface, processor time to rewrite the definition of the corresponding method in an Activity.
eg: Activity OnClickListener class implements an interface, rewritten OnClick (View v) while monitoring methods for certain components of the object add events directly setXxx.Listener (this) to
1 import android.app.Activity; 2 import android.os.Bundle; 3 import android.view.View; 4 import android.widget.Button; 5 import android.widget.Toast; 6 7 public class MainActivity extends Activity implements View.OnClickListener{ 8 private Button btn; 9 @Override 10 protected void onCreate(Bundle savedInstanceState){ 11 super.onCreate(savedInstanceState); 12 the setContentView (R.layou.activity_main); 13 is 14 BTN = (the Button) the findViewById (R.id.btn); 15 btn.setOnClickListener ( the this ); 16 . 17 } 18 is . 19 // abstract method of rewriting the interface 20 is @Override 21 is public void onClck (View V) { 22 is Toast.makeText (getApplicationContext (), "click button" , Toast.LENGTH_SHORT) the .Show (); 23 is } 24 }
5. Direct binding label
Xml corresponding to the layout file Activity in a defined event handler method
eg:public void myClick(View source)
source corresponding to the event source (component) corresponding to the component layout file is then to be a triggering event, provided a property: onclick = "myclick" to
MainActivity.java
. 1 public class the MainActivity the extends the Activity { 2 Private the Button BTN; . 3 @Override . 4 protected void the onCreate (the Bundle savedInstanceState) { . 5 Super .onCreate (savedInstanceState); . 6 the setContentView (R.layou.activity_main); . 7 . 8 . 9 10 } . 11 12 is / / customize a method, passing as a parameter a view component 13 is public void MyClick (Source View) { 14 Toast.makeText (getApplicationContext (), "click" , Toast.LENGTH_SHORT) the .Show (); 15 } 16 17 }
main.xml layout file:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 6 <Button 7 android:layout_width="wrap_content" 8 android:layout_height="wrap_content" 9 android:text="按钮" 10 android:onClick="myclick"/> 11 12 </LinearLayout>
IV Summary
In our use, it will be based on different situations, and the choice of different ways to handle the event .
1 in response to specific service (anonymous), generally used for a particular component
Tiers 2 and 3 (internal and external class class), may be common for multiple UI event processing, a UI components suitable for reuse, but the event information source to be passed to the event listener is less flexible, We must pass the member variables and method of construction
Method 4 (activity itself as a listener), it is one way most of our everyday business applications.
Method 5 (xml tag), by providing the UI components android: onClick attribute, then the method code to achieve, in this way because the business logic and UI coupling is too high, the actual business generally do not