簡體   English   中英

如何將onClickLIstener添加到我的代碼中以執行已經實現的方法

[英]How do I add an onClickLIstener to my code to carry out a method I already have implemented

您好,這是我有史以來第一次在堆棧溢出中發表文章,並且我是編程的完整初學者,因此,非常感謝您的幫助!

好的,所以我正在學習有關在Java for Android中制作TicTacToe(正負號)游戲的教程,並且在該教程中,人員在菜單中創建了“重置游戲”按鈕,但是我想在游戲區域下方創建一個實際的按鈕。 我已經添加了按鈕(請參閱XML的底部),它們已經是重置游戲的方法,因此,我需要幫助來確定如何在Java代碼中添加代碼以及在Java中添加代碼的位置,以便在單擊按鈕之一時重置該代碼。我的游戲(清除董事會)提前謝謝!

package com.C05025.noughtsandcrosses;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;

public class NoughtsAndCrossesGame extends Activity {

private NoughtsAndCrosses Game;
private Button PASButtons[];
private TextView InfoTextView;
private TextView UserCount;
private TextView DrawCount;
private TextView AICount;
private int UserCounter = 0;
private int DrawCounter = 0;
private int AICounter = 0;
private boolean UserFirst = true;
private boolean GameOver = false;

Button button;
@Override   
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.playarea);

    PASButtons = new Button[NoughtsAndCrosses.getPLAY_AREA_SIZE()];
    PASButtons[0] = (Button) findViewById(R.id.one);
    PASButtons[1] = (Button) findViewById(R.id.two);
    PASButtons[2] = (Button) findViewById(R.id.three);
    PASButtons[3] = (Button) findViewById(R.id.four);
    PASButtons[4] = (Button) findViewById(R.id.five);
    PASButtons[5] = (Button) findViewById(R.id.six);
    PASButtons[6] = (Button) findViewById(R.id.seven);
    PASButtons[7] = (Button) findViewById(R.id.eight);
    PASButtons[8] = (Button) findViewById(R.id.nine);

    InfoTextView = (TextView) findViewById(R.id.information);
    UserCount    = (TextView) findViewById(R.id.userCount);
    DrawCount    = (TextView) findViewById(R.id.drawsCount);
    AICount      = (TextView) findViewById(R.id.computerCount);

    UserCount.setText(Integer.toString(UserCounter));
    DrawCount.setText(Integer.toString(DrawCounter));
    AICount.setText(Integer.toString(AICounter));

    Game = new NoughtsAndCrosses();  

    startNewGame();
    }

private void startNewGame() {

    Game.resetBoard();
    for (int i = 0; i < PASButtons.length; i++) {
        PASButtons[i].setText("");
        PASButtons[i].setEnabled(true);
        PASButtons[i].setOnClickListener(new ButtonClickListener(i));
        }

    if (UserFirst) {
        InfoTextView.setText(R.string.turn_user);
        UserFirst = false; 
        }

    else {
        InfoTextView.setText(R.string.turn_ai);
        int move = Game.getAIMove();
        setMove(NoughtsAndCrosses.AI, move);
        UserFirst = true;           
    }       
}

private class ButtonClickListener implements View.OnClickListener {

    int location;
    public ButtonClickListener(int location) {
    this.location = location;   
    }
    public void onClick(View view) {

        if (!GameOver) {
            if (PASButtons[location].isEnabled()) {
                setMove(NoughtsAndCrosses.USER, location);

                int winner = Game.winner_check();
                if (winner == 0){ 
                    InfoTextView.setText(R.string.turn_ai);
                    int move = Game.getAIMove();
                    setMove(NoughtsAndCrosses.AI, move);
                    winner = Game.winner_check();
                }

                if (winner == 0) InfoTextView.setText(R.string.turn_user);

                else if (winner == 1) {
                    InfoTextView.setText(R.string.result_draw);
                    DrawCounter++;
                    DrawCount.setText(Integer.toString(DrawCounter));
                    GameOver = true;
                }

                else if (winner == 2) {
                    InfoTextView.setText(R.string.result_user_wins);
                    UserCounter++;
                    UserCount.setText(Integer.toString(UserCounter));
                    GameOver = true;
                }
                else {
                    InfoTextView.setText(R.string.result_ai_wins);
                    AICounter++;
                    AICount.setText(Integer.toString(AICounter));
                    GameOver = true;
                }
            }       
        }
    }
}

