我有按钮的这样如何在2d按钮数组上设置onClickListener?
b[1][3] = (Button) findViewById(R.id.uno);
b[1][2] = (Button) findViewById(R.id.due);
b[1][1] = (Button) findViewById(R.id.tre);
b[2][3] = (Button) findViewById(R.id.quattro);
b[2][2] = (Button) findViewById(R.id.cinque);
b[2][1] = (Button) findViewById(R.id.sei);
b[3][3] = (Button) findViewById(R.id.sette);
b[3][2] = (Button) findViewById(R.id.otto);
b[3][1] = (Button) findViewById(R.id.nove);
一个二维数组,我想要得到的东西像
b[x][y].setText("X");
我怎么可以从功能onClickListener
的x
和y
?
地图/对/元组的SparseArray .. 。首先在循环中构建它像'sparseArray.append(buttonID,new Pair(x,y))'...然后在onClick中获得'Pair p = sparseArray.get(view.getId())' – Selvin
为什么你无论如何需要索引?如果您可以直接获取按钮ID。 –