欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2024-03-07 17:25:15
本文实例为大家分享了android猜拳小游戏,供大家参考,具体内容如下 简单的 页面跳转 和 点击事件 的实现... --> androidmanifest...

本文实例为大家分享了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);
 }
 }
}

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