private void setMove(char player, int location) {

    Game.setMove(player, location);
    PASButtons[location].setEnabled(false);
    PASButtons[location].setText(String.valueOf(player));
    if (player == NoughtsAndCrosses.USER)
        PASButtons[location].setTextColor(Color.BLUE);
    else PASButtons[location].setTextColor(Color.GREEN);

  }
}

這是我的XML:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<TableLayout
         android:id="@+id/play_grid" 
         android:layout_width="fill_parent"
         android:layout_height="wrap_content" 
         android:layout_marginTop="5sp" >

<TableRow android:gravity="center_horizontal">

<Button  android:id="@+id/one" 
         android:layout_width="200sp"
         android:layout_height="200sp" 
         android:text="@string/one"
         android:textSize="70sp" />

<Button  android:id="@+id/two" 
         android:layout_width="200sp"
         android:layout_height="200sp" 
         android:text="@string/two"
         android:textSize="70sp" />

<Button  android:id="@+id/three" 
         android:layout_width="200sp"
         android:layout_height="200sp" 
         android:text="@string/three"
         android:textSize="70sp" />
</TableRow>

<TableRow android:gravity="center_horizontal">

<Button  android:id="@+id/four" 
         android:layout_width="200sp"
         android:layout_height="200sp" 
         android:text="@string/four"
         android:textSize="70sp" />

<Button  android:id="@+id/five" 
         android:layout_width="200sp"
         android:layout_height="200sp" 
         android:text="@string/five"
         android:textSize="70sp" />

<Button  android:id="@+id/six" 
         android:layout_width="200sp"
         android:layout_height="200sp" 
         android:text="@string/six"
         android:textSize="70sp" />
</TableRow>

<TableRow  android:gravity="center_horizontal">

<Button    android:id="@+id/seven" 
           android:layout_width="200sp"
           android:layout_height="200sp" 
           android:text="@string/seven"
           android:textSize="70sp" />

<Button    android:id="@+id/eight" 
           android:layout_width="200sp"
           android:layout_height="200sp" 
           android:text="@string/eight"
           android:textSize="70sp" />

<Button    android:id="@+id/nine" 
           android:layout_width="200sp"
           android:layout_height="200sp" 
           android:text="@string/nine"
           android:textSize="70sp" />
</TableRow>

</TableLayout>

<TextView
    android:id="@+id/information"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:gravity="center_horizontal"
    android:text="@string/info"
    android:textSize="25sp" />

<TableLayout
    android:id="@+id/tableLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center" >

    <TableRow
        android:id="@+id/tableRow4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center_horizontal" >

        <TextView
            android:id="@+id/user"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/user1"
            android:gravity="center" />

        <TextView
            android:id="@+id/userCount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="25sp"
            android:gravity="center" />

        <TextView
            android:id="@+id/draws"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/draws"
            android:gravity="center" />

        <TextView
            android:id="@+id/drawsCount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="25sp"
            android:gravity="center" />

        <TextView
            android:id="@+id/computer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/computer"
            android:gravity="center" />

        <TextView
            android:id="@+id/computerCount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center" />

         </TableRow>

</TableLayout>

<Button
    android:id="@+id/newGameBtn"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/newGame" />

<Button
    android:id="@+id/mainMenuBtn"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/mainMenu" />

<Button
    android:id="@+id/exitGameBtn"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/exitGame" />
</LinearLayout>

如果按鈕的ID為btnReset,則在以下行上方的java文件中:

     AICount      = (TextView) findViewById(R.id.computerCount);

添加以下行

    Button btnReset=(Button)findViewById(R.id.btnReset);
    btnReset.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            //write the reset code here

        }
    });

如您按鈕說明中所示:

<Button
android:id="@+id/newGameBtn"
android:onClick="resetBoard"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/newGame" />

您需要在活動中添加帶有代碼的public void resetBoard( View view )方法來重置游戲

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM