Android猜拳游戏

第一个页面布局代码:

<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/text"
        android:layout_below="@+id/text"
        android:layout_marginTop="25dp" >

        <RadioButton
            android:id="@+id/shitou"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/shitou" />

        <RadioButton
            android:id="@+id/bu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/bu" />

        <RadioButton
            android:id="@+id/jiandao"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/jiandao" />
    </RadioGroup>

    <Button
        android:id="@+id/chuquan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="15dp"
        android:layout_toRightOf="@+id/radioGroup1"
        android:text="@string/chuquan" />

</RelativeLayout>


第二个页面布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView
        android:id="@+id/showText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />
    
   <Button
        android:id="@+id/jixu"
        android:layout_width="156dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="50dp"
        android:text="@string/jixu" />

</LinearLayout>

strings布局:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">猜拳游戏</string>
    <string name="action_settings">Settings</string>
    <string name="text">请选择你要出的出拳</string>
    <string name="chuquan">出拳</string>
    <string name="shitou">石头</string>
    <string name="bu">布</string>
    <string name="jiandao">剪刀</string>
    <string name="jixu">返回继续</string>

</resources>


注:在AndroidManifest.xml中添加Activity:

<activity
            android:name=".ShowActivity"
            android:label="@string/app_name" >

        </activity>


 


实现代码:

MainActivity:

package bzu.wwj.guess;

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 chuquan;
	private int result;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		radioGroup=(RadioGroup) this.findViewById(R.id.radioGroup1);
		chuquan=(Button) this.findViewById(R.id.chuquan);
		chuquan.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(radioGroup.getCheckedRadioButtonId()==R.id.shitou){
					result=1;
				}else if(radioGroup.getCheckedRadioButtonId()==R.id.bu){
					result=2;
				}else{
					result=3;
				}
				Intent intent=new Intent();
				intent.setClass(MainActivity.this, ShowActivity.class);
				Bundle bundle=new Bundle();
				bundle.putInt("result", result);
				intent.putExtras(bundle);
				startActivity(intent);
				finish();
			}
		});
	}

	@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:

package bzu.wwj.guess;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ShowActivity extends Activity{
	private TextView resultText;
	private Button jixu;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_show);
		resultText=(TextView) this.findViewById(R.id.showText);
		//得到人出的拳 
		Bundle bundle=this.getIntent().getExtras();
		int result=bundle.getInt("result");
		//电脑出的拳
		int computer=(int) (Math.random()*10%3+1);

		//比较结果 得出胜负
		 String result1=compare(result ,computer);
		 resultText.setText(result1);
		 
		 jixu=(Button) this.findViewById(R.id.jixu);
		 //Button监听事件
		 jixu.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent=new Intent();
				intent.setClass(ShowActivity.this, MainActivity.class);
				startActivity(intent);
				finish();
			}
		});
	}
	
	//结果显示方法
	private String compare(int people ,int computer){
		StringBuffer sbf=new StringBuffer("结果:\n"+"人   "+compute(people)+"  VS  "+compute(computer)+"  电脑\n");
		if (people==computer) {
		sbf.append("平局,再接再励!");	
		}else if(people==3 && computer==1){
		sbf.append("电脑胜,真遗憾!");	
		}else if(people==1 && computer==3){
			sbf.append("人胜,恭喜你!");	
		}else if(people>computer){
			sbf.append("人胜,恭喜你!");	
		}else if(people<computer){
			sbf.append("电脑胜,真遗憾!");	
		}
		return sbf.toString();
	}
	
	//电脑的结果
	private String compute(int id){
		
		switch (id) {
		case 1:
			return "石头";
        case 2:
        	return "布";		
        case 3:
        	return "剪刀";
		default:
			break;
		}
		return null;
	}

}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值