[Android / Java] Understand the description type in listener settings (button installation)

Introduction

Various Listeners that appear in Android application development. There are two major patterns of writing in a java file, and it was confusing, so I will write it as a memorandum.

This sample is an application that displays a message when you press a button.

Confirmation of terms

Event: An operation to be performed on the screen (example: click) Event handler ・ ・ ・ Processing to be performed for the event (Example: Display text when clicked) Listener: Something that detects an event (example: button)

code

The layout is suitable.

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/text" />

</androidx.constraintlayout.widget.ConstraintLayout>

Pattern 1: Describe event & event handler and listener separately

MainActivity.java


public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //Listener setting (event detection button installed)
        Button button1 = findViewById(R.id.btn);
        button1.setOnClickListener(new TestListener());
    }

    //Creating a listener class(Click the button+Text display)
    private class TestListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            TextView textView = findViewById(R.id.text);
            textView.setText("I clicked button1");
        }
    }
}

Pattern 2: Describe events & event handlers & listeners together

MainActivity.java


public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Describe all together
        Button button2 = findViewById(R.id.btn);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TextView textView = findViewById(R.id.text);
                textView.setText("I clicked button2");
            }
        });
    }
}

Finally

In Button ・ SetOnClickListener ・ View.OnClickListener ・ OnClick Since the flow is decided, recognize it as one set.

Postscript (2020/11/1)

I introduced two, but in View.OnClickListener, it seems to be set in the basic anonymous class. Therefore, pattern 1 should be used as a reference for understanding the structure!

Recommended Posts

[Android / Java] Understand the description type in listener settings (button installation)
[Android] I want to get the listener from the button in ListView
Java reference to understand in the figure
The intersection type introduced in Java 10 is amazing (?)
Organized memo in the head (Java --Data type)
Type determination in Java
Java classes and instances to understand in the figure
Refer to C ++ in the Android Studio module (Java / kotlin)
[Android, Java] Convenient method to calculate the difference in days
Try functional type in Java! ①
Access the network interface in Java
Try implementing Android Hilt in Java
Guess the character code in Java
Unzip the zip file in Java
Let's understand the Array (Element) type!
Parsing the COTOHA API in Java
Settings for SSL debugging in Java
Let's understand the Optional (Wrapped) type!
Call the super method in Java
[Android] Easily control the event when the Back button is pressed in Fragment
[Java] Display the bit string stored in the byte type variable on the console
What wasn't fair use in the diversion of Java APIs on Android
Reason to add L to the number to be put in Java long type
Understand the difference between int and Integer and BigInteger in java and float and double
[Android Studio] Description that can be continuously input in SQLite Database [Java]