如何在第一次和第二次觸摸時執行不同的事件(TicTacToe for Android) (how to do different event on first and second touch (TicTacToe for Android))


問題描述

如何在第一次和第二次觸摸時執行不同的事件(TicTacToe for Android) (how to do different event on first and second touch (TicTacToe for Android))

我是在 android 中創建應用程序的初學者,但作為我的第一個項目,我正在嘗試構建一個井字遊戲。但我嘗試在第一次觸摸 Button 並設置 O 時設置 X 已經很長時間了在第二次觸摸。我不知道如何解決這個問題。我認為它應該像我跟隨的那樣工作,但它不是......!這是我的源代碼:

public class MainActivity extends Activity implements OnClickListener{

private Button b1,b2,b3,b4,b5,b6,b7,b8,b9;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    b1=(Button) findViewById(R.id.button1);
    b1.setOnClickListener(this);

    b2=(Button) findViewById(R.id.button2);
    b2.setOnClickListener(this);

    b3=(Button) findViewById(R.id.button3);
    b3.setOnClickListener(this);

    b4=(Button) findViewById(R.id.button6);
    b4.setOnClickListener(this);

    b5=(Button) findViewById(R.id.button10);
    b5.setOnClickListener(this);

    b6=(Button) findViewById(R.id.button11);
    b6.setOnClickListener(this);

    b7=(Button) findViewById(R.id.button7);
    b7.setOnClickListener(this);

    b8=(Button) findViewById(R.id.button8);
    b8.setOnClickListener(this);

    b9=(Button) findViewById(R.id.button5);
    b9.setOnClickListener(this);
}

public void onClick(View v)
{
    boolean press=true;
    int i=0;
    while(i<9){     
            if(press==true)
            {
                if((Button)v==b1)
                {
                    b1.setText("X");
                    press=false;
                }

                else if((Button)v==b2)
                {
                    b2.setText("X");
                    press=false;
                }

                else if((Button)v==b3)
                {
                    b3.setText("X");
                    press=false;
                }

            }

            if(press==false)
            {
                if((Button)v==b1)
                {
                    b1.setText("O");
                    press=true;
                }
                else if((Button)v==b2)
                {
                    b2.setText("O");
                    press=true;
                }
                else if((Button)v==b3)
                {
                    b3.setText("O");
                    press=true;
                }
            }
            i++;
    }
}

參考解法

方法 1:

onClick firstly you set boolean press=true; so in while loop it always take you into the first if loop

Declare boolean press=true; as a global variable

(by KurdAbhijeet)

參考文件

  1. how to do different event on first and second touch (TicTacToe for Android) (CC BY‑SA 2.5/3.0/4.0)

#tic-tac-toe #Android






相關問題

為什麼我的遞歸沒有返回但最終導致堆棧溢出? (Why does my recursion not return but end up in a stack overflow?)

重新開始遊戲 (Restarting a game)

沒有人工智能的井字遊戲 (Tic-Tac-Toe without AI)

Tictactoe 遊戲意外結束輸入期待 IF MySQL (Tictactoe game Unexpected end of input expecting IF MySQL)

如何在第一次和第二次觸摸時執行不同的事件(TicTacToe for Android) (how to do different event on first and second touch (TicTacToe for Android))

TicTacToe Python 檢查獲勝者 (TicTacToe Python Check for Winner)

使用 Java 可定制的井字遊戲板 (Customizable TicTacToe game board with Java)

我的井字遊戲理論上是個大問題,但對我來說一切似乎都很好 (Theoretically big problem with my Tic tac toe game , but for me all seems good)

遊戲線程問題 (Issues with threads in game)

我在使用 Turtle 圖形檢查井字遊戲中的獲勝者時遇到了一些問題 (I am having some problems checking the winner in a tic tac toe game using Turtle graphics)

為什麼我選擇第一個位置後立即收到勝利信息 (Why do i get the victory message right after choosing the first position)

如何修復此井字遊戲的未捕獲參考錯誤 (How do I fix an uncaught reference error for this TicTacToe)







留言討論