做了一个化学元素周期表,里面要用到大量的按钮,该咋做按钮的监听啊?难道一个一个做?我用的这种。
可是一所有的按钮似乎都没有区别了,我想想让程序知道我按的到底是哪一个按钮,该怎么办?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类
可是一所有的按钮似乎都没有区别了,我想想让程序知道我按的到底是哪一个按钮,该怎么办?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类
解决方案 »
- AndEngine连demo都跑不起来求助
- flash播放器 for android
- 内存中的jpeg数据传输到jni中解码然后得到bitmap!!
- activity 里面的this 怎么理解?
- 进公司不到一个月,想辞职重新找工作
- bitmap放大问题.
- 一个c函数输出是void*类型的东西,请问在jni怎么处理?
- 真心求帮忙,android关于找不到类
- 关于layoutInflater.inflate(int id,ViewRoot root,boolean flag)
- 使用2.3引用ActionBarSherlock报错
- ViewFlipper切换图片的一些问题
- 点击ListPreference控件,会弹出List列表框,如何将这个列表框中的某一项设置为不可用
你做一个MyButton类,
类里面写好监听
构造函数里面传入该按钮的值,这个按钮就成了一个独立的对象了。
监听程序里面根据构造函数中传入的值的不同而发送不同的值。
if (v == oneyuansu[i])
查看是哪个按钮按下。
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;
} }
}