更改屏幕方向时如何禁用按钮

时间:2019-02-17 17:57:38

标签: java android orientation getter-setter

我正在为自己的学习任务编写一个简单的Tic Tac Toe android应用程序。它有9个按钮,单击时会显示X或O,具体取决于转动的人。游戏结束时,所有按钮都将禁用,直到用户按下重置按钮为止。

我的难题:当我更改为横向模式时,这些按钮会启用,该如何解决?

我已经尝试使用getter-setter修复此问题,但是似乎没有getter-setter可以启用/禁用设置

当GameState指示玩家获胜/有平局时,此方法运行。

public void GameEnder(GameState state){
    TextView txt1;
    Button btn1;
    Button btn2;
    Button btn3;
    Button btn4;
    Button btn5;
    Button btn6;
    Button btn7;
    Button btn8;
    Button btn9;
    txt1 = (TextView)findViewById(R.id.textView);
    btn1 = findViewById(R.id.button1);
    btn2 = findViewById(R.id.button2);
    btn3 = findViewById(R.id.button3);
    btn4 = findViewById(R.id.button4);
    btn5 = findViewById(R.id.button5);
    btn6 = findViewById(R.id.button6);
    btn7 = findViewById(R.id.button7);
    btn8 = findViewById(R.id.button8);
    btn9 = findViewById(R.id.button9);
    switch(state){
        case PLAYER_ONE:
            txt1.setText("Cross wins!");
            txt1.setVisibility(View.VISIBLE);
            btn1.setEnabled(false);
            btn2.setEnabled(false);
            btn3.setEnabled(false);
            btn4.setEnabled(false);
            btn5.setEnabled(false);
            btn6.setEnabled(false);
            btn7.setEnabled(false);
            btn8.setEnabled(false);
            btn9.setEnabled(false);

            break;
        case PLAYER_TWO:
            txt1.setText("Circle wins!");
            txt1.setVisibility(View.VISIBLE);
            btn1.setEnabled(false);
            btn2.setEnabled(false);
            btn3.setEnabled(false);
            btn4.setEnabled(false);
            btn5.setEnabled(false);
            btn6.setEnabled(false);
            btn7.setEnabled(false);
            btn8.setEnabled(false);
            btn9.setEnabled(false);

            break;
        case DRAW:
            txt1.setText("It's a draw!");
            txt1.setVisibility(View.VISIBLE);
            btn1.setEnabled(false);
            btn2.setEnabled(false);
            btn3.setEnabled(false);
            btn4.setEnabled(false);
            btn5.setEnabled(false);
            btn6.setEnabled(false);
            btn7.setEnabled(false);
            btn8.setEnabled(false);
            btn9.setEnabled(false);
            break;
        case IN_PROGRESS:
            break;



    }
}

我正在寻找一种可以保存按钮的启用/禁用状态的吸气剂(如果甚至存在类似的东西)

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    //serialization of game
    outState.putSerializable("Game1", game); 
    //save state of visibility of X or O, key:value pair
    outState.putCharSequence("btn1", btn1.getText());
    outState.putCharSequence("btn2", btn2.getText());
    outState.putCharSequence("btn3", btn3.getText());
    outState.putCharSequence("btn4", btn4.getText());
    outState.putCharSequence("btn5", btn5.getText());
    outState.putCharSequence("btn6", btn6.getText());
    outState.putCharSequence("btn7", btn7.getText());
    outState.putCharSequence("btn8", btn8.getText());
    outState.putCharSequence("btn9", btn9.getText());
    outState.putInt("txt1", txt1.getVisibility());

}

@Override
protected void onRestoreInstanceState(Bundle inState) {
    super.onRestoreInstanceState(inState);
    inState.getSerializable("Game1");
    //summon value of image with key
    btn1.setText(inState.getCharSequence("btn1"));
    btn2.setText(inState.getCharSequence("btn2"));
    btn3.setText(inState.getCharSequence("btn3"));
    btn4.setText(inState.getCharSequence("btn4"));
    btn5.setText(inState.getCharSequence("btn5"));
    btn6.setText(inState.getCharSequence("btn6"));
    btn7.setText(inState.getCharSequence("btn7"));
    btn8.setText(inState.getCharSequence("btn8"));
    btn9.setText(inState.getCharSequence("btn9"));
    txt1.setVisibility(inState.getInt("txt1"));

    if(game.won() == GameState.PLAYER_ONE || game.won() == GameState.PLAYER_TWO){
        btn1.setEnabled(false);
        btn2.setEnabled(false);
        btn3.setEnabled(false);
        btn4.setEnabled(false);
        btn5.setEnabled(false);
        btn6.setEnabled(false);
        btn7.setEnabled(false);
        btn8.setEnabled(false);
        btn9.setEnabled(false);
    }

}

我希望这些按钮保持禁用状态,但是在更改方向时它们会启用。

编辑:我添加了一些代码,感谢您指出

这可以处理用户单击按钮的情况

