猜拳小游戏

1: 输入两位猜拳者的名字
2:各自选择石头,剪刀,布
3:点击Start,开始猜拳
4:显示选手猜拳胜负结果

activity_main.xml

<?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"
      tools:context=".MainActivity">
  
      <TextView
          android:id="@+id/hearderTitle"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textColor="#F00"
          android:textSize="24dp"
          android:text="猜拳游戏"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintLeft_toLeftOf="parent"
          app:layout_constraintRight_toRightOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintVertical_bias="0.127" />
  
      <ImageView
          android:id="@+id/Girl"
          android:layout_width="126dp"
          android:layout_height="128dp"
          android:layout_marginTop="44dp"
          android:layout_marginEnd="40dp"
          android:layout_marginRight="40dp"
          android:src="@mipmap/ic_launcher_round"
          android:visibility="visible"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintTop_toBottomOf="@+id/hearderTitle"
          tools:srcCompat="@mipmap/ic_launcher_round" />
  
      <ImageView
          android:id="@+id/Boy"
          android:layout_width="126dp"
          android:layout_height="128dp"
          android:layout_marginStart="46dp"
          android:layout_marginLeft="46dp"
          android:layout_marginTop="42dp"
          android:visibility="visible"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toBottomOf="@+id/hearderTitle"
          app:srcCompat="@mipmap/ic_launcher"
          tools:srcCompat="@mipmap/ic_launcher" />
  
      <EditText
          android:id="@+id/bText"
          android:layout_width="136dp"
          android:layout_height="41dp"
          android:layout_marginStart="42dp"
          android:layout_marginLeft="42dp"
          android:layout_marginTop="33dp"
          android:ems="10"
          android:inputType="textPersonName"
          android:text="Name"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toBottomOf="@+id/Boy" />
  
      <EditText
          android:id="@+id/gText2"
          android:layout_width="130dp"
          android:layout_height="41dp"
          android:layout_marginStart="67dp"
          android:layout_marginLeft="67dp"
          android:layout_marginTop="29dp"
          android:layout_marginEnd="36dp"
          android:layout_marginRight="36dp"
          android:ems="10"
          android:inputType="textPersonName"
          android:text="Name"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toEndOf="@+id/bText"
          app:layout_constraintTop_toBottomOf="@+id/Girl" />
  
      <RadioGroup
          android:id="@+id/boyGroup"
          android:layout_width="119dp"
          android:layout_height="102dp"
          android:layout_marginStart="52dp"
          android:layout_marginLeft="52dp"
          android:layout_marginTop="23dp"
          android:layout_marginBottom="165dp"
          android:checkedButton="@id/bStone"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toBottomOf="@+id/bText"
          app:layout_constraintVertical_bias="0.666">
  
          <RadioButton
              android:id="@+id/bStone"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="石头"
              android:textSize="18dp"
              tools:layout_editor_absoluteX="56dp"
              tools:layout_editor_absoluteY="426dp" />
  
          <RadioButton
              android:id="@+id/bScissors"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="剪刀"
              android:textSize="18dp"
              tools:layout_editor_absoluteX="56dp"
              tools:layout_editor_absoluteY="426dp" />
  
          <RadioButton
              android:id="@+id/bFabric"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="布"
              android:textSize="18dp"
              tools:layout_editor_absoluteX="56dp"
              tools:layout_editor_absoluteY="426dp" />
      </RadioGroup>
  
      <RadioGroup
          android:id="@+id/girlGroup"
          android:layout_width="119dp"
          android:layout_height="102dp"
          android:layout_marginStart="256dp"
          android:layout_marginLeft="256dp"
          android:layout_marginTop="23dp"
          android:layout_marginBottom="165dp"
          android:checkedButton="@id/gFabric"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toBottomOf="@+id/bText"
          app:layout_constraintVertical_bias="0.671">
  
          <RadioButton
              android:id="@+id/gStone"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="石头"
              android:textSize="18dp"
              tools:layout_editor_absoluteX="56dp"
              tools:layout_editor_absoluteY="426dp" />
  
          <RadioButton
              android:id="@+id/gScissors"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="剪刀"
              android:textSize="18dp"
              tools:layout_editor_absoluteX="56dp"
              tools:layout_editor_absoluteY="426dp" />
  
          <RadioButton
              android:id="@+id/gFabric"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="布"
              android:textSize="18dp"
              tools:layout_editor_absoluteX="56dp"
              tools:layout_editor_absoluteY="426dp" />
      </RadioGroup>
  
      <Button
          android:id="@+id/startGuess"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginStart="157dp"
          android:layout_marginLeft="157dp"
          android:layout_marginTop="8dp"
          android:layout_marginEnd="157dp"
          android:layout_marginRight="157dp"
          android:layout_marginBottom="38dp"
          android:text="Start"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="0.0"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toBottomOf="@+id/result" />
  
      <TextView
          android:id="@+id/result"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginStart="157dp"
          android:layout_marginLeft="157dp"
          android:layout_marginTop="110dp"
          android:layout_marginEnd="157dp"
          android:layout_marginRight="157dp"
          android:layout_marginBottom="8dp"
          android:text="显示结果"
          android:textColor="#F00"
          android:textSize="16dp"
          app:layout_constraintBottom_toTopOf="@+id/startGuess"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="0.0"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toBottomOf="@+id/bText" />
  
  
  </androidx.constraintlayout.widget.ConstraintLayout>
  

