UI 页面代码
<LinearLayout 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:orientation="vertical"
tools:context="com.chujingdai.s01_e12.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="猜拳游戏"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:orientation="vertical"
>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/ic_launcher"
/>
<RadioGroup
android:id="@+id/rp1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<RadioButton
android:id="@+id/r1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="石头"
/>
<RadioButton
android:id="@+id/r2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="剪刀"
/>
<RadioButton
android:id="@+id/r3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="布"
/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:orientation="vertical"
>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/ic_launcher"
/>
<RadioGroup
android:id="@+id/rp2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<RadioButton
android:id="@+id/r4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="石头"
/>
<RadioButton
android:id="@+id/r5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="剪刀"
/>
<RadioButton
android:id="@+id/r6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="布"
/>
</RadioGroup>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/bt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="确定"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="5dp"
android:text="测试结果"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/res"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="5dp"
/>
</LinearLayout>
</LinearLayout>
//后台代码
package com.chujingdai.s01_e12;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
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;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private String res1;
private String res2;
private int rs1;
private int rs2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioGroup Rp1=new RadioGroup(this);
RadioGroup Rp2=new RadioGroup(this);
Button btn=new Button(this);
Rp1=(RadioGroup) findViewById(R.id.rp1);
Rp2=(RadioGroup) findViewById(R.id.rp2);
btn=(Button) findViewById(R.id.bt);
final TextView res=(TextView) findViewById(R.id.res);
//设置GroupRadio监听器
Rp1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
//通过RadioGroup 读取该组下面的RadioButton TextView 下的数据
RadioButton rbi= (RadioButton) findViewById(group.getCheckedRadioButtonId());
//System.out.println(rbi.getText().toString());
res1=rbi.getText().toString();
if(res1.equals("石头")){
rs1=3;
}else if(res1.equals("剪刀")){
rs1=2;
}else if(res1.equals("布")){
rs1=1;
}
}
});
Rp2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
RadioButton rbi1= (RadioButton) findViewById(group.getCheckedRadioButtonId());
//System.out.println(rbi.getText().toString());
//用equals来比较字符串大小
res2=rbi1.getText().toString();
if(res2.equals("石头")){
rs2=3;
}else if(res2.equals("剪刀")){
rs2=2;
}else if(res2.equals("布")){
rs2=1;
}
}
});
//设置button监听器
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(rs1==rs2){
res.setText("平局");
}else if(rs1==3 && rs2==1){
res.setText("右方胜出");
}else if(rs1==1 && rs2==3){
res.setText("左方胜出");
}else if(rs1>rs2){
res.setText("左方胜出");
}else if(rs2>rs1){
res.setText("右方胜出");
}
}
});
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}