如何在我的代码中添加onClickLIstener来执行我已经实现的方法

时间:2014-02-13 14:32:27

标签: java android xml methods onclicklistener

你好这是我在堆栈溢出的第一篇文章,我是一个完整的编程初学者,所以你的帮助将不胜感激!

好的,所以我正在关注在java for Android中制作TicTacToe(noughts and crosses)游戏的教程,在教程中,该人在菜单中制作了一个重置​​游戏按钮但是我想在游戏区域下方制作一个实际的按钮。我添加了按钮(参见XML的底部),它们已经是一种重置游戏的方法,所以我需要帮助来弄清楚我在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>

2 个答案:

答案 0 :(得分:1)

如果您的按钮的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

        }
    });

答案 1 :(得分:0)

如你所示按钮说明:

<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 )方法,并使用重置游戏的代码