关于很大数量的button肿么处理

Redknot 2013-08-10 01:24:31
做了一个化学元素周期表,里面要用到大量的按钮,该咋做按钮的监听啊?难道一个一个做?我用的这种。
可是一所有的按钮似乎都没有区别了,我想想让程序知道我按的到底是哪一个按钮,该怎么办?
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onefindView();
twofindView();

MyOnTouchListener[] touch = new MyOnTouchListener[7];
MyButton listener = new MyButton();

for(int i=0;i<=6;i++){
oneyuansu[i].setOnClickListener(listener);
}
for(int i=0;i<=5;i++){
twoyuansu[i].setOnClickListener(listener);
}

}




class MyButton implements OnClickListener{

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,Information.class);
System.out.println("jbkjblkhkj");
startActivity(intent);
}

}
...全文
128 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Redknot 2013-08-11
  • 打赏
  • 举报
回复
实现了,用的这种方法
package redknot.example.elements;
import android.app.Activity;
import android.content.Intent;
import android.graphics.ColorMatrixColorFilter;
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 MainActivity extends Activity {
	
	private ImageButton[] oneyuansu = new ImageButton[7];
	private ImageButton[] twoyuansu = new ImageButton[6];
	
	protected void onCreate(Bundle savedInstanceState) {
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		onefindView();
		twofindView();
		
		MyOnTouchListener touch = new MyOnTouchListener();
		
		for(int i=0;i<=6;i++){
			oneyuansu[i].setOnClickListener(new MyButton(i));
		}
		for(int i=0;i<=5;i++){
			twoyuansu[i].setOnClickListener(new MyButton(i));
		}
		
	}
	
	class MyButton implements OnClickListener{
		
		private int c;
		
		public MyButton(int i){
		     c = i;
		}

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			System.out.println(c);
			Intent intent = new Intent(MainActivity.this,Information.class);
			startActivity(intent);
		}
		
	}
	
	
	public void onefindView(){
		oneyuansu[0] = (ImageButton) findViewById(R.id.h1);
		oneyuansu[1] = (ImageButton) findViewById(R.id.li1);
		oneyuansu[2] = (ImageButton) findViewById(R.id.na1);
		oneyuansu[3] = (ImageButton) findViewById(R.id.k1);
		oneyuansu[4] = (ImageButton) findViewById(R.id.rb1);
		oneyuansu[5] = (ImageButton) findViewById(R.id.cs1);
		oneyuansu[6] = (ImageButton) findViewById(R.id.fr1);
		
	}
	public void twofindView(){
		twoyuansu[0] = (ImageButton) findViewById(R.id.be1);
		twoyuansu[1] = (ImageButton) findViewById(R.id.mg1);
		twoyuansu[2] = (ImageButton) findViewById(R.id.ca1);
		twoyuansu[3] = (ImageButton) findViewById(R.id.sr1);
		twoyuansu[4] = (ImageButton) findViewById(R.id.ba1);
		twoyuansu[5] = (ImageButton) findViewById(R.id.ra1);
	}
	
	/*为了使图片按钮按下和弹起状态不同,采用过滤颜色的方法.按下的时候让图片颜色变淡*/
	public class MyOnTouchListener implements OnTouchListener{

		public final  float[] BT_SELECTED=new float[]
				{ 2, 0, 0, 0, 2,
			0, 2, 0, 0, 2,
			0, 0, 2, 0, 2,
			0, 0, 0, 1, 0 };			    

		public final float[] BT_NOT_SELECTED=new float[]
				{ 1, 0, 0, 0, 0,
			0, 1, 0, 0, 0,
			0, 0, 1, 0, 0,
			0, 0, 0, 1, 0 };
		public boolean onTouch(View v, MotionEvent event) {
			// TODO Auto-generated method stub
			if(event.getAction() == MotionEvent.ACTION_DOWN){
				System.out.println("ijksahfkjshqklf");
				v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_SELECTED));
				v.setBackgroundDrawable(v.getBackground());
			}
			else if(event.getAction() == MotionEvent.ACTION_UP){
				v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_NOT_SELECTED));
				v.setBackgroundDrawable(v.getBackground());
				
			}
			return false;
		}

	}
	
	
	
 
}
丰子 2013-08-11
  • 打赏
  • 举报
回复
可以使用gridvew或listview实现
freesonfish 2013-08-11
  • 打赏
  • 举报
回复
2楼的方法也行,大师还是建议通过一个tag来判断
帅得不敢出门 2013-08-10
  • 打赏
  • 举报
回复
好像可能通过判断 if (v == oneyuansu[i]) 查看是哪个按钮按下。
失落夏天 版主 2013-08-10
  • 打赏
  • 举报
回复
我晕死,当然不是了 你做一个MyButton类, 类里面写好监听 构造函数里面传入该按钮的值,这个按钮就成了一个独立的对象了。 监听程序里面根据构造函数中传入的值的不同而发送不同的值。

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