我有按鈕的這樣如何在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。 –