activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:id="@+id/button1"
android:text=" " />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:id="@+id/button2"
android:text=" " />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:id="@+id/button3"
android:text=" " />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:id="@+id/button4"
android:text=" " />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:id="@+id/button5"
android:text=" " />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:id="@+id/button6"
android:text=" " />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:id="@+id/button7"
android:text=" " />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:id="@+id/button8"
android:text=" " />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:id="@+id/button9"
android:text=" " />
</LinearLayout>
</LinearLayout>
MainActivity
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
int click = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
///////////////////
public void sendMessage(View v) {
Button b1 = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
Button b3 = (Button) findViewById(R.id.button3);
Button b4 = (Button) findViewById(R.id.button4);
Button b5 = (Button) findViewById(R.id.button5);
Button b6 = (Button) findViewById(R.id.button6);
Button b7 = (Button) findViewById(R.id.button7);
Button b8 = (Button) findViewById(R.id.button8);
Button b9 = (Button) findViewById(R.id.button9);
String buttonText = "";
if (click == 0) {
click = 1;
}
else
{
click = 0;
}
switch (v.getId()) {
case R.id.button1:
buttonText = b1.getText().toString();
if (buttonText.equals(" ") && click == 1) b1.setText("X");
if (buttonText.equals(" ") && click == 0) b1.setText("O");
break;
case R.id.button2:
buttonText = b2.getText().toString();
if (buttonText.equals(" ") && click == 1) b2.setText("X");
if (buttonText.equals(" ") && click == 0) b2.setText("O");
break;
case R.id.button3:
buttonText = b3.getText().toString();
if (buttonText.equals(" ") && click == 1) b3.setText("X");
if (buttonText.equals(" ") && click == 0) b3.setText("O");
break;
case R.id.button4:
buttonText = b4.getText().toString();
if (buttonText.equals(" ") && click == 1) b4.setText("X");
if (buttonText.equals(" ") && click == 0) b4.setText("O");
break;
case R.id.button5:
buttonText = b5.getText().toString();
if (buttonText.equals(" ") && click == 1) b5.setText("X");
if (buttonText.equals(" ") && click == 0) b5.setText("O");
break;
case R.id.button6:
buttonText = b6.getText().toString();
if (buttonText.equals(" ") && click == 1) b6.setText("X");
if (buttonText.equals(" ") && click == 0) b6.setText("O");
break;
case R.id.button7:
buttonText = b7.getText().toString();
if (buttonText.equals(" ") && click == 1) b7.setText("X");
if (buttonText.equals(" ") && click == 0) b7.setText("O");
break;
case R.id.button8:
buttonText = b8.getText().toString();
if (buttonText.equals(" ") && click == 1) b8.setText("X");
if (buttonText.equals(" ") && click == 0) b8.setText("O");
break;
case R.id.button9:
buttonText = b9.getText().toString();
if (buttonText.equals(" ") && click == 1) b9.setText("X");
if (buttonText.equals(" ") && click == 0) b9.setText("O");
break;
}
}
//////////////
}
|