猜拳小游戏

    猜拳小游戏

    根据以前学的知识,我们首先分析一下猜拳小游戏的布局! 和所需要的控件!以前学的页面跳转。

效果图:


一. 我们不用硬编码的方式,我们在res文件夹values文件夹里面的string.xml。

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <string name="app_name">caiquan</string>  
  5.     <string name="action_settings">Settings</string>  
  6.     <string name="hello_world">Hello world!</string>  
  7.     <string name="lable_main">请选择你要出的拳:</string>  
  8.     <string name="ra_shitou">石头</string>  
  9.     <string name="ra_jiandao">剪刀</string>  
  10.     <string name="ra_bu"></string>  
  11.     <string name="button_main">出拳</string>  
  12.     <string name="title_activity_show">ShowActivity</string>  
  13.   
  14. </resources>  

二. 然后再res文件夹中layout文件里面activity_main.xml定义布局!强调一点(RadioGroup控件把RadioButton单选框包围起来,意思就是把RadioButton单选框控件实现出来!如果不用RadioGroup控件包围RadioButton单选框,那RadioButton单选框无法实现单选效果!就变成多选了)。

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.     <TextView  
  7.         android:id="@+id/BT"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/lable_main" />  
  11. <RadioGroup   
  12.         android:id="@+id/radiogroupe"  
  13.         android:layout_width="fill_parent"  
  14.         android:layout_height="wrap_content"  
  15.         >  
  16.     <RadioButton  
  17.         android:id="@+id/BU"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:layout_alignLeft="@+id/CQ"  
  21.         android:layout_below="@+id/JD"  
  22.         android:layout_marginTop="24dp"  
  23.         android:text="@string/ra_bu" />  
  24.   
  25.     <RadioButton  
  26.         android:id="@+id/JD"  
  27.         android:layout_width="wrap_content"  
  28.         android:layout_height="wrap_content"  
  29.         android:layout_alignLeft="@+id/BU"  
  30.         android:layout_below="@+id/ST"  
  31.         android:layout_marginTop="16dp"  
  32.         android:text="@string/ra_jiandao" />  
  33.   
  34.     <RadioButton  
  35.         android:id="@+id/ST"  
  36.         android:layout_width="wrap_content"  
  37.         android:layout_height="wrap_content"  
  38.         android:layout_alignLeft="@+id/BT"  
  39.         android:layout_below="@+id/BT"  
  40.         android:text="@string/ra_shitou" />  
  41.  </RadioGroup>  
  42.     <Button  
  43.         android:id="@+id/CQ"  
  44.         android:layout_width="fill_parent"  
  45.         android:layout_height="wrap_content"  
  46.         android:layout_alignLeft="@+id/ST"  
  47.         android:layout_below="@+id/BU"  
  48.         android:layout_marginTop="14dp"  
  49.         android:text="@string/button_main" />  
  50.   
  51. </LinearLayout>  
三. 然后再res文件夹中layout文件里面activity_show.xml定义布局!这个布局页面主要是显示从activity_main 里面的数据传过来并接受显示在activity_show 的标签上。

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <LinearLayout 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.     >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/show"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content" />  
  11.     </LinearLayout>  
  12.           
