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

 更新时间:2016年08月30日 09:17:44   作者:西门吃雪  
这篇文章主要为大家详细介绍了最常见的猜拳小游戏Android代码实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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中两个类让你再也不用实现onActivityResult()

    Android中两个类让你再也不用实现onActivityResult()

    这篇文章主要给大家介绍了关于Android中两个类让你再也不用实现onActivityResult()的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起看看吧
    2018-08-08
  • Android仿今日头条多个fragment懒加载的实现

    Android仿今日头条多个fragment懒加载的实现

    我们在做应用开发的时候,一个Activity里面可能会以viewpager(或其他容器)与多个Fragment来组合使用,下面这篇文章主要给大家介绍了关于利用Android仿今日头条多个fragment懒加载的相关资料,需要的朋友可以参考下。
    2017-12-12
  • Android实现商城购物车功能的实例代码

    Android实现商城购物车功能的实例代码

    最近公司项目做商城模块,需要实现购物车功能,主要实现了单选、全选,金额合计,商品删除,商品数量加减等功能,这篇文章主要介绍了Android实现商城购物车功能,需要的朋友可以参考下
    2019-06-06
  • Android中fragment+viewpager实现布局

    Android中fragment+viewpager实现布局

    这篇文章主要为大家详细介绍了Android中fragment+viewpager实现布局效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10
  • Android 文件操作详解及简单实例

    Android 文件操作详解及简单实例

    这篇文章主要介绍了 Android 文件操作详解及简单实例的相关资料,需要的朋友可以参考下
    2017-02-02
  • Android金额输入框只允许输入小数点后两位效果

    Android金额输入框只允许输入小数点后两位效果

    实现android 金额输入框输入小数点后两位的效果也不是很复杂,只需要设置输入框输入的字符类型、设置InputFilter、设置输入变化监听即可。这篇文章主要介绍了Android金额输入框只允许输入小数点后两位 ,需要的朋友可以参考下
    2017-05-05
  • Android中将一个图片切割成多个图片的实现方法

    Android中将一个图片切割成多个图片的实现方法

    有种场景,我们想将一个图片切割成多个图片。比如我们在开发一个拼图的游戏,就首先要对图片进行切割
    2013-05-05
  • Android 6.0 扫描不到 Ble 设备需开启位置权限的方法

    Android 6.0 扫描不到 Ble 设备需开启位置权限的方法

    今天小编就为大家分享一篇Android 6.0 扫描不到 Ble 设备需开启位置权限的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • Android AutoCompleteTextView控件基本用法示例

    Android AutoCompleteTextView控件基本用法示例

    这篇文章主要介绍了Android AutoCompleteTextView控件基本用法,结合实例形式分析了AutoCompleteTextView控件的功能、使用方法及相关操作步骤,需要的朋友可以参考下
    2016-06-06
  • Android实现拍照、录像、录音代码范例

    Android实现拍照、录像、录音代码范例

    这篇文章主要介绍了Android实现拍照、录像、录音代码的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2016-10-10

最新评论