为什么我的ButtonClickListerner不工作?

时间:2014-11-14 22:05:47

标签: java android eclipse

所以我按照了如何制作TicTacToeGame的教程,但我想从自己添加一些东西:重启(十)和退出(十一)按钮(在我的代码中它们被称为十和十一)。所以我在课堂上添加了那些" ButtonClickListener"但他们没有工作。

我的MainActivity.java:

package com.wouter.testjk;


import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.wouter.testjk.R;


public class MainActivity extends Activity implements OnClickListener{

    private TicTacToeGame mGame;

    private Button mBoardButtons[];

    private TextView mInfoTextView;
    private TextView mHumanCount;
    private TextView mTieCount;
    private TextView mAndroidCount;

    private int mHumanCounter = 0;
    private int mTieCounter = 0;
    private int mAndroidCounter = 0;

    private boolean mHumanFirst = true;
    private boolean mGameOver = false;




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

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






        Button mTen = (Button) findViewById(R.id.ten);
        mTen.setOnClickListener(this);
        Button mEleven = (Button) findViewById(R.id.eleven);
        mEleven.setOnClickListener(this);



        mInfoTextView = (TextView) findViewById(R.id.information);
        mHumanCount = (TextView) findViewById(R.id.humancount);
        mTieCount = (TextView) findViewById(R.id.tiesCount);
        mAndroidCount = (TextView) findViewById(R.id.androidCount);

        mHumanCount.setText(Integer.toString(mHumanCounter));
        mTieCount.setText(Integer.toString(mTieCounter));
        mHumanCount.setText(Integer.toString(mAndroidCounter));

        mGame = new TicTacToeGame();

