SoFunction
Updated on 2025-04-10

The most common Android code implementation of the rock-scissors game

The examples in this article share Android rock-paper-scissors game for your reference. The specific content is as follows

Simple page jump and click event implementation...

-->

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:andro
 package=""
 android:versionCode="1"
 android:versionName="1.0" >

 <uses-sdk
  android:minSdkVersion="15"
  android:targetSdkVersion="19" />

 <application
  android:allowBackup="true"
  android:icon="@drawable/ic_launcher"
  android:label="@string/app_name"
  android:theme="@style/AppTheme" >
  <activity
   android:name=""
   android:label="@string/app_name" >
   <intent-filter>
    <action android:name="" />

    <category android:name="" />
   </intent-filter>
  </activity>
  <activity 
   android:name="">
  </activity>
 </application>

</manifest>

AndroidManifest

-->

&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;resources&gt;

 &lt;string name="app_name"&gt;fight&lt;/string&gt;
 &lt;string name="hello_world"&gt;Hello world!&lt;/string&gt;
 &lt;string name="action_settings"&gt;Settings&lt;/string&gt;
 &lt;string name="player1"&gt;Party A&lt;/string&gt;
 &lt;string name="player2"&gt;Party B&lt;/string&gt;
 &lt;string name="choose1"&gt;Stone&lt;/string&gt;
 &lt;string name="choose2"&gt;Scissors&lt;/string&gt;
 &lt;string name="choose3"&gt;cloth&lt;/string&gt;
 &lt;string name="sure"&gt;Punch&lt;/string&gt;
 &lt;string name="again"&gt;Another round&lt;/string&gt;

&lt;/resources&gt;


--&gt; fragment_main.xml
&lt;RelativeLayout xmlns:andro
 xmlns:tools="/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#ffffff"
 tools:context="$PlaceholderFragment" &gt;

 &lt;TextView
  android:
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentTop="true"
  android:layout_centerHorizontal="true"
  android:layout_marginTop="36dp"
  android:text="@string/player1"
  android:textSize="30sp" /&gt;

 &lt;RadioGroup
  android:
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerHorizontal="true"
  android:layout_centerVertical="true" &gt;

  &lt;RadioButton
   android:
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:checked="true"
   android:text="@string/choose1" /&gt;

  &lt;RadioButton
   android:
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/choose2" /&gt;

  &lt;RadioButton
   android:
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/choose3" /&gt;
 &lt;/RadioGroup&gt;

 &lt;Button
  android:
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignLeft="@+id/radioGroup1"
  android:layout_below="@+id/radioGroup1"
  android:layout_marginTop="14dp"
  android:text="@string/sure" /&gt;

 &lt;ImageView
  android:
  android:layout_width="120dp"
  android:layout_height="120dp"
  android:layout_above="@+id/radioGroup1"
  android:layout_below="@+id/textView1"
  android:layout_centerHorizontal="true"
  android:src="@drawable/b" /&gt;

&lt;/RelativeLayout&gt;

--> activity_other.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:andro
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" 
 android:background="#ffffff" >

 <TextView
  android:
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentTop="true"
  android:layout_centerHorizontal="true"
  android:layout_marginTop="36dp"
  android:text="@string/player2"
  android:textSize="30sp" />

 <RadioGroup
  android:
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerHorizontal="true"
  android:layout_centerVertical="true" >

  <RadioButton
   android:
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:checked="true"
   android:text="@string/choose1" />

  <RadioButton
   android:
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/choose2" />

  <RadioButton
   android:
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/choose3" />
 </RadioGroup>

 <Button
  android:
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignLeft="@+id/radioGroup1"
  android:layout_below="@+id/radioGroup1"
  android:layout_marginTop="14dp"
  android:text="@string/sure" />

 <TextView
  android:
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignLeft="@+id/textView1"
  android:layout_below="@+id/button1"
  android:visibility="invisible"
  android:layout_marginTop="14dp"/>

 <Button
  android:
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@+id/textView2"
  android:layout_centerHorizontal="true"
  android:visibility="invisible"
  android:text="@string/again" />

 <ImageView
  android:
  android:layout_width="120dp"
  android:layout_height="120dp"
  android:layout_above="@+id/radioGroup1"
  android:layout_below="@+id/textView1"
  android:layout_centerHorizontal="true"
  android:src="@drawable/a" />
 
</RelativeLayout>

--> MainActivity

package ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

public class MainActivity extends Activity {
 // Set a static variable to close the Activity public static MainActivity instance = null;
 private RadioGroup radioGroup1;
 private Button button1;
 private ImageView imageView1;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 // Represents the current activity instance = this;
 (savedInstanceState);
 setContentView(.fragment_main);
 radioGroup1 = (RadioGroup) findViewById(.radioGroup1);
 // Set the picture to be transparent // imageView1 = (ImageView) findViewById(.imageView1);
 // ().setAlpha(100);
 button1 = (Button) findViewById(.button1);
 (new MyButtonListener());
 }

 class MyButtonListener implements OnClickListener {

 @Override
 public void onClick(View v) {
  // Get the selected RadioButton  RadioButton radioButton = (RadioButton) findViewById(radioGroup1
   .getCheckedRadioButtonId());
  String radioText = ().toString();
  Intent intent = new Intent();
  ("checked", radioText);
  (, );
  startActivity(intent);
 }
 }
}

--> OtherActivity

package ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

public class OtherActivity extends Activity {
 private RadioGroup radioGroup1;
 private Button button1;
 private TextView textView2;
 private RadioButton radioButton;
 private Button button2;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 (savedInstanceState);
 setContentView(.activity_other);
 radioGroup1 = (RadioGroup) findViewById(.radioGroup1);
 button1 = (Button) findViewById(.button1);
 textView2 = (TextView) findViewById(.textView2);
 button2 = (Button) findViewById(.button2);
 (new MyButtonListener());
 (new MyButtonListener1());
 }

 class MyButtonListener implements OnClickListener {

 @Override
 public void onClick(View v) {
  radioButton = (RadioButton) findViewById(radioGroup1
   .getCheckedRadioButtonId());
  String buttonText = ().toString();
  Intent intent = getIntent();
  String checked = ("checked");
  // Set View to visible  ();
  ();
  String msg = "A:" + checked + "\n" + "B" + buttonText
   + "\n";
  if ((checked)) {
  (msg + "draw");
  }
  if (("Stone")) {
  if (("Scissors")) {
   (msg + "Party B wins");
  } else if (("cloth")) {
   (msg + "Party A wins");
  }
  }
  if (("Scissors")) {
  if (("cloth")) {
   (msg + "Party B wins");
  } else if (("Stone")) {
   (msg + "Party A wins");
  }
  }
  if (("cloth")) {
  if (("Stone")) {
   (msg + "Party B wins");
  } else if (("Scissors")) {
   (msg + "Party A wins");
  }
  }
 }
 }

 class MyButtonListener1 implements OnClickListener {

 @Override
 public void onClick(View arg0) {
  Intent intent = new Intent();
  (, );
  finish();
  // Close the specified activity  ();
  startActivity(intent);
 }
 }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.