作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
您好,这是我在堆栈溢出中发表的第一篇文章,我是一个完全的编程初学者,因此非常感谢您的帮助!
好的,所以我正在遵循一个关于在 Android 上用 java 制作 TicTacToe(圈和十字)游戏的教程,在教程中,人们在菜单中制作了一个“重置游戏”按钮,但是我想在游戏区域下方制作一个实际的按钮。我已经添加了按钮(参见 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>
最佳答案
如果您的按钮 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
}
});
关于java - 如何将 onClickLIstener 添加到我的代码中以执行我已经实现的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21757091/
我是一名优秀的程序员,十分优秀!