我必须编写一个TicTacToe应用程序:
这是activity_main.xml的代码: (这是一个黑色背景上的TextViews的简单3x3字段)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:screenOrientation="portrait"
tools:context=".MainActivity">
<androidx.gridlayout.widget.GridLayout
android:layout_width="294dp"
android:layout_height="286dp"
android:background="#000000"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView">
<TextView
android:id="@+id/tictactoe_center_left"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_center_left"
android:text="@string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="0"
app:layout_row="1" />
<TextView
android:id="@+id/tictactoe_center_right"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_center_right"
android:text="@string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="2"
app:layout_row="1" />
<TextView
android:id="@+id/tictactoe_center_center"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_center_center"
android:text="@string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="1"
app:layout_row="1" />
<TextView
android:id="@+id/tictactoe_top_left"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:ems="10"
android:enabled="false"
android:focusable="true"
android:onClick="click_top_left"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="0"
app:layout_row="0"
tools:text="@string/game_playersymbol_x" />
<TextView
android:id="@+id/tictactoe_top_center"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_top_center"
android:text="@string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tictactoe_top_right"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_top_right"
android:text="@string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tictactoe_bottom_left"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_bottom_left"
android:text="@string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="0"
app:layout_row="2" />
<TextView
android:id="@+id/tictactoe_bottom_right"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_bottom_right"
android:text="@string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="2"
app:layout_row="2" />
<TextView
android:id="@+id/tictactoe_bottom_center"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_bottom_center"
android:text="@string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="1"
app:layout_row="2" />
</androidx.gridlayout.widget.GridLayout>
<Button
android:id="@+id/button"
android:layout_width="213dp"
android:layout_height="61dp"
android:layout_marginBottom="56dp"
android:background="@drawable/roundbutton"
android:text="@string/menu_play_singleplayer"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textColor="#979797"
android:textSize="50sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.066" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是MainActivity.java的代码:
package com.example.tictactoe;
import androidx.appcompat.app.AppCompatActivity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private int counter = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
public void click_top_left(View v) { click_action((TextView)findViewById(R.id.tictactoe_top_left)); }
public void click_top_center(View v) { click_action((TextView)findViewById(R.id.tictactoe_top_center)); }
public void click_top_right(View v) { click_action((TextView)findViewById(R.id.tictactoe_top_right)); }
public void click_center_left(View v) { click_action((TextView)findViewById(R.id.tictactoe_center_left)); }
public void click_center_center(View v) { click_action((TextView)findViewById(R.id.tictactoe_center_center)); }
public void click_center_right(View v) { click_action((TextView)findViewById(R.id.tictactoe_center_right)); }
public void click_bottom_left(View v) { click_action((TextView)findViewById(R.id.tictactoe_bottom_left)); }
public void click_bottom_center(View v) { click_action((TextView)findViewById(R.id.tictactoe_bottom_center)); }
public void click_bottom_right(View v) { click_action((TextView)findViewById(R.id.tictactoe_bottom_right)); }
public void click_action(TextView tv) {
if ((counter%2)==0)
tv.setText("O");
else
tv.setText("X");
counter++;
}
}
我已将android:clickable设置为true。 我设置了一个onclick侦听器。 我根本找不到我的错误...
我将不胜感激!
答案 0 :(得分:0)
首先为您的文本视图提供一个id,然后定义一个Textview textview
之类的示例,在Java中找到它,然后在onCreate方法中使用setOnClickListener(),如下所示。
textveiw.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { }});
答案 1 :(得分:0)
您已设置所有TextView
:
android:enabled="false"
为什么?
更改为:
android:enabled="true"