首页 > 代码库 > Android-->猜拳小游戏
Android-->猜拳小游戏
--> 简单的 页面跳转 和 点击事件 的实现...
--> AndroidManifest.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.dragon.android.fight" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="15" 9 android:targetSdkVersion="19" />10 11 <application12 android:allowBackup="true"13 android:icon="@drawable/ic_launcher"14 android:label="@string/app_name"15 android:theme="@style/AppTheme" >16 <activity17 android:name="com.dragon.android.fight.MainActivity"18 android:label="@string/app_name" >19 <intent-filter>20 <action android:name="android.intent.action.MAIN" />21 22 <category android:name="android.intent.category.LAUNCHER" />23 </intent-filter>24 </activity>25 <activity 26 android:name="com.dragon.android.fight.OtherActivity">27 </activity>28 </application>29 30 </manifest>
--> strings.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 4 <string name="app_name">fight</string> 5 <string name="hello_world">Hello world!</string> 6 <string name="action_settings">Settings</string> 7 <string name="player1">甲方</string> 8 <string name="player2">乙方</string> 9 <string name="choose1">石头</string>10 <string name="choose2">剪刀</string>11 <string name="choose3">布</string>12 <string name="sure">出拳</string>13 <string name="again">再来一局</string>14 15 </resources>
--> fragment_main.xml
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="#ffffff" 6 tools:context="com.dragon.android.fight.MainActivity$PlaceholderFragment" > 7 8 <TextView 9 android:id="@+id/textView1"10 android:layout_width="wrap_content"11 android:layout_height="wrap_content"12 android:layout_alignParentTop="true"13 android:layout_centerHorizontal="true"14 android:layout_marginTop="36dp"15 android:text="@string/player1"16 android:textSize="30sp" />17 18 <RadioGroup19 android:id="@+id/radioGroup1"20 android:layout_width="wrap_content"21 android:layout_height="wrap_content"22 android:layout_centerHorizontal="true"23 android:layout_centerVertical="true" >24 25 <RadioButton26 android:id="@+id/radio0"27 android:layout_width="wrap_content"28 android:layout_height="wrap_content"29 android:checked="true"30 android:text="@string/choose1" />31 32 <RadioButton33 android:id="@+id/radio1"34 android:layout_width="wrap_content"35 android:layout_height="wrap_content"36 android:text="@string/choose2" />37 38 <RadioButton39 android:id="@+id/radio2"40 android:layout_width="wrap_content"41 android:layout_height="wrap_content"42 android:text="@string/choose3" />43 </RadioGroup>44 45 <Button46 android:id="@+id/button1"47 android:layout_width="wrap_content"48 android:layout_height="wrap_content"49 android:layout_alignLeft="@+id/radioGroup1"50 android:layout_below="@+id/radioGroup1"51 android:layout_marginTop="14dp"52 android:text="@string/sure" />53 54 <ImageView55 android:id="@+id/imageView1"56 android:layout_width="120dp"57 android:layout_height="120dp"58 android:layout_above="@+id/radioGroup1"59 android:layout_below="@+id/textView1"60 android:layout_centerHorizontal="true"61 android:src="@drawable/b" />62 63 </RelativeLayout>
--> activity_other.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 android:background="#ffffff" > 7 8 <TextView 9 android:id="@+id/textView1"10 android:layout_width="wrap_content"11 android:layout_height="wrap_content"12 android:layout_alignParentTop="true"13 android:layout_centerHorizontal="true"14 android:layout_marginTop="36dp"15 android:text="@string/player2"16 android:textSize="30sp" />17 18 <RadioGroup19 android:id="@+id/radioGroup1"20 android:layout_width="wrap_content"21 android:layout_height="wrap_content"22 android:layout_centerHorizontal="true"23 android:layout_centerVertical="true" >24 25 <RadioButton26 android:id="@+id/radio0"27 android:layout_width="wrap_content"28 android:layout_height="wrap_content"29 android:checked="true"30 android:text="@string/choose1" />31 32 <RadioButton33 android:id="@+id/radio1"34 android:layout_width="wrap_content"35 android:layout_height="wrap_content"36 android:text="@string/choose2" />37 38 <RadioButton39 android:id="@+id/radio2"40 android:layout_width="wrap_content"41 android:layout_height="wrap_content"42 android:text="@string/choose3" />43 </RadioGroup>44 45 <Button46 android:id="@+id/button1"47 android:layout_width="wrap_content"48 android:layout_height="wrap_content"49 android:layout_alignLeft="@+id/radioGroup1"50 android:layout_below="@+id/radioGroup1"51 android:layout_marginTop="14dp"52 android:text="@string/sure" />53 54 <TextView55 android:id="@+id/textView2"56 android:layout_width="wrap_content"57 android:layout_height="wrap_content"58 android:layout_alignLeft="@+id/textView1"59 android:layout_below="@+id/button1"60 android:visibility="invisible"61 android:layout_marginTop="14dp"/>62 63 <Button64 android:id="@+id/button2"65 android:layout_width="wrap_content"66 android:layout_height="wrap_content"67 android:layout_below="@+id/textView2"68 android:layout_centerHorizontal="true"69 android:visibility="invisible"70 android:text="@string/again" />71 72 <ImageView73 android:id="@+id/imageView1"74 android:layout_width="120dp"75 android:layout_height="120dp"76 android:layout_above="@+id/radioGroup1"77 android:layout_below="@+id/textView1"78 android:layout_centerHorizontal="true"79 android:src="@drawable/a" />80 81 </RelativeLayout>
--> MainActivity
1 package com.dragon.android.fight; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.view.View.OnClickListener; 8 import android.widget.Button; 9 import android.widget.ImageView;10 import android.widget.RadioButton;11 import android.widget.RadioGroup;12 13 public class MainActivity extends Activity {14 // 设置一个静态变量,用于关闭Activity15 public static MainActivity instance = null;16 private RadioGroup radioGroup1;17 private Button button1;18 private ImageView imageView1;19 20 @Override21 protected void onCreate(Bundle savedInstanceState) {22 // 代表当前的Activity23 instance = this;24 super.onCreate(savedInstanceState);25 setContentView(R.layout.fragment_main);26 radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);27 // 设置图片透明28 // imageView1 = (ImageView) findViewById(R.id.imageView1);29 // imageView1.getBackground().setAlpha(100);30 button1 = (Button) findViewById(R.id.button1);31 button1.setOnClickListener(new MyButtonListener());32 }33 34 class MyButtonListener implements OnClickListener {35 36 @Override37 public void onClick(View v) {38 // 得到选中的RadioButton39 RadioButton radioButton = (RadioButton) findViewById(radioGroup140 .getCheckedRadioButtonId());41 String radioText = radioButton.getText().toString();42 Intent intent = new Intent();43 intent.putExtra("checked", radioText);44 intent.setClass(MainActivity.this, OtherActivity.class);45 startActivity(intent);46 }47 }48 }
--> OtherActivity
1 package com.dragon.android.fight; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.view.View.OnClickListener; 8 import android.widget.Button; 9 import android.widget.RadioButton;10 import android.widget.RadioGroup;11 import android.widget.TextView;12 13 public class OtherActivity extends Activity {14 private RadioGroup radioGroup1;15 private Button button1;16 private TextView textView2;17 private RadioButton radioButton;18 private Button button2;19 20 @Override21 protected void onCreate(Bundle savedInstanceState) {22 super.onCreate(savedInstanceState);23 setContentView(R.layout.activity_other);24 radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);25 button1 = (Button) findViewById(R.id.button1);26 textView2 = (TextView) findViewById(R.id.textView2);27 button2 = (Button) findViewById(R.id.button2);28 button1.setOnClickListener(new MyButtonListener());29 button2.setOnClickListener(new MyButtonListener1());30 }31 32 class MyButtonListener implements OnClickListener {33 34 @Override35 public void onClick(View v) {36 radioButton = (RadioButton) findViewById(radioGroup137 .getCheckedRadioButtonId());38 String buttonText = radioButton.getText().toString();39 Intent intent = getIntent();40 String checked = intent.getStringExtra("checked");41 // 设置View为可见42 textView2.setVisibility(View.VISIBLE);43 button2.setVisibility(View.VISIBLE);44 String msg = "甲出:" + checked + "\n" + "乙出:" + buttonText45 + "\n";46 if (buttonText.equals(checked)) {47 textView2.setText(msg + "平局");48 }49 if (buttonText.equals("石头")) {50 if (checked.equals("剪刀")) {51 textView2.setText(msg + "乙方赢");52 } else if (checked.equals("布")) {53 textView2.setText(msg + "甲方赢");54 }55 }56 if (buttonText.equals("剪刀")) {57 if (checked.equals("布")) {58 textView2.setText(msg + "乙方赢");59 } else if (checked.equals("石头")) {60 textView2.setText(msg + "甲方赢");61 }62 }63 if (buttonText.equals("布")) {64 if (checked.equals("石头")) {65 textView2.setText(msg + "乙方赢");66 } else if (checked.equals("剪刀")) {67 textView2.setText(msg + "甲方赢");68 }69 }70 }71 }72 73 class MyButtonListener1 implements OnClickListener {74 75 @Override76 public void onClick(View arg0) {77 Intent intent = new Intent();78 intent.setClass(OtherActivity.this, MainActivity.class);79 finish();80 // 关闭指定Activity81 MainActivity.instance.finish();82 startActivity(intent);83 }84 }85 }
Android-->猜拳小游戏
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。