给多个button批量添加监听器

让你的activity 继承onclicklisten。然后每个button都设置this 作为监听器。

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;
	}
 
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值