Android Basics Getting Started tutorial--3.1 event handling mechanism based on listening

Source: Internet
Author: User

Android Basics Getting Started tutorial--3.1.1 event handling mechanism based on listening

tags (space delimited): Android Basics Getting Started Tutorial

Introduction to this section:

In the second chapter we learn about the Android UI controls, which we can use to make a beautiful interface, but only the interface; the next step is to start learning logic and business implementation, this section is about the Android event handling mechanism! What is the event handling mechanism? Give a
A simple example, such as clicking a button, we send a login request to the server! Of course, there is more than one event handling mechanism in Android,
For example, when the screen is selected, we click on an area on the screen ... To put it simply, the event-handling mechanism is when we interact with the UI, and we add a little bit of something behind the scenes! In this section, we describe the most frequent use: the event-handling mechanism based on listening!

1. Monitoring-based time-processing mechanism model:

Process Model Diagram:

Text expression:

Event listener consists of three types of objects, event source , event and event listener .
The processing flow is as follows:
Step 1: set up a listener for an event source (component) to listen for user actions
Step 2: user actions that trigger the listener for the event source
Step 3: generate the corresponding event object
Step 4: Pass the event source object as a parameter to the event listener
Step 5: event listener to judge the event object, execute the corresponding event handler (the corresponding event processing method)

Induction:

Event listener mechanism is a delegated event handling mechanism, event source (component) event handler delegate to event listener
Notifies the specified event listener when a specified event occurs on the event source, and performs the appropriate action

2. Five different types of use:

Let's take one of the following:
Simple button Click, prompt toast Information program , use five different forms to achieve!

1) Direct use of anonymous internal classes

Usually the most commonly used one: directly after the Setxxxlistener, the method can be rewritten;
It is usually used temporarily, and reusability is not high!

The implementation code is as follows:Mainacivity.java:

 PackageCom.jay.example.innerlisten;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.Toast;Importandroid.app.Activity; Public  class mainactivity extends Activity {        PrivateButton btnshow;@Override        protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);            Setcontentview (R.layout.activity_main);            Btnshow = (Button) Findviewbyid (r.id.btnshow); Btnshow.setonclicklistener (NewOnclicklistener () {//Override Click event handling method onclick ()            @Override                 Public void OnClick(View v) {//Show toast InformationToast.maketext (Getapplicationcontext (),"You clicked the button.", Toast.length_short). Show ();        }            }); }        }
2) using the inner class

Different from the anonymous inner class above!
Use advantage: You can reuse it in this class, and you can access all the interface components of the external class directly!

The implementation code is as follows:Mainacivity.java:

 PackageCom.jay.example.innerlisten;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.Toast;Importandroid.app.Activity; Public  class mainactivity extends Activity {        PrivateButton btnshow;@Override        protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);            Setcontentview (R.layout.activity_main); Btnshow = (Button) Findviewbyid (r.id.btnshow);//Direct new an inner class object as a parameterBtnshow.setonclicklistener (NewBtnclicklistener ()); }//define an internal class, implement the View.onclicklistener interface, and override the OnClick () methodClass Btnclicklistener implements View.onclicklistener {@Override             Public void OnClick(View v) {Toast.maketext (Getapplicationcontext (),"button is clicked.", Toast.length_short). Show (); }        }    }
3) Use an external class:

is to create another Java file to handle the event, which is less of a form! Because external classes do not have direct access to the user interface
A component in a class that is used to pass a component through a construction method, which results in a lack of concise code!

PS: In order to demonstrate the parameters, use TextView instead of toast tips!

The implementation code is as follows:Myclick.java:

 PackageCom.jay.example.innerlisten;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.TextView; Public  class Myclick implements Onclicklistener {        PrivateTextView textshow;//Pass the text box as a parameter     Public Myclick(TextView txt)        {textshow = txt; }@Override         Public void OnClick(View v) {//Click to set the text displayed in the textboxTextshow.settext ("Click on the button!"); }    }

Mainactivity.java

 PackageCom.jay.example.innerlisten;ImportAndroid.os.Bundle;ImportAndroid.widget.Button;ImportAndroid.widget.TextView;Importandroid.app.Activity; Public  class mainactivity extends Activity {        PrivateButton btnshow;PrivateTextView 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);//Direct new to an external class and pass TextView as a parameterBtnshow.setonclicklistener (NewMyclick (txtshow)); }         }
4) Use activity directly as event listener

Just let the activity class implement the Xxxlistener event listener interface and define overriding event handler methods in activity
Eg:actitity implements the Onclicklistener interface, overriding the OnClick (view) method to add the event listener for some builds
, direct Setxxx.listener (this) can

The implementation code is as follows:Mainacivity.java:

 PackageCom.jay.example.innerlisten;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.Toast;Importandroid.app.Activity;//Let activity method implement Onclicklistener interface Public  class mainactivity extends Activity  implements  Onclicklistener{        PrivateButton btnshow;@Override        protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);            Setcontentview (R.layout.activity_main); Btnshow = (Button) Findviewbyid (r.id.btnshow);//write a directBtnshow.setonclicklistener ( This); }//overriding an abstract method in an interface    @Override         Public void OnClick(View v) {Toast.maketext (Getapplicationcontext (),"Click on the button", Toast.length_short). Show (); }         }
5) bind directly to the label:

is to define an event-handling method directly in the corresponding activity in the XML layout file.
eg:public void Myclick (View source) source corresponding to event sources (component)
Then the layout file corresponds to the build to trigger the event, set a property: OnClick = "Myclick" can be

The implementation code is as follows:Mainacivity.java:

 PackageCom.jay.example.caller;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Toast; Public  class mainactivity extends Activity {        @Override        protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);         Setcontentview (R.layout.activity_main); }//Customize a method to pass in a view component as a parameter     Public void Myclick(View Source) {Toast.maketext (Getapplicationcontext (),"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" >                                                <buttonandroid:layout_width="Wrap_content"android:layout_height= "Wrap_content" Android:text="button"android:onclick="Myclick"/>                                                      </linearlayout>    
Summary of this section

This section gives you an introduction to the event handling mechanism in Android, the example of Onclicklistener click Events, of course, in addition to this there are other events, such as Onitemclicklistener, which need to pass setxxxlistener these, are basically based on event monitoring!
In addition these five kinds of ways use more is: 1,2,3,5 Several, depends on the specific situation ~

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Basics Getting Started tutorial--3.1 event handling mechanism based on listening

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.