最常见的猜拳小游戏Android代码实现

所属分类: 软件编程 / Android 阅读数: 56
收藏 0 赞 0 分享

本文实例为大家分享了Android猜拳小游戏,供大家参考,具体内容如下

简单的 页面跳转 和 点击事件 的实现...

--> AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.dragon.android.fight"
 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="com.dragon.android.fight.MainActivity"
   android:label="@string/app_name" >
   <intent-filter>
    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
  </activity>
  <activity 
   android:name="com.dragon.android.fight.OtherActivity">
  </activity>
 </application>

</manifest>

AndroidManifest

--> strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

 <string name="app_name">fight</string>
 <string name="hello_world">Hello world!</string>
 <string name="action_settings">Settings</string>
 <string name="player1">甲方</string>
 <string name="player2">乙方</string>
 <string name="choose1">石头</string>
 <string name="choose2">剪刀</string>
 <string name="choose3">布</string>
 <string name="sure">出拳</string>
 <string name="again">再来一局</string>

</resources>


--> fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#ffffff"
 tools:context="com.dragon.android.fight.MainActivity$PlaceholderFragment" >

 <TextView
  android:id="@+id/textView1"
  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" />

 <RadioGroup
  android:id="@+id/radioGroup1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerHorizontal="true"
  android:layout_centerVertical="true" >

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

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

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

 <Button
  android:id="@+id/button1"
  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" />

 <ImageView
  android:id="@+id/imageView1"
  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" />

</RelativeLayout>

--> activity_other.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" 
 android:background="#ffffff" >

 <TextView
  android:id="@+id/textView1"
  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:id="@+id/radioGroup1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerHorizontal="true"
  android:layout_centerVertical="true" >

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

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

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

 <Button
  android:id="@+id/button1"
  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:id="@+id/textView2"
  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:id="@+id/button2"
  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:id="@+id/imageView1"
  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 com.dragon.android.fight;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends Activity {
 // 设置一个静态变量,用于关闭Activity
 public static MainActivity instance = null;
 private RadioGroup radioGroup1;
 private Button button1;
 private ImageView imageView1;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 // 代表当前的Activity
 instance = this;
 super.onCreate(savedInstanceState);
 setContentView(R.layout.fragment_main);
 radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);
 // 设置图片透明
 // imageView1 = (ImageView) findViewById(R.id.imageView1);
 // imageView1.getBackground().setAlpha(100);
 button1 = (Button) findViewById(R.id.button1);
 button1.setOnClickListener(new MyButtonListener());
 }

 class MyButtonListener implements OnClickListener {

 @Override
 public void onClick(View v) {
  // 得到选中的RadioButton
  RadioButton radioButton = (RadioButton) findViewById(radioGroup1
   .getCheckedRadioButtonId());
  String radioText = radioButton.getText().toString();
  Intent intent = new Intent();
  intent.putExtra("checked", radioText);
  intent.setClass(MainActivity.this, OtherActivity.class);
  startActivity(intent);
 }
 }
}

--> OtherActivity

package com.dragon.android.fight;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

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) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_other);
 radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);
 button1 = (Button) findViewById(R.id.button1);
 textView2 = (TextView) findViewById(R.id.textView2);
 button2 = (Button) findViewById(R.id.button2);
 button1.setOnClickListener(new MyButtonListener());
 button2.setOnClickListener(new MyButtonListener1());
 }

 class MyButtonListener implements OnClickListener {

 @Override
 public void onClick(View v) {
  radioButton = (RadioButton) findViewById(radioGroup1
   .getCheckedRadioButtonId());
  String buttonText = radioButton.getText().toString();
  Intent intent = getIntent();
  String checked = intent.getStringExtra("checked");
  // 设置View为可见
  textView2.setVisibility(View.VISIBLE);
  button2.setVisibility(View.VISIBLE);
  String msg = "甲出:" + checked + "\n" + "乙出:" + buttonText
   + "\n";
  if (buttonText.equals(checked)) {
  textView2.setText(msg + "平局");
  }
  if (buttonText.equals("石头")) {
  if (checked.equals("剪刀")) {
   textView2.setText(msg + "乙方赢");
  } else if (checked.equals("布")) {
   textView2.setText(msg + "甲方赢");
  }
  }
  if (buttonText.equals("剪刀")) {
  if (checked.equals("布")) {
   textView2.setText(msg + "乙方赢");
  } else if (checked.equals("石头")) {
   textView2.setText(msg + "甲方赢");
  }
  }
  if (buttonText.equals("布")) {
  if (checked.equals("石头")) {
   textView2.setText(msg + "乙方赢");
  } else if (checked.equals("剪刀")) {
   textView2.setText(msg + "甲方赢");
  }
  }
 }
 }

 class MyButtonListener1 implements OnClickListener {

 @Override
 public void onClick(View arg0) {
  Intent intent = new Intent();
  intent.setClass(OtherActivity.this, MainActivity.class);
  finish();
  // 关闭指定Activity
  MainActivity.instance.finish();
  startActivity(intent);
 }
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android样式和主题之选择器的实例讲解

今天小编就为大家分享一篇关于Android样式和主题之选择器的实例讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Android应用动态修改主题的方法示例

今天小编就为大家分享一篇关于Android应用动态修改主题的方法示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter 网络请求框架封装详解

这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Flutter倒计时/计时器的实现代码

这篇文章主要介绍了Flutter倒计时/计时器的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android使用google breakpad捕获分析native cash

这篇文章主要介绍了Android使用google breakpad捕获分析native cash 的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Flutter WebView与JS互相调用简易指南

这篇文章主要介绍了详解Flutter WebView与JS互相调用简易指南,分为JS调用Flutter和Flutter调用JS,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android开发实现ListView和adapter配合显示图片和文字列表功能示例

这篇文章主要介绍了Android开发实现ListView和adapter配合显示图片和文字列表功能,涉及Android使用ListView结合adapter适配器实现图文显示功能相关的布局、解析、权限控制等操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android文字基线Baseline算法的使用讲解

今天小编就为大家分享一篇关于Android文字基线Baseline算法的使用讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter自定义实现神奇动效的卡片切换视图的示例代码

这篇文章主要介绍了Flutter自定义实现神奇动效的卡片切换视图的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android实现自定义滑动刻度尺方法示例

这篇文章主要给大家介绍了关于Android实现自定义滑动刻度尺的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多