【问题标题】:Can't modify variable inside onClick method无法在 onClick 方法中修改变量
【发布时间】:2016-02-21 20:06:09
【问题描述】:

所以,我创建了一个 java 类来实现 onClickListener,并在这个类中编写了 onClick 公共方法。在这个方法之外,我创建了一个 int 对象,我想在 onClick 方法中修改这个对象。我通过检查其他类似的 SO 问题进行了很多研究,并尝试了很多方法,例如将对象创建为 public int,或将其设为 private int 并有另一种方法来更改它,然后在 onClick 中调用此方法。但是,似乎没有任何效果。

下面显示的代码将 int 对象创建为 private int 并命名为 turn。为了在onClick 中更改它,我首先创建了一个名为changeTurn 的公共方法来修改它,然后我在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;
        }
    }
}

根据我的尝试,每次单击我的 9 个按钮中的任何一个时,程序只会进入第一个 if 内,这些按钮的 setOnClickListeners 连接到这个 onClick 方法。另外,turn的值在我打印出来的时候一直是1,基本上就是onClick方法里面的changeTurn没有改变它的值。

关于应用程序的一般信息:我正在尝试在 3x3 网格中制作一个带有 9 个按钮的井字游戏。由于会有 2 名玩家,我试图使用这个 turn 整数来跟踪轮到谁按下按钮。如果 turn 为 1,则按钮的文本变为 X,如果 turn 为 2,则变为 O。现在,每次我按下按钮时,它总是变为 X。

如果有任何帮助或想法,我将不胜感激。

【问题讨论】:

  • 嘿@Klaus,你能告诉我你如何为你的按钮设置点击监听器吗?
  • 我这样做的方法是创建一个井字游戏对象,然后将其传递给主服务器上的 setOnClickListener()。所以像这样:井字游戏 tic = new TicTacToe(buttons, text);按钮一 = (Button) findViewById(R.id.button1); one.setOnClickListener(tic);

标签: java android onclick android-button


【解决方案1】:

您将转弯设置为 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:

switch(this.turn) {
    case 1:
        this.turn = 2;
        break;
    case 2:
        this.turn = 1;
        break;
}

switch 唯一的问题是如果你忘记了一个 break 语句,你最终会遇到不可预知的混乱。

最后,一个快速的建议:如果您尝试创建一个数字循环(1 .. n 然后返回 1),那么您应该考虑模数运算符 (%),例如 x = x % n + 1;

【讨论】:

  • 是的,这是一个很好的收获。实际上我也必须对另外两个 if 语句做同样的事情,所以让它们 if 和 else if。感谢您的帮助。
【解决方案2】:

尝试像这样使用它

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);
    }

【讨论】:

  • 我刚试了一下,好像不行。当我现在单击任何按钮时,它根本不会改变。我认为它实际上并没有进入两个 if 语句中的任何一个。我还尝试在 onClick 方法之外设置 turn[0] = 1(以对其进行初始化),但它给了我一个错误,说“意外的令牌”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-09
  • 1970-01-01
  • 1970-01-01
  • 2012-09-15
  • 1970-01-01
  • 2011-08-06
相关资源
最近更新 更多