![](https://img-my.csdn.net/uploads/201304/18/1366283562_2722.png)
![](https://img-my.csdn.net/uploads/201304/18/1366283569_1912.png)
MainActivity布局:
<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:background="@drawable/a"
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/tv00"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/tv00"
android:textSize="18sp" />
<RadioGroup
android:id="@+id/rg00"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/tv00"
android:layout_below="@id/tv00"
android:layout_marginTop="30dp"
android:orientation="horizontal"
android:textSize="18sp" >
<RadioButton
android:id="@+id/radio00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/radio00" />
<RadioButton
android:id="@+id/radio01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio01" />
<RadioButton
android:id="@+id/radio02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio02" />
</RadioGroup>
<Button
android:id="@+id/button00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/rg00"
android:layout_below="@+id/rg00"
android:layout_marginTop="22dp"
android:text="@string/button00"
android:textSize="18sp" />
</RelativeLayout>
ShowActivity布局:
<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:background="@drawable/c"
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=".ShowActivity" >
<TextView
android:id="@+id/tv10"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
MainActivity.java代码
package bzu.gezi.com.example.fingerguessinggame;
import java.util.Random;
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.RadioButton;*/
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MainActivity extends Activity {
private RadioGroup rg00;
/*private RadioButton rb00,rb01,rb02;*/
private Button button00; private String pcfinger;
private String finger="石头";
private int i;
private String winner;
private Random random;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
random=new Random();
rg00.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.radio00:
finger="石头";
break;
case R.id.radio01:
finger="剪刀";
break;
case R.id.radio02:
finger="布";
break;
}
}
});
button00.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
intentActivity();
}
});
}
public void intentActivity(){
i=random.nextInt(3)+1;
switch (i) {
case 1:
pcfinger="石头";
if(finger=="石头"){winner="两者都";}
else if(finger=="剪刀"){winner="电脑";}
else{winner="玩家";}
break;
case 2:
pcfinger="剪刀";
if(finger=="石头"){winner="玩家";}
else if(finger=="剪刀"){winner="两者都";}
else{winner="电脑";}
break;
case 3:
pcfinger="布";
if(finger=="石头"){winner="电脑";}
else if(finger=="剪刀"){winner="玩家";}
else{winner="两者都";}
break;
}
Intent intent=new Intent();
Bundle bundle=new Bundle();
bundle.putString("pcfinger",pcfinger);
bundle.putString("finger",finger);
bundle.putString("winner",winner);
bundle.putInt("i",i);
intent.putExtras(bundle);
intent.setClass(MainActivity.this,ShowActivity.class);
startActivity(intent);
}
public void init(){
rg00=(RadioGroup) findViewById(R.id.rg00);
/*rb00=(RadioButton) findViewById(R.id.radio00);
rb01=(RadioButton) findViewById(R.id.radio01);
rb02=(RadioButton) findViewById(R.id.radio02);*/
button00=(Button) findViewById(R.id.button00);
}
@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;
}
}
MainActivity.java代码
package bzu.gezi.com.example.fingerguessinggame;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;
public class ShowActivity extends Activity {
private TextView tv10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
tv10=(TextView) this.findViewById(R.id.tv10);
Intent intent=new Intent();
Bundle bundle=new Bundle();
bundle=getIntent().getExtras();
String pcfinger=bundle.getString("pcfinger");
String finger=bundle.getString("finger");
String winner=bundle.getString("winner");
int i=bundle.getInt("i");
String str="玩家:"+finger+" vs 电脑:"+pcfinger+"\n"+winner+" 赢";
tv10.setText(str);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.show, menu);
return true;
}
}