Java 如何向代码中添加onClickLIstener以执行我已经实现的方法
您好,这是我在stack overflow中的第一篇帖子,我是一个完全的编程初学者,非常感谢您的帮助 好的,我正在学习一个用java为android制作Tictatcoe(noughts and Crosss)游戏的教程,在这个教程中,玩家在菜单中创建了一个重置游戏按钮,但是我想在游戏区域下方创建一个实际的按钮。我已经添加了按钮(见XML的底部),它们已经是重置游戏的方法,所以我需要帮助来弄清楚如何在java中添加代码,以及在哪里添加代码,以便当我单击其中一个按钮时,它会为我重置游戏(清除棋盘),提前谢谢Java 如何向代码中添加onClickLIstener以执行我已经实现的方法,java,android,xml,methods,onclicklistener,Java,Android,Xml,Methods,Onclicklistener,您好,这是我在stack overflow中的第一篇帖子,我是一个完全的编程初学者,非常感谢您的帮助 好的,我正在学习一个用java为android制作Tictatcoe(noughts and Crosss)游戏的教程,在这个教程中,玩家在菜单中创建了一个重置游戏按钮,但是我想在游戏区域下方创建一个实际的按钮。我已经添加了按钮(见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);
}
}
package com.C05025.noughtsandcross;
导入android.app.Activity;
导入android.graphics.Color;
导入android.os.Bundle;
导入android.widget.Button;
导入android.widget.TextView;
导入android.view.view;
公开课NoughtsandCrossGame扩展活动{
私人游戏;
专用按钮PASButtons[];
私有文本视图信息文本视图;
私有文本视图用户计数;
私有文本视图支取计数;
私有文本视图帐户;
私有int UserCounter=0;
专用int DrawCounter=0;
私有整数AICounter=0;
私有布尔UserFirst=true;
私有布尔GameOver=false;
按钮;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.playarea);
PASButtons=新按钮[NoughtsandCross.getPLAY_AREA_SIZE()];
PASButtons[0]=(按钮)findViewById(R.id.one);
PASButtons[1]=(按钮)findViewById(R.id.two);
PASButtons[2]=(按钮)findViewById(R.id.three);
PASButtons[3]=(按钮)findViewById(R.id.four);
PASButtons[4]=(按钮)findViewById(R.id.five);
PASButtons[5]=(按钮)findViewById(R.id.six);
PASButtons[6]=(按钮)findViewById(R.id.seven);
PASButtons[7]=(按钮)findViewById(R.id.eight);
PASButtons[8]=(按钮)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));
setText(Integer.toString(AICounter));
游戏=新世纪和十字架();
startNewName();
}
私有void startNewName(){
Game.resetBoard();
对于(int i=0;i
以下是我的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>
与您的按钮描述相同:
<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)
方法,如果按钮的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
}
});