MainActivity.java

package com.davis.asapp02;
      
      import androidx.appcompat.app.AppCompatActivity;
      
      import android.os.Bundle;
      import android.view.View;
      import android.widget.Button;
      import android.widget.EditText;
      import android.widget.RadioButton;
      import android.widget.RadioGroup;
      import android.widget.TextView;
      import android.widget.Toast;
      
      public class MainActivity extends AppCompatActivity {
      
          //1.声明要用到的控件
           private RadioGroup boyGroup;
           private RadioGroup girlGroup;
           private RadioButton bStone;
           private RadioButton bScissors;
           private RadioButton bFabric;
           private RadioButton gStone;
           private RadioButton gScissors;
           private RadioButton gFabric;
           private Button startGuess;
           private TextView gResult;
           private EditText bText;
           private EditText gText;
           private String str_result1 = "石头";
           private String str_result2 = "石头";
      
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              _FindView();
              boyGroup.setOnCheckedChangeListener(new _boyGroup());
              girlGroup.setOnCheckedChangeListener(new _girlGroup());
              startGuess.setOnClickListener(new _startGuess());
          }
          private void _FindView(){
              boyGroup = findViewById(R.id.boyGroup);
              girlGroup = findViewById(R.id.girlGroup);
              bStone = findViewById(R.id.bStone);
              bScissors = findViewById(R.id.bScissors);
              bFabric = findViewById(R.id.bScissors);
              gStone = findViewById(R.id.gStone);
              gScissors = findViewById(R.id.gScissors);
              gFabric = findViewById(R.id.gFabric);
              startGuess = findViewById(R.id.startGuess);
              gResult = findViewById(R.id.result);
              bText = findViewById(R.id.bText);
              gText = findViewById(R.id.gText2);
              bStone.setChecked(true);
              gStone.setChecked(true);
          }
          //boyGroup监听函数
          class _boyGroup implements RadioGroup.OnCheckedChangeListener{
              @Override
              public void onCheckedChanged(RadioGroup group, int checkedId) {
                  if(bStone.isChecked())   str_result1 = "石头";
                  else if(bScissors.isChecked())  str_result1 = "剪刀";
                  else if(bFabric.isChecked())  str_result1 = "布";        }
          }
          //girlGroup监听函数
          class _girlGroup implements RadioGroup.OnCheckedChangeListener{
              @Override
              public void onCheckedChanged(RadioGroup group, int checkedId) {
                  if(gStone.isChecked())   str_result2 = "石头";
                  else if(gScissors.isChecked())   str_result2 = "剪刀";
                  else if(gFabric.isChecked())  str_result2 = "布";
              }
          }
          //按键监听函数
          class _startGuess implements View.OnClickListener{
              @Override
              public void onClick(View v) {
                  String result = null;
                  if(str_result1 == "石头"){
                      if(str_result2 == "石头") result = "打平手!";
                      else if(str_result2 == "剪刀")
                          result = "A赢了!";
                      else if(str_result2 == "布")
                          result = "B赢了!";
                  }
                  else if(str_result1 == "剪刀") {
                      if (str_result2 == "石头")
                          result = "B赢了!";
                      else if (str_result2 == "剪刀")
                          result = "打平手!";
                      else if (str_result2 == "布")
                          result = "A赢了!";
                  }
                  else if(str_result1 == "布"){
                      if (str_result2 == "石头")
                          result = "A赢了!";
                      else if (str_result2 == "剪刀")
                          result = "B赢了!";
                      else if (str_result2 == "布")
                          result = "打平手!";
                  }
                  String UserA = bText.getText().toString();
                  String UserB = gText.getText().toString();
                  String winner = null;
                  System.out.println("UserName = " + UserA + ",UserNameB = " + UserB + ",winner = " + winner);
                  //判断有没有正确的输入用户名
                  //if(UserA == null || UserB == null)
                  //if(UserA == ""   && UserB == ""  )
                  //用上面两个方法行不通都不好用
                  //所以换成通过判断字符串长度来判定字符串为空
                  if(UserA.length() == 0 || UserB.length()==0) {
                      System.out.println("还没有输入姓名");
                      Toast.makeText(MainActivity.this,"请输入姓名",Toast.LENGTH_LONG).show();
                  }
                  else { //              if(result == "A赢了!")    winner = editText1.getText().toString();
                      //              else if(result == "B赢了!")   winner = editText2.getText().toString();
                      //用下面contains文本包含更方便快捷 也不容易出错
                      if (result.contains("A")) winner = UserA + " 赢了";
                      else if (result.contains("B")) winner = UserB + "赢了";
                      else winner = "打平手!";
                      gResult.setText(winner);
                      System.out.println(winner);
                  }
              }
          }
      }