buttonのクリックイベントをまとめる
- implements View.OnClickListener
- buttonにリスナーを追加
- onClick(View v)メソッドをOverride
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
Log.d("myTag", "click = button1");
break;
case R.id.button2:
Log.d("myTag", "click = button2");
break;
}
}
}