Android-->猜拳小游戲

来源:http://www.cnblogs.com/xmcx1995/archive/2016/08/29/5820227.html
-Advertisement-
Play Games

--> 簡單的 頁面跳轉 和 點擊事件 的實現... --> AndroidManifest.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/ ...


--> 簡單的 頁面跳轉 和 點擊事件 的實現... 

--> 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 }

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • × 目錄 [1]特征 [2]屬性 [3]方法 前面的話 元素的特性在DOM中以Attr類型表示,從技術角度講,特性是存在於元素的attributes屬性中的節點。儘管特性是節點,但卻不是DOM節點樹的一部分。本文將詳細介紹該部分內容 特征 特性節點的三個node屬性————nodeType、node ...
  • 讀完 O’Reilly Media 出版的 Web Audio API 第一章對基礎部分做了一些筆記和整理。 ...
  • 1.undefined undefined在js中並不是關鍵字/保留字,因此在IE5.5~8中可以對undefined賦值,但是在IE9以上,對其賦值是無效的 在IE5.5~8中: undefined 1 undefined number 在IE9以上: undefined undefined un ...
  • 原型陷阱: 在處理原型問題上時,我們要註意兩種行為。 1. 當我們對原型對象執行完全替換的時候,有可能會觸發原型鏈的某種異常。 2. prototype.constructor 屬性是不可靠的。 下麵,我們新建一個構造函數,並創建兩個對象: 即使在對象she1和she2對象被創建之後,我們仍然可以對 ...
  • 面向對象 一、編程範式: 1.命令式編程,2.聲明式 命令式編程的思想:面向過程,面向對象。聲明式的思想:DSL(領域特定語言) 命令式編程的定義:告訴電腦以什麼指令來執行代碼。註重中間過程。 聲明式的定義:告訴電腦想要什麼結果就讓電腦自己去執行。不需要關心過程。 面向過程:一步一步的向下執行 ...
  • 一,文本格式化:此例演示如何在一個 HTML 文件中對文本進行格式化。 效果如下: 二,預格式文本:此例演示如何使用 pre 標簽對空行和空格進行控制。 效果如下: 三,“電腦輸出”標簽:此例演示不同的“電腦輸出”標簽的顯示效果。 效果如下: 四,地址:此例演示如何在 HTML 文件中寫地址。 ...
  • 簡單記錄下網站首頁的搭建過程。 背景 自從網站功能變數名稱備案成功下來,一直以來都沒想好首頁應該怎麼寫。其實不是沒想好,而是沒有準備好首頁的多張大背景圖片應該存放在那,畢竟是最廉價的雲伺服器,應該本著勤儉持家的理念,能省就省嘛。不過還好, "bing中文搜索" 官網的背景圖片每天都會更新,於是萌生出了用no ...
  • [1]特征 [2]子節點 [3]特性操作 [4]attributes屬性 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...