ButtonのsetOnClickListenerですが、匿名クラスを使ったりと色々やり方があるようです。それぞれ良し悪しが個々の状況によってあると思います。すぐに使えるように確認して見ました。
Android 8.0
Button onClickListner
このサイトでのButton Clickのリスナーは匿名クラスを多用していますが、他にも方法があります。規模が大きなprojectになれば使い方も変えた方がメンテナンスしやすくなります。
匿名クラス
GoogleのReferenceに紹介されているやり方で、メソッド内でクラスを定義する匿名(無名)クラスを使います。ボタンが1、2個であればこれで済ませることが多いですね。
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Button button1 = findViewById(R.id.button_1); button1.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Log.d("debug", "button1, Perform action on click"); } }); final Button button2 = findViewById(R.id.button_2); button2.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Log.d("debug","button2, Perform action on click"); } }); } } |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" tools:context=".MainActivity"> <Button android:id="@+id/button_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button 1" /> <Button android:id="@+id/button_2" android:layout_margin="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button 2" /> </LinearLayout> |
変数でまとめる
匿名クラスを使う方法は1文なのですが、長ったらしいしごちゃごちゃする、最後の;を忘れることがある等々
1 2 3 4 5 |
button1.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Log.d("debug", "button1, Perform action on click"); } }); |
単純に見やすくするには、これを変数に置き換えてしまうやり方があります
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Button button1 = findViewById(R.id.button_1); button1.setOnClickListener(buttonClick); final Button button2 = findViewById(R.id.button_2); button2.setOnClickListener(buttonClick); } private View.OnClickListener buttonClick = new View.OnClickListener() { @Override public void onClick(View view) { switch (view.getId()) { case R.id.button_1: Log.d("debug","button1, Perform action on click"); break; case R.id.button_2: Log.d("debug","button2, Perform action on click"); break; } } }; } |
レイアウトは同じです。
OnClickListener を implementsして onClick を定義
匿名クラスとしない場合はどうするかというと、OnClickListenerをimplementsします。するとpublic void onClick(View view){}というメソッドを作るようにように要求されます。
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; public class MainActivity extends Activity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Button button1 = findViewById(R.id.button_1); button1.setOnClickListener(this); final Button button2 = findViewById(R.id.button_2); button2.setOnClickListener(this); } public void onClick(View view){ switch (view.getId()) { case R.id.button_1: Log.d("debug","button1, Perform action on click"); break; case R.id.button_2: Log.d("debug","button2, Perform action on click"); break; } } } |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" tools:context=".MainActivity"> <Button android:id="@+id/button_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button 1" /> <Button android:id="@+id/button_2" android:layout_margin="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button 2" /> </LinearLayout> |
ボタンクリックでの処理を1か所にまとめられるので可読性が上がるかもしれません。
カスタムクラスを作成
メソッドでクリックを受けていますが、カスタムクラスにしてしまうこともできます。その場合は、OnClickListenerを継承したクラスとします。
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Button button1 = findViewById(R.id.button_1); button1.setOnClickListener(new buttonClick()); final Button button2 = findViewById(R.id.button_2); button2.setOnClickListener(new buttonClick()); } } class buttonClick implements View.OnClickListener { @Override public void onClick(View view) { switch (view.getId()) { case R.id.button_1: Log.d("debug","button1, Perform action on click"); break; case R.id.button_2: Log.d("debug","button2, Perform action on click"); break; } } } |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" tools:context=".MainActivity"> <Button android:id="@+id/button_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button 1" /> <Button android:id="@+id/button_2" android:layout_margin="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button 2" /> </LinearLayout> |
android:onClick
XMLレイアウトの中でandroid:onClickを定義してメソッドを実行させるケースです。これもGoogleのReferenceで紹介されています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void selfDestruct(View view) { switch (view.getId()) { case R.id.button_1: Log.d("debug","button1, Perform action on click"); break; case R.id.button_2: Log.d("debug","button2, Perform action on click"); break; } } } |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" tools:context=".MainActivity"> <Button android:id="@+id/button_1" android:onClick="selfDestruct" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button 1" /> <Button android:id="@+id/button_2" android:onClick="selfDestruct" android:layout_margin="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button 2" /> </LinearLayout> |
ActivityのコードにはButtonのインスタンスもありません、これが気持ち悪いと感じるかどうかです。Fragmentではうまくつかえるのでしょうか…
5種類のButton Click
この5種類をテストするコードを書いてみました。
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.text_view); // 匿名クラス final Button button0 = findViewById(R.id.button_0); button0.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { String str = getString(R.string.bt0); textView.setText(str); } }); // 変数でまとめる final Button button1 = findViewById(R.id.button_1); button1.setOnClickListener(buttonClick); // OnClickListener を implementsして onClick を定義 final Button button2 = findViewById(R.id.button_2); button2.setOnClickListener(this); // カスタムクラス final Button button3 = findViewById(R.id.button_3); button3.setOnClickListener(new BtClick()); } // 変数でまとめる private View.OnClickListener buttonClick = new View.OnClickListener() { @Override public void onClick(View view) { switch (view.getId()) { case R.id.button_1: String str = getString(R.string.bt1); textView.setText(str); break; } } }; // OnClickListener を implementsして onClick を定義 public void onClick(View view){ switch (view.getId()) { case R.id.button_2: String str = getString(R.string.bt2); textView.setText(str); break; } } // カスタムクラス class BtClick implements View.OnClickListener { @Override public void onClick(View view) { switch (view.getId()) { case R.id.button_3: String str = getString(R.string.bt3); textView.setText(str); break; } } } // android:onClick public void selfDestruct(View view) { String str = getString(R.string.bt4); textView.setText(str); } } |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" android:padding="50dp" tools:context=".MainActivity"> <TextView android:id="@+id/text_view" android:text="@string/app_name" android:textColor="#000" android:textSize="30sp" android:layout_margin="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/button_0" android:text="@string/bt0" android:layout_margin="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/button_1" android:text="@string/bt1" android:layout_margin="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/button_2" android:text="@string/bt2" android:layout_margin="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/button_3" android:text="@string/bt3" android:layout_margin="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/button_4" android:text="@string/bt4" android:onClick="selfDestruct" android:layout_margin="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> |
リソース
strings.xml
1 2 3 4 5 6 7 8 |
<resources> <string name="app_name">TestButtonClick</string> <string name="bt0">Button 0</string> <string name="bt1">Button 1</string> <string name="bt2">Button 2</string> <string name="bt3">Button 3</string> <string name="bt4">Button 4</string> </resources> |
References:
Button
関連ページ:
- 簡単な Button アプリを作る
- ButtonをJavaコードだけで設定する
- カスタムボタンを作る
- ImageButton:Button, ImageButton に画像を設定する
- onClickListenerの色々な設定
- Button 配列を設定する