public void tileClicked(View view) {
    int id = view.getId();
    TileState state = null;
    switch(view.getId()){
        case R.id.button1:
            state = game.choose(0, 0);
            break;
        case R.id.button2:
            state = game.choose(0,1);
            break;
        case R.id.button3:
            state = game.choose(0, 2);
            break;
        case R.id.button4:
            state = game.choose(1,0);
            break;
        case R.id.button5:
            state = game.choose(1,1);
            break;
        case R.id.button6:
            state = game.choose(1,2);
            break;
        case R.id.button7:
            state = game.choose(2,0);
            break;
        case R.id.button8:
            state = game.choose(2,1);
            break;
        case R.id.button9:
            state = game.choose(2,2);
            break;

    }
    game.won();
    GameEnder(game.won());

    buttonUpdater(state, id);

}

我添加了“游戏”类,包括序列化。这段代码还将检查tilestate以及是否有赢家

import java.io.Serializable;

public class Game implements Serializable{

final private int BOARD_SIZE = 3;
private TileState[][] board;
private Boolean playerOneTurn;  // true if player 1's turn, false if player 
2's turn
private int movesPlayed = 0;
public Boolean gameOver;




public Game() {
    board = new TileState[BOARD_SIZE][BOARD_SIZE];
    for (int i = 0; i < BOARD_SIZE; i++)
        for (int j = 0; j < BOARD_SIZE; j++)
            board[i][j] = TileState.BLANK;

    playerOneTurn = true;
    gameOver = false;
}


public TileState getTileState(int row, int column){
    return this.board[row][column];
}

public TileState choose(int row, int column) {
    if (this.getTileState(row, column) == TileState.BLANK){
        if (playerOneTurn){
            this.board[row][column] = TileState.CROSS;
            playerOneTurn = false;
            movesPlayed ++;
            return TileState.CROSS;
        }
        else{
            this.board[row][column] = TileState.CIRCLE;
            playerOneTurn = true;
            movesPlayed ++;
            return TileState.CIRCLE;
        }
    }
    else{
        return TileState.INVALID;
    }


}

public GameState won(){
    //check all rows
    if(movesPlayed >= 9){
        return GameState.DRAW;
    }
    else if(getTileState(0, 0) == getTileState(0,1) && getTileState(0,2) == getTileState(0,1) && getTileState(0,1) != TileState.BLANK){
        if(!playerOneTurn){

            return GameState.PLAYER_ONE;
        }
        else {
            return GameState.PLAYER_TWO;
        }
    }
    else if(getTileState(1,0) == getTileState(1,1) && getTileState(1,2) == getTileState(1,1)&& getTileState(1,0) != TileState.BLANK){
        if(!playerOneTurn){
            return GameState.PLAYER_ONE;
        }
        else {
            return GameState.PLAYER_TWO;
        }
    }
    else if(getTileState(2, 0) == getTileState(2,1) && getTileState(2,2) == getTileState(2,1)&& getTileState(2,0) != TileState.BLANK){
        if(!playerOneTurn){
            return GameState.PLAYER_ONE;
        }
        else {
            return GameState.PLAYER_TWO;
        }
    }
    //check all columns
    else if(getTileState(0, 0) == getTileState(1,0) && getTileState(2,0) == getTileState(1,0)&& getTileState(0,0) != TileState.BLANK){
        if(!playerOneTurn){
            return GameState.PLAYER_ONE;
        }
        else {
            return GameState.PLAYER_TWO;
        }
    }
    else if(getTileState(0, 1) == getTileState(1,1) && getTileState(2,1) == getTileState(1,1)&& getTileState(0,1) != TileState.BLANK){
        if(!playerOneTurn){
            return GameState.PLAYER_ONE;
        }
        else {

            return GameState.PLAYER_TWO;
        }
    }
    else if(getTileState(0, 2) == getTileState(1,2) && getTileState(2,2) == getTileState(1,2)&& getTileState(0,2) != TileState.BLANK){
        if(!playerOneTurn){
            return GameState.PLAYER_ONE;
        }
        else {
            return GameState.PLAYER_TWO;
        }
    }
    // check voor schuine rij
    else if(getTileState(0, 0) == getTileState(1,1) && getTileState(2,2) == getTileState(1,1)&& getTileState(0,0) != TileState.BLANK){
        if(!playerOneTurn){

            return GameState.PLAYER_ONE;
        }
        else {
            return GameState.PLAYER_TWO;
        }
    }
    //check voor andere schuine rij
    else if(getTileState(2, 0) == getTileState(1,1) && getTileState(0,2) == getTileState(1,1)&& getTileState(2,0) != TileState.BLANK){
        if(!playerOneTurn){
            return GameState.PLAYER_ONE;
        }
        else {
            return GameState.PLAYER_TWO;
        }
    }
    else{
        return GameState.IN_PROGRESS;
    }


}

编辑:添加了枚举GameState

package com.example.mainactivity;

public enum GameState {
    IN_PROGRESS,
    PLAYER_ONE,
    PLAYER_TWO,
    DRAW;
}

0 个答案:

没有答案