1、猜拳小游戏页面布置:activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Tishi1" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkedButton="@+id/shitou"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/shitou"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="石头" />
<RadioButton
android:id="@+id/jiandao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="剪刀" />
<RadioButton
android:id="@+id/bu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="布" />
</RadioGroup>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="出拳" />
执行程序页面显示如下:
</LinearLayout>
返回结果的页面布局resultstyle.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">
<TextView
android:id="@+id/res"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25px"
/>
</LinearLayout>
执行程序页面显示如下:
读取传递参数主要代码MainActivity:
package com.game.zzw;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private RadioButton rdoST, rdoJD, rdoB;
private Button btnSend;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置当前的布局视图
setContentView(R.layout.activity_main);
rdoST = (RadioButton) findViewById(R.id.shitou);
rdoJD = (RadioButton) findViewById(R.id.jiandao);
rdoB = (RadioButton) findViewById(R.id.bu);
btnSend = (Button) findViewById(R.id.button1);
btnSend.setOnClickListener(new ViewBtnOcl());
}
class ViewBtnOcl implements View.OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
String ren = "0";
if (rdoST.isChecked()) {
ren = "1";
} else if (rdoJD.isChecked()) {
ren = "2";
} else {
ren = "3";
}
Intent intent = new Intent(MainActivity.this,
ResultActivity.class);
intent.putExtra("ren", ren);
startActivity(intent);
break;
}
}
}
}
结果比较返回值主要代码ResulActivity:
package com.game.zzw;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class ResultActivity extends Activity {
private TextView myTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.resultstyle);
myTextView = (TextView) findViewById(R.id.res);
Intent intent = getIntent();
int ren = Integer.parseInt(intent.getStringExtra("ren"));
// 业务逻辑判断
// 自动生成npc产生拳的代码(1~3)
int npc = new Random().nextInt(3) + 1;
// 处理请求数据并进行逻辑判断,将结果返回
String res = "";
if (ren == npc) {
res = "平局,再接再厉";
} else if (ren == 3 && npc == 1) {
res = "恭喜,您获胜了……";
} else if (ren == 1 && npc == 3) {
res = "虽败犹荣……再来一局";
} else if (ren < npc) {
res = "恭喜,您获胜了……";
} else {
res = "虽败犹荣……再来一局";
}
String msg = "您:" + change(ren) + " vs " + "电脑:" + change(npc)
+ "\n您" + res;
myTextView.setText(msg);
}
// 将数字转换成汉字
private String change(int r) {
switch (r) {
case 1:
return "石头";
case 2:
return "剪刀";
case 3:
return "布";
}
return null;
}
}