1
0

More than 3 years have passed since last update.

【Android/Java】リスナ設定(ボタン設置)における記述型式を把握する

Last updated at Posted at 2020-10-30

はじめに

Androidアプリ開発で出てくる様々なListener。
javaファイルでの書き方が大きく2パターンあり、混乱したので備忘録として記しておく。

今回のサンプルはボタンを押すと、メッセージが表示されるアプリです。
※尚、ボタンはボタンでも、ActionBarにボタン(オプションメニュー)を設置する方法は全く違うのでこちらを参照してみてください。
https://qiita.com/kilalabu/items/62a39e63ee2c6d4293bd

用語の確認

イベント ・・・ 画面に対して行う操作のこと (例:クリックする)
イベントハンドラ ・・・ そのイベントに対して行う処理のこと (例:クリックされたらテキストを表示する)
リスナ ・・・ イベントの検知をしてくれるもの (例:ボタン)

コード

レイアウトは適当です。

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="ボタン"
        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>

パターン1: イベント&イベントハンドラとリスナを分けて記述

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

        // リスナ設定(イベント検知ボタンを設置)
        Button button1 = findViewById(R.id.btn);
        button1.setOnClickListener(new TestListener());
    }

    // リスナクラスの作成(ボタンをクリック+テキスト表示)
    private class TestListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            TextView textView = findViewById(R.id.text);
            textView.setText("button1をクリックしました");
        }
    }
}

パターン2: イベント&イベントハンドラ&リスナをまとめて記述

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

        // 全てまとめて記述
        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("button2をクリックしました");
            }
        });
    }
}

最後に

Buttonにおいては
・setOnClickListener
・View.OnClickListener
・onClick
という流れが決まっているので1セットとして認識すること。

追記(2020/11/1)

2つ紹介しましたがView.OnClickListenerにおいては基本無名クラスでセットするみたいです。
ですのでパターン1は構造を理解するための参考にしていただければと!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0