Easy Tutorial
❮ Es6 Generator Scala Method Functio Different ❯

3.1.1 Event Handling Mechanism Based on Listening

Category Android Basic Tutorial

Introduction to This Section:

>

In the second chapter, we learned about Android's UI controls, which we can use to create an exquisite interface, but that's all; the interface itself. The next step is to start learning about logic and business implementation, and this chapter discusses Android's event handling mechanism! What is an event handling mechanism? For a simple example, when you click a button, we send a login request to the server! Of course, there is more than one event handling mechanism in Android, such as screen selection, when we click on a certain area of the screen... In short, the event handling mechanism is just a few small actions we add behind the scenes when we interact with the UI! In this section, we will introduce the most frequently used one: the event handling mechanism based on listening!


1. The Event Handling Mechanism Based on Listening Model:

Flowchart:

Text Description:

>

In the event listening mechanism, there are three types of objects: event source, event, and event listener. The processing process is as follows: Step 1: Set a listener for a certain event source (component) to monitor user operations. Step 2: The user's operation triggers the listener of the event source. Step 3: A corresponding event object is generated. Step 4: Pass this event source object as a parameter to the event listener. Step 5: The event listener judges the event object and executes the corresponding event handler (the processing method for the corresponding event).

Summary:

>

The event listening mechanism is a delegated event handling mechanism, where the event source (component) delegates event handling to the event listener. When a specified event occurs in the event source, it notifies the specified event listener to perform the corresponding operation.


2. Five Different Usage Forms:

We will implement the following: a simple button click to display Toast information program; using five different forms!

Effect Picture:


1) Directly using an anonymous inner class

>

The most commonly used one: directly setXxxListener, and then override the method inside; It is usually used once and has low reusability!

The implementation code is as follows: MainAcivity.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() {    
            //Override the click event handling method onClick()    
            @Override    
            public void onClick(View v) {    
                //Display Toast information    
                Toast.makeText(getApplicationContext(), "You clicked the button", Toast.LENGTH_SHORT).show();    
            }    
        });    
    }        
}

2) Using an inner class

>

Different from the anonymous inner class above! The advantage of using it: it can be reused in this class and can directly access all the UI components of the outer class!

The implementation code is as follows: MainAcivity.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 inner class object as a parameter    
        btnshow.setOnClickListener(new BtnClickListener());    
    }     
    //Define an inner class, implement the View.OnClickListener interface, and override the onClick() method    
    class BtnClickListener implements View.OnClickListener    
    {    
        @Override    
        public void onClick(View v) {    
            Toast.makeText(getApplicationContext(), "The button was clicked", Toast.LENGTH_SHORT).show();   
        }    
    }    
}

3) Using an external class:

>

It is to create another Java file to handle events separately, which is used less frequently! Because the external class cannot directly access the components in the user interface class, it needs to be passed in through the constructor method for use; this results in the code not btnshow = (Button) findViewById(R.id.btnshow); // Directly write this btnshow.setOnClickListener(this); }

// Override the abstract method in the interface @Override public void onClick(View v) { Toast.makeText(getApplicationContext(), "Clicked the button", Toast.LENGTH_SHORT).show(); }
}


5) Direct Binding to Tag:

>

It is directly defining an event handling method in the corresponding Activity in the xml layout file. eg: public void myClick(View source) source corresponds to the event source (component) Then in the layout file, set an attribute for the component that needs to trigger the event: onclick = "myclick"

The implementation code is as follows: 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);
    }
    // Define a custom method, passing in a view component as a parameter
    public void myclick(View source)
    {
        Toast.makeText(getApplicationContext(), "The button was 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>

Summary of This Section

>

This section introduced the event handling mechanism in Android, with the example being the onClickListener click event. Of course, in addition to this, there are other events such as onItemClickListener. All those that need to be set through setXxxListener are basically based on event listening! In addition, the more commonly used methods are: 1, 2, 3, 5, depending on the specific situation.

-1.0 Android Basic Tutorial Introduction

-1.0.1 Latest Android Basic Tutorial Catalog for 2015

-1.1 Background and System Architecture Analysis

-1.2 Development Environment Setup

-1.2.1 Developing Android APP with Eclipse + ADT + SDK

-1.2.2 Developing Android APP with Android Studio

-1.3 Solving SDK Update Issues

-1.4 Genymotion Emulator Installation

-1.5.1 Git Tutorial on Basic Operations of Local Repositories

-1.5.2 Git on Using GitHub to Set Up a Remote Repository

-1.6 How to Play with 9 (Nine Sister) Images

-1.7 Interface Prototype Design

-1.8 Project Related Analysis (Various Files, Resource Access)

-1.9 Android Program Signing and Packaging

-1.11 Decompiling APK to Obtain Code & Resources

-2.1 The Concept of View and ViewGroup

-2.2.1 LinearLayout (Linear Layout)

-2.2.2 RelativeLayout (Relative Layout)

-2.2.3 TableLayout (Table Layout)

-2.2.4 FrameLayout (Frame Layout)

-2.2.5 GridLayout (Grid Layout)

-2.2.6 AbsoluteLayout (Absolute Layout)

-2.3.1 TextView (Text Box) Detailed Explanation

-2.3.2 EditText (Input Box) Detailed Explanation

-2.3.3 Button (Button) and ImageButton (Image Button)

WeChat Follow

❮ Es6 Generator Scala Method Functio Different ❯