四. 在MainActivity.java里面获取布局页面里面的ID地址,并定义属性。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package edu.bzu.caiquan;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.content.Intent;  
  6. import android.view.Menu;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.RadioGroup;  
  11.   
  12. public class MainActivity extends Activity {  
  13.     private RadioGroup radioGroup;  
  14.     private Button button;  
  15.       
  16.     private OnClickListener listener=new OnClickListener() {  
  17.           
  18.         @Override  
  19.         public void onClick(View v) {  
  20.             // TODO Auto-generated method stub  
  21.             switch(v.getId()){  
  22.             case R.id.CQ:  
  23.                 int id=radioGroup.getCheckedRadioButtonId();  
  24.                 int quan=0;  
  25.                 switch(id){  
  26.                 case R.id.ST:  
  27.                     quan=1;  
  28.                     break;  
  29.                     case R.id.JD:  
  30.                         quan=2;  
  31.                         break;  
  32.                         case R.id.BU:  
  33.                             quan=3;  
  34.                             break;  
  35.                             default:  
  36.                             break;  
  37.                 }  
  38.            Intent intent = new Intent();  
  39.                 intent.setClass(MainActivity.this, ShowActivity.class);  
  40.                 intent.putExtra("quan", quan);  
  41.                 startActivity(intent);  
  42.                 break;  
  43.               
  44.             default:  
  45.                 break;        
  46.             }  
  47.               
  48.         }  
  49.     };  
  50.     @Override  
  51.     protected void onCreate(Bundle savedInstanceState) {  
  52.         super.onCreate(savedInstanceState);  
  53.         setContentView(R.layout.activity_main);  
  54.       
  55.         radioGroup=(RadioGroup)findViewById(R.id.radiogroupe);  
  56.         button=(Button)findViewById(R.id.CQ);  
  57.         button.setOnClickListener(listener);  
  58.           
  59.           
  60.     }  
  61.   
  62.     @Override  
  63.     public boolean onCreateOptionsMenu(Menu menu) {  
  64.         // Inflate the menu; this adds items to the action bar if it is present.  
  65.         getMenuInflater().inflate(R.menu.main, menu);  
  66.         return true;  
  67.     }  
  68.   
  69. }  

五. 在ShowActivity.java里面计算人和电脑随机出拳,谁是赢家!

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package edu.bzu.caiquan;  
  2.   
  3. import java.util.Random;  
  4. import android.os.Bundle;  
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.view.Menu;  
  8. import android.widget.TextView;  
  9.   
  10. public class ShowActivity extends Activity {  
  11.     private TextView tView;  
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_show);       
  16.         tView=(TextView) findViewById(R.id.show);  
  17.           
  18.         //得到人出的拳   
  19.         Intent intent=getIntent();  
  20.         Bundle bundle=intent.getExtras();  
  21.          int ren=bundle.getInt("quan");  
  22.            
  23.         //电脑出的拳  
  24.          Random random=new Random();  
  25.          int cpu=random.nextInt(3-1)+1;  
  26.          //比较结果 得出胜负  
  27.          String show=compare(ren,cpu);  
  28.          tView.setText(show);  
  29.     }  
  30.   
  31.     private String compare(int ren, int cpu) {  
  32.         // TODO Auto-generated method stub    
  33.         StringBuffer sbf=new StringBuffer("结果:\n"+"人   "+convert(ren)+"  VS  "+convert(cpu)+"  电脑\n");  
  34.         if (ren==cpu) {  
  35.         sbf.append("平局,再接再励!");   
  36.         }else if(ren==1 && cpu==3){  
  37.         sbf.append("电脑胜,很遗憾!下次努力!");      
  38.         }else if(ren==3 && cpu==1){  
  39.             sbf.append("人胜,继续努力!");   
  40.         }else if(ren<cpu){  
  41.             sbf.append("人胜,继续努力!");   
  42.         }else if(ren>cpu){  
  43.             sbf.append("电脑胜,很遗憾!下次努力!");      
  44.         }  
  45.         return sbf.toString();  
  46.     }  
  47.       
  48.     private String convert(int id){  
  49.           
  50.         switch (id) {  
  51.         case 1:  
  52.             return "石头";  
  53.         case 2:  
  54.             return "剪刀";          
  55.         case 3:  
  56.             return "布";  
  57.         default:  
  58.             break;  
  59.         }  
  60.         return null;  
  61.     }  
  62.   
  63.     @Override  
  64.     public boolean onCreateOptionsMenu(Menu menu) {  
  65.         // Inflate the menu; this adds items to the action bar if it is present.  
  66.         getMenuInflater().inflate(R.menu.show, menu);  
  67.         return true;  
  68.     }  
  69.   
  70. }  


六. AndroidManifest.xml.声明

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="edu.bzu.caiquan"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="18" />  
  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="edu.bzu.caiquan.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="edu.bzu.caiquan.ShowActivity"  
  27.             android:label="@string/title_activity_show" >  
  28.         </activity>  
  29.     </application>  
  30.   
  31. </manifest>  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值