        startNewGame();


    }


    private void startNewGame()
    {
        mGame.clearBoard();

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

        }
        if (mHumanFirst)
        {
            mInfoTextView.setText(R.string.first_human);
            mHumanFirst = false;
        }
        else
        {
            mInfoTextView.setText(R.string.turn_computer);
            int move = mGame.getComputerMove();
            setMove(mGame.ANDROID_PLAYER, move);
            mHumanFirst = true;
        }
    }

    private class ButtonClickListener implements View.OnClickListener
    {
        int location;



        public ButtonClickListener(int location)
        {
            this.location = location;
        }

    }



    private void setMove(char player, int location)
    {
        mGame.setMove(player,location);
        mBoardButtons[location].setEnabled(false);
        mBoardButtons[location].setText(String.valueOf(player));
        if (player == mGame.HUMAN_PLAYER)
            mBoardButtons[location].setTextColor(Color.GREEN);
        else
        {
            mBoardButtons[location].setTextColor(Color.RED);
        }
    }


    @Override
    public void onClick(View v) {

        switch (view.getId())
        {
        case R.id.ten:

            startNewGame();
            return;

        case R.id.eleven:

            MainActivity.this.finish();
            return;

        }


        if (!mGameOver)
        {
            if(mBoardButtons[location].isEnabled())
            {
                setMove(mGame.HUMAN_PLAYER, location);

                int winner = mGame.checkForWinner();

                if (winner == 0)
                {
                    mInfoTextView.setText(R.string.turn_computer);
                    int move = mGame.getComputerMove();
                    setMove(mGame.ANDROID_PLAYER, move);
                    winner = mGame.checkForWinner();

                }
                if (winner == 0)
                        mInfoTextView.setText(R.string.turn_human);
                else if (winner == 1)
                {
                    mInfoTextView.setText(R.string.result_tie);
                    mTieCounter++;
                    mTieCount.setText(Integer.toString(mTieCounter));
                    mGameOver = true;
                }       
                else if (winner ==2)
                {
                    mInfoTextView.setText(R.string.result_human_wins);
                    mHumanCounter++;
                    mHumanCount.setText(Integer.toString(mHumanCounter));
                    mGameOver = true;
                }
                else if (winner ==3)
                {
                    mInfoTextView.setText(R.string.result_android_wins);
                    mAndroidCounter++;
                    mAndroidCount.setText(Integer.toString(mAndroidCounter));
                    mGameOver = true;
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您已指定MainAcivity实现OnClickListener。这意味着您的代码需要实现OnClickListener中指定的方法,这些方法是在代码末尾使用以下代码块完成的;

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}

但正如您所看到的,您实际上并没有在此代码块中执行任何操作。

您还创建了一个名为ButtonClickListener的私有类,它也实现了OnClickListener,但是当您在按钮上使用setOnClickListener方法时,您不会分配ButtonClickListener的实例。您所做的就是分配this这是您的MainActivity类,然后将其转换为OnClickListener,由于MainActivity已经实现了OnClickListener,因此它也是多余的。


修改(请参阅评论)这是重新排列的代码。

package com.wouter.testjk;


import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.wouter.testjk.R;


public class MainActivity extends Activity implements OnClickListener{

    private TicTacToeGame mGame;

    private Button mBoardButtons[];

    private TextView mInfoTextView;
    private TextView mHumanCount;
    private TextView mTieCount;
    private TextView mAndroidCount;

    private int mHumanCounter = 0;
    private int mTieCounter = 0;
    private int mAndroidCounter = 0;

    private boolean mHumanFirst = true;
    private boolean mGameOver = false;

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

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

        Button mTen = (Button) findViewById(R.id.ten);
        mTen.setOnClickListener(this);
        Button mEleven = (Button) findViewById(R.id.eleven);
        mEleven.setOnClickListener(this);



        mInfoTextView = (TextView) findViewById(R.id.information);
        mHumanCount = (TextView) findViewById(R.id.humancount);
        mTieCount = (TextView) findViewById(R.id.tiesCount);
        mAndroidCount = (TextView) findViewById(R.id.androidCount);

        mHumanCount.setText(Integer.toString(mHumanCounter));
        mTieCount.setText(Integer.toString(mTieCounter));
        mHumanCount.setText(Integer.toString(mAndroidCounter));

        mGame = new TicTacToeGame();

        startNewGame();

    }

    private void startNewGame()
    {
        mGame.clearBoard();

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

        }
        if (mHumanFirst)
        {
            mInfoTextView.setText(R.string.first_human);
            mHumanFirst = false;
        }
        else
        {
            mInfoTextView.setText(R.string.turn_computer);
            int move = mGame.getComputerMove();
            setMove(mGame.ANDROID_PLAYER, move);
            mHumanFirst = true;
        }
    }

    private class ButtonClickListener implements View.OnClickListener
    {
        int location;

        public ButtonClickListener(int location)
        {
            this.location = location;
        }

        @Override
        public void onClick(View v) {
            if (!mGameOver)
            {
                if(mBoardButtons[location].isEnabled())
                {
                    setMove(mGame.HUMAN_PLAYER, location);

                    int winner = mGame.checkForWinner();

                    if (winner == 0)
                    {
                        mInfoTextView.setText(R.string.turn_computer);
                        int move = mGame.getComputerMove();
                        setMove(mGame.ANDROID_PLAYER, move);
                        winner = mGame.checkForWinner();

                    }
                    if (winner == 0)
                            mInfoTextView.setText(R.string.turn_human);
                    else if (winner == 1)
                    {
                        mInfoTextView.setText(R.string.result_tie);
                        mTieCounter++;
                        mTieCount.setText(Integer.toString(mTieCounter));
                        mGameOver = true;
                    }       
                    else if (winner ==2)
                    {
                        mInfoTextView.setText(R.string.result_human_wins);
                        mHumanCounter++;
                        mHumanCount.setText(Integer.toString(mHumanCounter));
                        mGameOver = true;
                    }
                    else if (winner ==3)
                    {
                        mInfoTextView.setText(R.string.result_android_wins);
                        mAndroidCounter++;
                        mAndroidCount.setText(Integer.toString(mAndroidCounter));
                        mGameOver = true;
                    }
                }
            }    
        }
    }

    private void setMove(char player, int location)
    {
        mGame.setMove(player,location);
        mBoardButtons[location].setEnabled(false);
        mBoardButtons[location].setText(String.valueOf(player));
        if (player == mGame.HUMAN_PLAYER)
            mBoardButtons[location].setTextColor(Color.GREEN);
        else
        {
            mBoardButtons[location].setTextColor(Color.RED);
        }
    }

    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
        case R.id.ten:

            startNewGame();
            return;

        case R.id.eleven:

            MainActivity.this.finish();
            return;

        }
    }
}

答案 1 :(得分:0)

您在mTen和mEleven按钮上设置的监听器就是您的活动。

事情是没有实现onClick()方法。

如果我得到你想要做的事情,为了使你的mTen和mEleven按钮点击由ButtonClickListener处理,你必须首先实例化它,然后在按钮上设置它。

也许是这样的:

ButtonClickListener mButtonListener = new ButtonClickListener();
mTen.setOnClickListener(mButtonListener);
mEleven.setOnClickListener(mButtonListener;