做了一个化学元素周期表,里面要用到大量的按钮,该咋做按钮的监听啊?难道一个一个做?我用的这种。
可是一所有的按钮似乎都没有区别了,我想想让程序知道我按的到底是哪一个按钮,该怎么办?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);
}

}Java

解决方案 »

  1.   

    我晕死,当然不是了
    你做一个MyButton类,
    类里面写好监听
    构造函数里面传入该按钮的值,这个按钮就成了一个独立的对象了。
    监听程序里面根据构造函数中传入的值的不同而发送不同的值。
      

  2.   

    好像可能通过判断
    if (v == oneyuansu[i])
    查看是哪个按钮按下。
      

  3.   

    2楼的方法也行,大师还是建议通过一个tag来判断
      

  4.   

    实现了,用的这种方法
    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;
    } }



     
    }