Android listener-based event processing

Source: Internet
Author: User

Android listener-based event processing

Android provides powerful event processing mechanisms, mainly including:

1. listener-based event processing mechanism: bind a specific event listener to the Android interface component.

2. Callback-based event processing mechanism: the main method is to rewrite the specific callback method of the Android component or the callback method of the Activity. That is to say, the vast majority of Android interface components provide callback methods for event responses. Developers only need to rewrite them.

 

Listener-based event processing is a more object-oriented event processing method, which is almost the same as the Swing Processing Method in Java.

Listener processing model:

Event Source): The place where an event occurs, that is, various components, such as buttons, windows, and menus.

Event): Events encapsulate specific events (user operations) on interface components ). If the program needs to obtain information about the Event on the interface component, it generally obtains the information through the Event object.

Event Listener): Monitors the time when the event source occurs and responds to each event.

Delegate event handling method:

A common component (event source) delegates the entire event processing to a specific object (event listener). When the event source is generated for a specified time, notify the delegated event listener to process the event.

Each component can specify an event listener for a specific time, and each event listener can also listen to one or more event sources. Because multiple events may occur on the same event source, you can authorize different event processors to process all possible events on the event source; at the same time, a class of events can be processed using the same event listener.

Activity_main.xml

<! -- {Cke_protected} {C} % 3C! % 2D % 2D % 3 Fxml % 20 version % 3D % 221.0% 20 encoding % 3D % 22utf-8% 22% 3F % 2D % 2D % 3E --> <linearlayout xmlns: android = "http://schemas.android.com/apk/res/android" android: orientation = "vertical" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: gravity = "center_horizontal"> <edittext android: id = "@ + id/txt" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: edit Able = "false" android: cursorvisible = "false" android: textsize = "12pt"> <! -- {Cke_protected} {C} % 3C! % 2D % 2D % 20% E5 % AE % 9A % E4 % B9 % 89% E4 % B8 % 80% E4 % B8 % AA % E6 % 8C % 89% E9 % 92% AE % EF % BC % 8C % E8 % AF % A5 % E6 % 8C % 89% E9 % 92% AE % E5 % B0 % 86% E4 % BD % 9C % E4 % B8 % BA % E4 % BA % 8B % E4 % BB % B6 % E6 % BA % 90% 2D % 2D % 3E --> <button android: id = "@ + id/bn" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Click me"> </button> </edittext> </linearlayout>
MainActivity. java
Public class MainActivity extends Activity {@ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // obtain the bn Button in the application. Button bn = (Button) findViewById (R. id. bn); // bind the event listener to the button. Bn. setOnClickListener (new MyClickListener (); // ①} // defines the listener class MyClickListener implements View for a click event. onClickListener {// method required to implement the listener class. This method serves as the event processor @ Overridepublic void onClick (View v) {EditText txt = (EditText) findViewById(R.id.txt); txt. setText ("bn button clicked! ");}}}
The procedure for programming the listener-based event processing model is as follows:

 

1. Obtain the common interface component (event source), that is, the Monitored object.

2. Implement the time listener class. This listener class is a special Java class and must implement a XxxListener interface.

3. Call the setXXXListener method of the event source to register the event listener object with a common component (event source)

 

The event listener must implement the event listening interface. Different Android interface components provide different listener interfaces, which usually exist in the form of internal classes. Taking the View class as an example, it contains the following listening interfaces:

View. OnClickListener: specifies the time required for clicking the event Listener of an event.

View. OnCreateContextMenuListener: the interface that must be implemented by the event listener when the context menu is created

View. onFocusChangeListener: the interface that must be implemented by the event listener whose focus changes the event

View. OnKeyListener: the interface that must be implemented by the time listener of the key event

The so-called event listener is actually a java class instance that implements a specific interface. There are several forms in the program:

1. Internal class form: Define the event listener as the internal class of the current class

 

Public class EventQs extends Activity {@ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // obtain the bn Button in the application. Button bn = (Button) findViewById (R. id. bn); // bind the event listener to the button. Bn. setOnClickListener (new MyClickListener (); // defines the listener of a click event. The class MyClickListener implements View is an internal class. onClickListener {// method required to implement the listener class. This method serves as the event processor @ Overridepublic void onClick (View v) {EditText txt = (EditText) findViewById(R.id.txt); txt. setText ("bn button clicked! ");}}}
Because the listener is an internal class, you can freely access all interface components of the external class, which is also an advantage of the internal class.

 

2. Form of external class: Define the event listener class as an external class

The event listener in the form of an external class cannot freely access the component that creates the GUI class. When using the event listener, you must pass the reference of the component of the GUI class to the listener class, so it is rarely used.

MainActivity. java

 

Public class MainActivity extends Activity {EditText address; EditText content; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // obtain the recipient address and text message content address = (EditText) findViewById (R. id. address); content = (EditText) findViewById (R. id. content); Button bn = (Button) findViewById (R. id. send); bn. setOnLongClickListener (new SendSmsListener (this, address, content); // external class form, which needs to be passed }}
SendSmsListener. java

 

 

Public class SendSmsListener implements OnLongClickListener {private Activity act; private EditText address; private EditText content; public SendSmsListener (Activity act, EditText address, EditText content) {this. act = act; this. address = address; this. content = content ;}@ Overridepublic boolean onLongClick (View source) {String addressStr = address. getText (). toString (); String contentStr = content. getText (). toString (); // get the SMS manager SmsManager smsManager = SmsManager. getDefault (); // create the PendingIntentPendingIntent sentIntent = PendingIntent. getBroadcast (act, 0, new Intent (), 0); // send smsManager text message. sendTextMessage (addressStr, null, contentStr, sentIntent, null); Toast. makeText (act, "message sent successfully", Toast. LENGTH_LONG ). show (); return false ;}}

 

3. the Activity itself serves as the event listener class: Let the Activity itself implement the listener interface and implement the event processor Method

 

// Activity implements the event listener interface public class ActivityListener extends Activityimplements OnClickListener {EditText show; Button bn; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); show = (EditText) findViewById (R. id. show); bn = (Button) findViewById (R. id. bn); // directly use Activity as the event listener bn. setOnClickListener (this);} // event handling method @ Overridepublic void o NClick (View v) {show. setText ("bn button clicked! ");}}

 

4. Anonymous internal class: use an anonymous internal class to create an event listener object. The code is simple and can only be used once.

 

Public class MainActivity extends Activity {EditText show; Button bn; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); show = (EditText) findViewById (R. id. show); bn = (Button) findViewById (R. id. bn); // directly use Activity as the event listener bn. setOnClickListener (new OnClickListener () {// event handling method @ Overridepublic void onClick (View v) {show. setText ("bn button ticket Click! ");}});}

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.