如何在用户点击 android 后自动让应用点击按钮?

how to make the app click button automatically after user's click in android?

我正在制作井字游戏。我想创建一个播放器部分。

用户总是以 X 开头。

当用户点击按钮写X时,我想让设备自动在任何按钮上写O来和用户一起玩。

这是我的代码,但它不起作用,当 X 赢得比赛时,它会崩溃,但如果 O 获胜,则比赛有效:

Button a1,a2,a3,b1,b2,b3,c1,c2,c3,newGameBtn;
Button[] bArrary;
TextView thincrativTV;

// X = true ; O = false.
boolean turn = true;
int turn_count = 0;



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

    //set font path
    String fontpath = "fonts/ubuntub.ttf";
    //loading the font
    Typeface tf = Typeface.createFromAsset(getAssets(), fontpath);


    newGameBtn = (Button) findViewById(R.id.onePlayer_BNewGame);

    a1 = (Button) findViewById(R.id.onePlayer_A1);
    a2 = (Button) findViewById(R.id.onePlayer_A2);
    a3 = (Button) findViewById(R.id.onePlayer_A3);

    b1 = (Button) findViewById(R.id.onePlayer_B1);
    b2 = (Button) findViewById(R.id.onePlayer_B2);
    b3 = (Button) findViewById(R.id.onePlayer_B3);

    c1 = (Button) findViewById(R.id.onePlayer_C1);
    c2 = (Button) findViewById(R.id.onePlayer_C2);
    c3 = (Button) findViewById(R.id.onePlayer_C3);

    bArrary = new Button[]{a1,a2,a3,b1,b2,b3,c1,c2,c3};

    for (Button b : bArrary){
        b.setOnClickListener(this);
        //set custom font to all buttons
        b.setTypeface(tf);
    }

    newGameBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // we need to reset turn and turn_count and re enable all buttons
            turn = true;
            turn_count = 0;
            enableDisableAllButtons(true);
        }
    });

    // to make the copyrights work
    thincrativTV = (TextView) findViewById(R.id.onePlayer_TextView);
    if (thincrativTV !=null) {
        thincrativTV.setMovementMethod(LinkMovementMethod.getInstance());
    }

}



@Override
public void onClick(View v) {
    Button b = (Button) v;
    buttonClicked(b);

    Handler myHandler = new Handler();

    myHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            randomClick();
        }
    },500);
    //randomClick();

}



public void randomClick(){
    Random rand = new Random();
    if (turn_count<9) {
        Button nextB = bArrary[rand.nextInt(bArrary.length)];
        while (!nextB.isClickable()) {
            nextB = bArrary[rand.nextInt(bArrary.length)];
        }
        buttonClicked(nextB);
    }
}



public void buttonClicked(Button b){

    // i just wanna change the text on the button to X/O.
    if (turn){
        // X's turn
        b.setText("X");
    }else {
        // O's turn
        b.setText("O");
    }

    turn_count++;

    // and change the turn
    b.setBackgroundColor(Color.LTGRAY);
    b.setClickable(false);
    turn = !turn;

    checkForWinner();
}



private void checkForWinner(){

    boolean there_is_a_winner = false;

    // first we check the horizontals.
    if (a1.getText() == a2.getText() && a2.getText() == a3.getText() && !a1.isClickable()){
        there_is_a_winner = true;
    }else if (b1.getText() == b2.getText() && b2.getText() == b3.getText() && !b1.isClickable()){
        there_is_a_winner = true;
    }else if (c1.getText() == c2.getText() && c2.getText() == c3.getText() && !c1.isClickable()){
        there_is_a_winner = true;
    }

    // second we check the verticals.
    if (a1.getText() == b1.getText() && b1.getText() == c1.getText() && !a1.isClickable()){
        there_is_a_winner = true;
    }else if (a2.getText() == b2.getText() && b2.getText() == c2.getText() && !b2.isClickable()){
        there_is_a_winner = true;
    }else if (a3.getText() == b3.getText() && b3.getText() == c3.getText() && !c3.isClickable()){
        there_is_a_winner = true;
    }

    // finally we check the diagonal.
    if (a1.getText() == b2.getText() && b2.getText() == c3.getText() && !a1.isClickable()){
        there_is_a_winner = true;
    }else if (a3.getText() == b2.getText() && b2.getText() == c1.getText() && !b2.isClickable()){
        there_is_a_winner = true;
    }


    if (there_is_a_winner) {
        if (!turn){
            toast("X wins");
        }else {
            toast("O wins");
        }

        enableDisableAllButtons(false);
    } else if (turn_count == 9){
        // when all the buttons are clicked and disabled.
        toast("Oops!, Play Again");
    }
}



private void enableDisableAllButtons(boolean enable){

    // false to disable
    for (Button b : bArrary){
        b.setClickable(enable);

        //this is for color.
        if (enable){
            b.setBackgroundColor(Color.parseColor("#3c94d3"));
            //set the text back to original blank
            b.setText("");
        }else {
            b.setBackgroundColor(Color.LTGRAY);
        }
    }
}



private void toast(String message){
    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();

}

*注:X是用户,O是设备。

我看到方法 buttonClicked(Button b) 仅在用户单击任何按钮时调用。轮到用户了,而不是机器。 因此,在轮到用户处理后,您可以选择随机按钮并用它调用 buttonClicked()。

@Override
public void onClick(View v) {
    Button b = (Button) v;
    buttonClicked(b);

    // now you choose random button nextB 
    // -> call buttonClicked(nextB);
    Random rand = new Random();
    // machine only can set "O" when the map has any button  (when turn_count < 9)
    if (turn_count < 9) {
        Button nextB = bArrary[rand.nextInt(bArrary.length)];
        while (!nextB.isClickable()) {
            nextB = bArrary[rand.nextInt(bArrary.length)];
        }
        buttonClicked(nextB);
    }
}

在方法 randomClicks(Button b) 上,我们将调用 b.setText("0")。

public void randomClicks(Button b){
    b.setText("O");
}

我认为它会起作用。