最常见的猜拳小游戏Android代码实现
2019/7/7 21:26:19
本文主要是介绍最常见的猜拳小游戏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代码实现的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-01-18android.permission.read_media_video
- 2024-01-18android_getaddrinfo failed eai_nodata
- 2024-01-18androidmo
- 2024-01-15Android下三种离屏渲染技术
- 2024-01-09Android 蓝牙使用
- 2024-01-06Android对接华为AI - 文本识别
- 2023-11-15代码安全之代码混淆及加固(Android)
- 2023-11-10简述Android语音播报TTS
- 2023-11-06Android WiFi工具类
- 2023-07-22Android开发未来的出路