Android程序——人机猜拳

Android程序——人机猜拳_第1张图片

一.MainActivity.java和SecondActivity.java

package cn.edu.bzu.grid.activity;




import com.example.grid.R;

import cn.edu.bzu.grid.model.ImageAdapter;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;

public class MainActivity extends Activity {

	GridView gridView;
	ImageView imageView;
	
	private Integer[] integer={
			R.drawable.a1,
			R.drawable.a2,
			R.drawable.a3,
			R.drawable.a4,
			R.drawable.a5,
			R.drawable.a6,
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		gridView = (GridView)findViewById(R.id.GV01);
		imageView = (ImageView)findViewById(R.id.IV01);
		
		
		gridView.setAdapter(new ImageAdapter(this));
		//gridView.setBackgroundResource(R.drawable.ic_launcher);
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				imageView.setImageResource(integer[arg2]);
			}
		});
		
	}

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

}
 
 
package cn.edu.bzu.caiquan.activity;

import cn.edu.bzu.caiquan.lei.Computer;
import cn.edu.bzu.caiquan.lei.People;
import cn.edu.bzu.test.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class SecondAcitity extends Activity{
	
	TextView textView;
	
	 @Override
	    protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.second_acitity);
	        
	        textView = (TextView)findViewById(R.id.TV1);
	        
	        Bundle bundle = this.getIntent().getExtras(); //接受传来的值
	        int chuquan = bundle.getInt("people");
	        
	        People people = new People(chuquan);
	        Computer computer = new Computer();
	        computer.setChuQuFanfShi( computer.chuquan());
	        
	       /**
	        * 为使代码更简洁,可以将下列代码抽出。
	        */
	        
	        int peo = people.getChuQuFanfShi();
	        int com = computer.getChuQuFanfShi();
	        System.out.println("peo="+peo+"  com="+com);
	    	if(peo==1&&com==2||peo==2&&com==3||peo==3&&com==1){
	    		if(peo==1){
	    			textView.setText("玩家:石头"+"VS"+"电脑:剪子");
	    		}
	    		if(peo==2){
	    			textView.setText("玩家:剪子"+"VS"+"电脑:布");
	    		}
	    		if(peo==3){
	    			textView.setText("玩家:布"+"VS"+"电脑:石头");
	    		}
	    		textView.append("\n赢");
			}else if(com==1&&peo==2||com==2&&peo==3||com==3&&peo==1){
				if(peo==1){
	    			textView.setText("玩家:石头"+"VS"+"电脑:布");
	    		}
	    		if(peo==2){
	    			textView.setText("玩家:剪子"+"VS"+"电脑:石头");
	    		}
	    		if(peo==3){
	    			textView.setText("玩家:布"+"VS"+"电脑:剪子");
	    		}
	    		textView.append("\n输");
				
				
			}else if(com==peo||com==peo||com==peo){
				if(peo==1){
	    			textView.setText("玩家:石头"+"VS"+"电脑:石头");
	    		}
	    		if(peo==2){
	    			textView.setText("玩家:剪子"+"VS"+"电脑:剪子");
	    		}
	    		if(peo==3){
	    			textView.setText("玩家:布"+"VS"+"电脑:布");
	    		}
	    		textView.append("\n平");
				
			}
       
	        
	    }

}

二.computer.java和people.java

package cn.edu.bzu.caiquan.lei;

import java.util.Random;

public class Computer {
	
	private int chuQuFanfShi;
	
	public int getChuQuFanfShi() {
		return chuQuFanfShi;
	}

	public void setChuQuFanfShi(int chuQuFanfShi) {
		this.chuQuFanfShi = chuQuFanfShi;
	}

	public int chuquan(){
		
		Random random = new Random();	
		return  random.nextInt(3)+1;
	}

}



package cn.edu.bzu.caiquan.lei;


public class People {
	
	private int chuQuFanfShi;
	
	public People(int chuQuFanfShi){
		this.chuQuFanfShi = chuQuFanfShi;
		
	}
	
	
	public int getChuQuFanfShi() {
		return chuQuFanfShi;
	}

	public void setChuQuFanfShi(int chuQuFanfShi) {
		this.chuQuFanfShi = chuQuFanfShi;
	}
	
	
	



三.布局文件
(1)<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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/moshi" />
    <RadioGroup
        android:id="@+id/chuquan"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:layout_marginTop="30dp"
        android:orientation="horizontal">
        <RadioButton 
            android:id="@+id/shitou"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/shitou"
            android:typeface="serif"
            android:textSize="18sp"
            android:textColor="#001122"/>
        <RadioButton 
            android:id = "@+id/jianzi"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:typeface="sans"
            android:text="@string/jianzi"
            android:textSize="18sp"/>
        <RadioButton 
            android:id="@+id/bu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/bu"
            android:textSize="18sp"/>
    </RadioGroup>
    <Button 
        android:id="@+id/dongzuo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="120dp"
        android:layout_centerHorizontal="true"
        android:text="@string/chuquan"
        android:textSize="30sp"
        android:textColor="#00EECC"/>
</RelativeLayout>
(2)<?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" >
    
    <ImageView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/tupian"/>
    
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:textSize="30sp"
        android:text="作者:XX"/>
       <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="版本:1.0.0.10"/>
  

</LinearLayout>
(3<?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/TV1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>







   

你可能感兴趣的:(android,布局)