效果图如下:
点击“菜单”键:
点击”关于“:
点击“出拳”:
一共建立了两个包,一个包中两个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
<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="请你选择要出的拳头" /> <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="石头"/> <RadioButton android:id = "@+id/jianzi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="剪子"/> <RadioButton android:id="@+id/bu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="布"/> </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="出拳"/></RelativeLayout>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
<?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>
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 自定义对话框布局文件
<?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>