android划拳游戏代码,学习Android之第四个小程序划拳(代码实现MENU、自定义对话框)...

学习Android之第四个小程序猜拳(代码实现MENU、自定义对话框)

效果图如下:

212730278.png

点击“菜单”键:

212730279.png

点击”关于“:

212730280.png

点击“出拳”:

212730281.png

一共建立了两个包,一个包中两个activity,另一个包中computer类,people类。

此程序主要穿插自定义对话框和代码实现菜单知识。

代码如下:

MainAcitity.java

package cn.edu.bzu.caiquan.activity;

import cn.edu.bzu.test.R;

import android.os.Bundle;

import android.app.Activity;

import android.app.AlertDialog;

import android.content.DialogInterface;

import android.content.Intent;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.*;

public class MainActivity extends Activity {

public static final int MENU_ABOUT=1; //关于菜单

public static final int MENU_EXIT =2; //关于菜单

RadioButton shitou;

RadioButton jianzi;

RadioButton bu;

Button chuquan;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

init();

}

public void init(){

shitou = (RadioButton)findViewById(R.id.shitou);

jianzi = (RadioButton)findViewById(R.id.jianzi);

bu = (RadioButton)findViewById(R.id.bu);

chuquan = (Button)findViewById(R.id.dongzuo);

chuquan.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {

int i;

if(shitou.isChecked()) {

i = 1;

}else if(jianzi.isChecked()){

i = 2;

}else{

i = 3;

}

Intent intent = new Intent();

intent.putExtra("people", i);

intent.setClass(MainActivity.this,SecondAcitity.class );

intent.putExtras(intent);

startActivity(intent);

}

});

}

/**

* 使用自定义对话框

*/

@Override

public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {

case MENU_ABOUT:

LayoutInflater layoutInflater = getLayoutInflater();

View view = layoutInflater.inflate(R.layout.dialog_item, null);

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("关于")

.setView(view)

.setPositiveButton("确定", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {

dialog.dismiss();

}

}).show();

break;

case MENU_EXIT:

MainActivity.this.finish();

break;

}

return super.onOptionsItemSelected(item);

}

/**

* 代码实现菜单

*/

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

menu.add(0, MENU_ABOUT, 1, "关于");

menu.add(0, MENU_EXIT, 2, "退出");

return true;

}

}

activity_main.xml

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

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="请你选择要出的拳头" />

android:id="@+id/chuquan"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginTop="30dp"

android:orientation="horizontal">

android:id="@+id/shitou"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="石头"/>

android:id = "@+id/jianzi"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="剪子"/>

android:id="@+id/bu"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="布"/>

android:id="@+id/dongzuo"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="120dp"

android:layout_centerHorizontal="true"

android:text="出拳"/>

SecondAcitivity.java

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();

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平");

}

}

}

second_acitity.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/TV1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"/>

Computer.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;

}

}

People.java

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;

}

}

second_acitity.xml  自定义对话框布局文件

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:src="@drawable/tupian"/>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginTop="40dp"

android:textSize="30sp"

android:text="作者:XX"/>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textSize="30sp"

android:text="版本:1.0.0.10"/>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值