【发布时间】:2015-01-21 12:59:11
【问题描述】:
我正在尝试制作一个两人井字游戏。当第一个玩家单击按钮上带有标记 X 的 OnClick 方法时,但我卡住了 - 我不知道如何使 onClick() 检测第二个玩家何时单击以及如何在按钮上标记 O ..plz 帮助..我的.java在下面
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setBoard();
}
int c[][];
int i, j, k = 0;
Button b[][];
TextView textView;
private void setBoard() {
b = new Button[4][4];
c = new int[4][4];
// Button = (TextView) findViewById(R.id.newgame);
b[1][3] = (Button) findViewById(R.id.one);
b[1][2] = (Button) findViewById(R.id.two);
b[1][1] = (Button) findViewById(R.id.three);
b[2][3] = (Button) findViewById(R.id.four);
b[2][2] = (Button) findViewById(R.id.five);
b[2][1] = (Button) findViewById(R.id.six);
b[3][3] = (Button) findViewById(R.id.seven);
b[3][2] = (Button) findViewById(R.id.eight);
b[3][1] = (Button) findViewById(R.id.nine);
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 3; j++)
c[i][j] = 2;
}
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 3; j++) {
b[i][j].setOnClickListener(new MyClickListener(i, j));
if (!b[i][j].isEnabled()) {
b[i][j].setText("o");
b[i][j].setEnabled(true);
}
}
}
}
class MyClickListener implements View.OnClickListener {
int x;
int y;
public MyClickListener(int x, int y) {
this.x = x;
this.y = y;
}
public void onClick(View view) {
if (b[x][y].isEnabled()) {
b[x][y].setEnabled(false);
b[x][y].setText("X");
c[x][y] = 0;
textView.setText("");
//WHAT NEXT
}
}
}
}
}
【问题讨论】:
标签: java android android-activity