我必須編寫一個(gè) TicTacToe 應(yīng)用程序:這是 Activity_main.xml 的代碼:(這是一個(gè)黑色背景上的 TextViews 的簡(jiǎn)單 3x3 字段)這是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 設(shè)置為 true。我設(shè)置了一個(gè) onclick 監(jiān)聽(tīng)器。我根本找不到我的錯(cuò)誤......我將不勝感激任何形式的幫助!
Android TextView OnClick-Event 不起作用
當(dāng)年話下
2023-12-21 10:38:13