複数buttonの一括リスナーの追加
6030 ワード
あなたのactivityにonclicklistenを継承させます.その後、各buttonはthisをリスナーとして設定します.
Activityの実現方法はviewのIDで区別する
Activityの実現方法はviewのIDで区別する
package com.aiai.www;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.ImageButton;
public class MalataNumSearchActivity extends Activity implements OnClickListener, OnTouchListener{
/** Called when the activity is first created. */
ImageButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;
ImageButton bcls, barrow, bdot;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
b0 = (ImageButton) findViewById(R.id.imageButton0);
b0.setOnTouchListener(this);
b1 = (ImageButton) findViewById(R.id.imageButton1);
b1.setOnTouchListener(this);
b2 = (ImageButton) findViewById(R.id.imageButton2);
b2.setOnTouchListener(this);
b3 = (ImageButton) findViewById(R.id.imageButton3);
b3.setOnTouchListener(this);
b4 = (ImageButton) findViewById(R.id.imageButton4);
b4.setOnTouchListener(this);
b5 = (ImageButton) findViewById(R.id.imageButton5);
b5.setOnTouchListener(this);
b6 = (ImageButton) findViewById(R.id.imageButton6);
b6.setOnTouchListener(this);
b7 = (ImageButton) findViewById(R.id.imageButton7);
b7.setOnTouchListener(this);
b8 = (ImageButton) findViewById(R.id.imageButton8);
b8.setOnTouchListener(this);
b9 = (ImageButton) findViewById(R.id.imageButton9);
b9.setOnTouchListener(this);
bcls = (ImageButton) findViewById(R.id.imageButtonCls);
bcls.setOnTouchListener(this);
barrow = (ImageButton) findViewById(R.id.imageButtonarrow);
barrow.setOnTouchListener(this);
bdot = (ImageButton) findViewById(R.id.imageButtondot);
bdot.setOnTouchListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("onClick id ="+v.getId());
}
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.imageButton0:
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
b0.setBackgroundDrawable(null);
}
else if (event.getAction() == MotionEvent.ACTION_UP)
{
b0.setBackgroundDrawable(getResources()
.getDrawable(R.drawable.n_0));
}
break;
case R.id.imageButton1:
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
b1.setBackgroundDrawable(null);
}
else if (event.getAction() == MotionEvent.ACTION_UP)
{
b1.setBackgroundDrawable(getResources()
.getDrawable(R.drawable.n_1));
}
break;
case R.id.imageButton2:
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
b2.setBackgroundDrawable(null);
}
else if (event.getAction() == MotionEvent.ACTION_UP)
{
b2.setBackgroundDrawable(getResources()
.getDrawable(R.drawable.n_2));
}
break;
case R.id.imageButton3:
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
b3.setBackgroundDrawable(null);
}
else if (event.getAction() == MotionEvent.ACTION_UP)
{
b3.setBackgroundDrawable(getResources()
.getDrawable(R.drawable.n_3));
}
break;
case R.id.imageButton4:
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
b4.setBackgroundDrawable(null);
}
else if (event.getAction() == MotionEvent.ACTION_UP)
{
b4.setBackgroundDrawable(getResources()
.getDrawable(R.drawable.n_4));
}
break;
case R.id.imageButton5:
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
b5.setBackgroundDrawable(null);
}
else if (event.getAction() == MotionEvent.ACTION_UP)
{
b5.setBackgroundDrawable(getResources()
.getDrawable(R.drawable.n_5));
}
break;
case R.id.imageButton6:
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
b6.setBackgroundDrawable(null);
}
else if (event.getAction() == MotionEvent.ACTION_UP)
{
b6.setBackgroundDrawable(getResources()
.getDrawable(R.drawable.n_6));
}
break;
case R.id.imageButton7:
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
b7.setBackgroundDrawable(null);
}
else if (event.getAction() == MotionEvent.ACTION_UP)
{
b7.setBackgroundDrawable(getResources()
.getDrawable(R.drawable.n_7));
}
break;
case R.id.imageButton8:
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
b8.setBackgroundDrawable(null);
}
else if (event.getAction() == MotionEvent.ACTION_UP)
{
b8.setBackgroundDrawable(getResources()
.getDrawable(R.drawable.n_8));
}
break;
case R.id.imageButton9:
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
b9.setBackgroundDrawable(null);
}
else if (event.getAction() == MotionEvent.ACTION_UP)
{
b9.setBackgroundDrawable(getResources()
.getDrawable(R.drawable.n_9));
}
break;
case R.id.imageButtonarrow:
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
barrow.setBackgroundDrawable(null);
}
else if (event.getAction() == MotionEvent.ACTION_UP)
{
barrow.setBackgroundDrawable(getResources()
.getDrawable(R.drawable.arrow));
}
break;
case R.id.imageButtonCls:
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
bcls.setBackgroundDrawable(null);
}
else if (event.getAction() == MotionEvent.ACTION_UP)
{
bcls.setBackgroundDrawable(getResources()
.getDrawable(R.drawable.clear));
}
break;
case R.id.imageButtondot:
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
bdot.setBackgroundDrawable(null);
}
else if (event.getAction() == MotionEvent.ACTION_UP)
{
bdot.setBackgroundDrawable(getResources()
.getDrawable(R.drawable.dot));
}
break;
default :
break;
}
return false;
}
}