Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按钮阵列上的ANDROID onTouchListener_Android_Arrays - Fatal编程技术网

按钮阵列上的ANDROID onTouchListener

按钮阵列上的ANDROID onTouchListener,android,arrays,Android,Arrays,我在一个按钮阵列中放置了很多按钮(16*16)。 按钮编号与它们应该在另一个数组中进行的更改直接相关(例如,按钮[12][7]将统计[12][7]的值设置为1) 所以我认为可以在onTouch方法中设置一行,该方法对每个按钮都有反应。 示例(当然,不起作用) 在这个伪代码中,按钮将创建2个ints来描述传递给stat数组的数组的2个维度 如果有人能解决这个问题,他今晚会帮我省下几个小时 谢谢您的回答。您是否将onTouchListener添加到按钮的容器中 最好是为每个按钮添加一个onTouch

我在一个按钮阵列中放置了很多按钮(16*16)。 按钮编号与它们应该在另一个数组中进行的更改直接相关(例如,
按钮[12][7]
统计[12][7]
的值设置为1)

所以我认为可以在
onTouch
方法中设置一行,该方法对每个按钮都有反应。 示例(当然,不起作用)

在这个伪代码中,按钮将创建2个
int
s来描述传递给stat数组的数组的2个维度

如果有人能解决这个问题,他今晚会帮我省下几个小时


谢谢您的回答。

您是否将onTouchListener添加到按钮的容器中

最好是为每个按钮添加一个onTouchListener,然后arg0将对应于特定的按钮


另一个选项是使用GridView,它有一个您可以使用的setOnItemClickListener

试试这样的方法:

Button[][] myButtonMatrix = new Button[] {
 new Button[] { button11, button12, button13, button 14 },
 new Button[] { button21, button22, button23, button24 }
};

public class MatrixButtonListener implements View.OnClickListener {
        private int x;
        private int y;

        public MatrixButtonListener(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public int getX() { return x; }
        public int getY() { return y; }

        @Override
        public void onClick(View v) {
            stat[x][y] = x-y; // changes were made only in relation to x and y, nothing else
            // for example:
            if(x == 0) { 
                // button in first row
                // do something
            }
        }
    };

// to apply to each button in matrix:

for(int i=0; i<myButtonMatrix.length; i++) {
    for(int j=0; j<myButtonMatrix[i].length; j++) {
         myButtonMatrix[i][j].setOnClickListener(new MatrixButtonListener(i,j));
    }
}
按钮[][]我的按钮矩阵=新按钮[]{
新按钮[]{button11,button12,button13,button14},
新按钮[]{button21,button22,button23,button24}
};
公共类MatrixButtonListener实现View.OnClickListener{
私人INTX;
私营企业;
公共矩阵ButtonListener(整数x,整数y){
这个.x=x;
这个。y=y;
}
public int getX(){return x;}
public int getY(){return y;}
@凌驾
公共void onClick(视图v){
stat[x][y]=x-y;//只对x和y进行了更改,没有其他更改
//例如:
如果(x==0){
//第一行的按钮
//做点什么
}
}
};
//要应用于矩阵中的每个按钮,请执行以下操作:

对于(inti=0;i我认为HasMap是一个更好的解决方案

private HashMap<Integer,Button> btnMap = new HashMap<Integer, Button>();

private void init(){
    Button yourFirstBtn = (Button) findViewById(R.id.yourFirstBtn);
    btnMap.put(yourFirstBtn.getId(), yourFirstBtn);

    for(Button tempBtn: btnMap.values()){
        tempBtn.setOnClickListener(this);
    }
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
    Button clickedBtn = btnMap.get(v.getId());
}
private HashMap btnMap=new HashMap();
私有void init(){
按钮yourFirstBtn=(按钮)findViewById(R.id.yourFirstBtn);
btnMap.put(yourFirstBtn.getId(),yourFirstBtn);
对于(按钮tempBtn:btnMap.values()){
tempBtn.setOnClickListener(此);
}
}
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
按钮clickedBtn=btnMap.get(v.getId());
}

在将每个按钮添加到数组中时,设置一个指示其索引的标记。标记用于将属性添加到视图中,而不必求助于其他数据结构

例如:

button[12][7].setTag("12|7");
如果您的按钮是在XML中预定义的,那么您可以对以下内容执行相同的操作:

android:tag="12|7"
然后在touch listener中(我假设所有按钮都连接了同一个按钮),从被触摸的视图中获取标签:

String tag = (String) view.getTag();
然后,根据需要使用子字符串并使用两个索引:

String indx1 = tag.substring(0, tag.indexOf("|"));
String indx2 = tag.substring(tag.indexOf("|")+1);
stat[Integer.parseInt(indx1)][Integer.parseInt(indx2)] = 1;

我正在将onTouchListener添加到一个巨大的循环中的每个按钮。网格视图的工作量与我猜想的相同。非常感谢,我现在已经做了一些不同的事情,但我认为,您的解决方案非常智能!
String indx1 = tag.substring(0, tag.indexOf("|"));
String indx2 = tag.substring(tag.indexOf("|")+1);
stat[Integer.parseInt(indx1)][Integer.parseInt(indx2)] = 1;