Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android-->猜拳小游戲,android--猜拳

Android-->猜拳小游戲,android--猜拳

編輯:關於android開發

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 <application 12 android:allowBackup="true" 13 android:icon="@drawable/ic_launcher" 14 android:label="@string/app_name" 15 android:theme="@style/AppTheme" > 16 <activity 17 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> AndroidManifest

--> 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    <RadioGroup
19        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        <RadioButton
26            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        <RadioButton
33            android:id="@+id/radio1"
34            android:layout_width="wrap_content"
35            android:layout_height="wrap_content"
36            android:text="@string/choose2" />
37 
38        <RadioButton
39            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    <Button
46        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    <ImageView
55        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 <RadioGroup 19 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 <RadioButton 26 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 <RadioButton 33 android:id="@+id/radio1" 34 android:layout_width="wrap_content" 35 android:layout_height="wrap_content" 36 android:text="@string/choose2" /> 37 38 <RadioButton 39 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 <Button 46 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 <TextView 55 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 <Button 64 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 <ImageView 73 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> activity_main

--> 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     // 設置一個靜態變量,用於關閉Activity
15     public static MainActivity instance = null;
16     private RadioGroup radioGroup1;
17     private Button button1;
18     private ImageView imageView1;
19 
20     @Override
21     protected void onCreate(Bundle savedInstanceState) {
22         // 代表當前的Activity
23         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         @Override
37         public void onClick(View v) {
38             // 得到選中的RadioButton
39             RadioButton radioButton = (RadioButton) findViewById(radioGroup1
40                     .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     @Override
21     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         @Override
35         public void onClick(View v) {
36             radioButton = (RadioButton) findViewById(radioGroup1
37                     .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" + "乙出:" + buttonText
45                     + "\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         @Override
76         public void onClick(View arg0) {
77             Intent intent = new Intent();
78             intent.setClass(OtherActivity.this, MainActivity.class);
79             finish();
80             // 關閉指定Activity
81             MainActivity.instance.finish();
82             startActivity(intent);
83         }
84     }
85 }

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved