猜拳小游戏
根据以前学的知识,我们首先分析一下猜拳小游戏的布局! 和所需要的控件!以前学的页面跳转。
效果图:
一. 我们不用硬编码的方式,我们在res文件夹values文件夹里面的string.xml。
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="app_name">caiquan</string>
- <string name="action_settings">Settings</string>
- <string name="hello_world">Hello world!</string>
- <string name="lable_main">请选择你要出的拳:</string>
- <string name="ra_shitou">石头</string>
- <string name="ra_jiandao">剪刀</string>
- <string name="ra_bu">布</string>
- <string name="button_main">出拳</string>
- <string name="title_activity_show">ShowActivity</string>
- </resources>
二. 然后再res文件夹中layout文件里面activity_main.xml定义布局!强调一点(RadioGroup控件把RadioButton单选框包围起来,意思就是把RadioButton单选框控件实现出来!如果不用RadioGroup控件包围RadioButton单选框,那RadioButton单选框无法实现单选效果!就变成多选了)。
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/BT"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/lable_main" />
- <RadioGroup
- android:id="@+id/radiogroupe"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- >
- <RadioButton
- android:id="@+id/BU"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/CQ"
- android:layout_below="@+id/JD"
- android:layout_marginTop="24dp"
- android:text="@string/ra_bu" />
- <RadioButton
- android:id="@+id/JD"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/BU"
- android:layout_below="@+id/ST"
- android:layout_marginTop="16dp"
- android:text="@string/ra_jiandao" />
- <RadioButton
- android:id="@+id/ST"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/BT"
- android:layout_below="@+id/BT"
- android:text="@string/ra_shitou" />
- </RadioGroup>
- <Button
- android:id="@+id/CQ"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/ST"
- android:layout_below="@+id/BU"
- android:layout_marginTop="14dp"
- android:text="@string/button_main" />
- </LinearLayout>
- <LinearLayout 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"
- >
- <TextView
- android:id="@+id/show"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- </LinearLayout>
- package edu.bzu.caiquan;
- import android.os.Bundle;
- import android.app.Activity;
- import android.content.Intent;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.RadioGroup;
- public class MainActivity extends Activity {
- private RadioGroup radioGroup;
- private Button button;
- private OnClickListener listener=new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- switch(v.getId()){
- case R.id.CQ:
- int id=radioGroup.getCheckedRadioButtonId();
- int quan=0;
- switch(id){
- case R.id.ST:
- quan=1;
- break;
- case R.id.JD:
- quan=2;
- break;
- case R.id.BU:
- quan=3;
- break;
- default:
- break;
- }
- Intent intent = new Intent();
- intent.setClass(MainActivity.this, ShowActivity.class);
- intent.putExtra("quan", quan);
- startActivity(intent);
- break;
- default:
- break;
- }
- }
- };
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- radioGroup=(RadioGroup)findViewById(R.id.radiogroupe);
- button=(Button)findViewById(R.id.CQ);
- button.setOnClickListener(listener);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
五. 在ShowActivity.java里面计算人和电脑随机出拳,谁是赢家!
- package edu.bzu.caiquan;
- import java.util.Random;
- import android.os.Bundle;
- import android.app.Activity;
- import android.content.Intent;
- import android.view.Menu;
- import android.widget.TextView;
- public class ShowActivity extends Activity {
- private TextView tView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_show);
- tView=(TextView) findViewById(R.id.show);
- //得到人出的拳
- Intent intent=getIntent();
- Bundle bundle=intent.getExtras();
- int ren=bundle.getInt("quan");
- //电脑出的拳
- Random random=new Random();
- int cpu=random.nextInt(3-1)+1;
- //比较结果 得出胜负
- String show=compare(ren,cpu);
- tView.setText(show);
- }
- private String compare(int ren, int cpu) {
- // TODO Auto-generated method stub
- StringBuffer sbf=new StringBuffer("结果:\n"+"人 "+convert(ren)+" VS "+convert(cpu)+" 电脑\n");
- if (ren==cpu) {
- sbf.append("平局,再接再励!");
- }else if(ren==1 && cpu==3){
- sbf.append("电脑胜,很遗憾!下次努力!");
- }else if(ren==3 && cpu==1){
- sbf.append("人胜,继续努力!");
- }else if(ren<cpu){
- sbf.append("人胜,继续努力!");
- }else if(ren>cpu){
- sbf.append("电脑胜,很遗憾!下次努力!");
- }
- return sbf.toString();
- }
- private String convert(int id){
- switch (id) {
- case 1:
- return "石头";
- case 2:
- return "剪刀";
- case 3:
- return "布";
- default:
- break;
- }
- return null;
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.show, menu);
- return true;
- }
- }
六. AndroidManifest.xml.声明
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="edu.bzu.caiquan"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="18" />
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="edu.bzu.caiquan.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="edu.bzu.caiquan.ShowActivity"
- android:label="@string/title_activity_show" >
- </activity>
- </application>
- </manifest>