2014-02-13 30 views
0

您好,這是我在堆棧溢出中的第一篇文章,我是一名完整的初學者,因此非常感謝您的幫助!如何添加一個onClickLIstener到我的代碼來執行我已經實現的方法

好的,所以我正在按照製作TicTacToe(nirtts和十字架)遊戲的教程在java中爲android和教程中的人在菜單中使重置遊戲按鈕,但是我想做一個實際的按鈕下游戲區。我已經添加了按鈕(請參閱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> 

回答

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 

     } 
    }); 
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)方法的代碼重置您的遊戲

相關問題