无法在 onClick 方法中修改变量
Can't modify variable inside onClick method
所以,我创建了一个 java class 来实现 onClickListener
并且在这个 class 里面我写了 onClick
public方法。在这个方法之外,我创建了一个 int 对象,我想在 onClick
方法中修改这个对象。我通过检查其他类似的 SO 问题进行了很多研究,并且尝试了很多事情,比如将对象创建为 public int,或将其设为 private int 并使用另一种方法对其进行更改,然后在 onClick 中调用此方法。但是,似乎没有任何效果。
下面显示的代码将 int 对象创建为 private int 并命名为 turn
。要在 onClick
中更改它,我首先创建了一个名为 changeTurn
的 public 方法来修改它,然后我在 onClick
中调用此方法。
public class TicTacToe implements View.OnClickListener {
Button buttons[] = new Button[9];
TextView result;
public TicTacToe(Button[] buttonList, TextView text) {
buttons = buttonList;
result = text;
}
//public void
private int turn = 1; // The object that needs to be modified in onCLick
@Override
public void onClick(View v) {
Button b = (Button) v;
if((((Button) v).getText() != "X") && (((Button) v).getText() != "O")) {
if(this.turn == 1) {
b.setText("X");
changeTurn(); // ***Should change the value of turn***
result.setText("Turn is: " + this.turn);
}
if(this.turn == 2) {
b.setText("O");
changeTurn(); // ***Should change the value of turn***
result.setText("Turn is: " + turn);
}
}
}
public void changeTurn() {
if(this.turn == 1) {
this.turn = 2;
}
if(this.turn == 2) {
this.turn = 1;
}
}
}
根据我的尝试,程序仅在第一个 if 内运行,每次我单击我的 9 个按钮中的任何一个,其 setOnClickListeners 连接到此 onClick 方法。另外,我打印出来的时候turn的值一直是1,也就是说它的值没有被onClick方法里面的changeTurn改变。
有关该应用程序的一般信息:我正在尝试在具有 9 个按钮的 3x3 网格中制作井字游戏。由于会有 2 名玩家,我试图使用这个 turn 整数来跟踪轮到谁按下按钮。如果 turn 为 1,则按钮的文本变为 X,如果 turn 为 2,则变为 O。现在,每次我按下按钮,它总是变为 X。
如果有任何帮助或想法,我将不胜感激。
尝试这样使用它
final private int[] turn = {0}
然后将代码更改为
if(turn[0] == 1) {
b.setText("X");
turn[0]=2; // ***Should change the value of turn***
result.setText("Turn is: " + turn);
}
if(turn[0] == 2) {
b.setText("O");
turn[0]=1; // ***Should change the value of turn***
result.setText("Turn is: " + turn);
}
您正在将转弯设置为 2,然后立即将其设置回 1。
// turn == 1
if(this.turn == 1) { // true
this.turn = 2; // turn == 2
}
if(this.turn == 2) { // now true!
this.turn = 1; // turn == 1
}
最简单的做法是,如果跳过第一个块,则只进入第二个块,即:
if(this.turn == 1) {
this.turn = 2;
} else if(this.turn == 2) {
this.turn = 1;
}
或者,如果您希望用更多回合数扩展方块,请使用开关:
switch(this.turn) {
case 1:
this.turn = 2;
break;
case 2:
this.turn = 1;
break;
}
switch 的唯一问题是,如果您忘记了 break 语句,您最终会遇到不可预知的混乱。
最后,一点小建议:如果你想创建一个数字循环(1 .. n 然后回到 1),那么你应该考虑像 x = x % n + 1;
这样的模数运算符 (%)
所以,我创建了一个 java class 来实现 onClickListener
并且在这个 class 里面我写了 onClick
public方法。在这个方法之外,我创建了一个 int 对象,我想在 onClick
方法中修改这个对象。我通过检查其他类似的 SO 问题进行了很多研究,并且尝试了很多事情,比如将对象创建为 public int,或将其设为 private int 并使用另一种方法对其进行更改,然后在 onClick 中调用此方法。但是,似乎没有任何效果。
下面显示的代码将 int 对象创建为 private int 并命名为 turn
。要在 onClick
中更改它,我首先创建了一个名为 changeTurn
的 public 方法来修改它,然后我在 onClick
中调用此方法。
public class TicTacToe implements View.OnClickListener {
Button buttons[] = new Button[9];
TextView result;
public TicTacToe(Button[] buttonList, TextView text) {
buttons = buttonList;
result = text;
}
//public void
private int turn = 1; // The object that needs to be modified in onCLick
@Override
public void onClick(View v) {
Button b = (Button) v;
if((((Button) v).getText() != "X") && (((Button) v).getText() != "O")) {
if(this.turn == 1) {
b.setText("X");
changeTurn(); // ***Should change the value of turn***
result.setText("Turn is: " + this.turn);
}
if(this.turn == 2) {
b.setText("O");
changeTurn(); // ***Should change the value of turn***
result.setText("Turn is: " + turn);
}
}
}
public void changeTurn() {
if(this.turn == 1) {
this.turn = 2;
}
if(this.turn == 2) {
this.turn = 1;
}
}
}
根据我的尝试,程序仅在第一个 if 内运行,每次我单击我的 9 个按钮中的任何一个,其 setOnClickListeners 连接到此 onClick 方法。另外,我打印出来的时候turn的值一直是1,也就是说它的值没有被onClick方法里面的changeTurn改变。
有关该应用程序的一般信息:我正在尝试在具有 9 个按钮的 3x3 网格中制作井字游戏。由于会有 2 名玩家,我试图使用这个 turn 整数来跟踪轮到谁按下按钮。如果 turn 为 1,则按钮的文本变为 X,如果 turn 为 2,则变为 O。现在,每次我按下按钮,它总是变为 X。
如果有任何帮助或想法,我将不胜感激。
尝试这样使用它
final private int[] turn = {0}
然后将代码更改为
if(turn[0] == 1) {
b.setText("X");
turn[0]=2; // ***Should change the value of turn***
result.setText("Turn is: " + turn);
}
if(turn[0] == 2) {
b.setText("O");
turn[0]=1; // ***Should change the value of turn***
result.setText("Turn is: " + turn);
}
您正在将转弯设置为 2,然后立即将其设置回 1。
// turn == 1
if(this.turn == 1) { // true
this.turn = 2; // turn == 2
}
if(this.turn == 2) { // now true!
this.turn = 1; // turn == 1
}
最简单的做法是,如果跳过第一个块,则只进入第二个块,即:
if(this.turn == 1) {
this.turn = 2;
} else if(this.turn == 2) {
this.turn = 1;
}
或者,如果您希望用更多回合数扩展方块,请使用开关:
switch(this.turn) {
case 1:
this.turn = 2;
break;
case 2:
this.turn = 1;
break;
}
switch 的唯一问题是,如果您忘记了 break 语句,您最终会遇到不可预知的混乱。
最后,一点小建议:如果你想创建一个数字循环(1 .. n 然后回到 1),那么你应该考虑像 x = x % n + 1;
这样的模数运算符 (%)