我先把图片贴上来吧。。
选好你要出的,之后结果显示为
电脑出什么是随机的。。。。
下面是代码部分。
1.布局:
<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"><RadioGroup android:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal"><RadioButton android:id="@+id/shitou"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="石头"android:checked="true"/><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/chuquan"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="出拳"/>
</LinearLayout>
2.Activity 层
RadioButton shitou,jiandao,bu;Button chuquan;int user,diannao;String [] xianshi = new String[3];Random random = new Random();@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);shitou = (RadioButton) findViewById(R.id.shitou);jiandao = (RadioButton) findViewById(R.id.jiandao);bu = (RadioButton) findViewById(R.id.bu);chuquan = (Button) findViewById(R.id.chuquan);chuquan.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {diannao = Math.abs(random.nextInt() % 3);user = 1;String userxinxi = null;if(shitou.isChecked()){ xianshi[0] = "剪刀";xianshi[1] = "石头";xianshi[2] = "布";userxinxi = "石头";}else if (jiandao.isChecked()) {xianshi[0] = "布";xianshi[1] = "剪刀";xianshi[2] = "石头";userxinxi = "剪刀";}else if (bu.isChecked()) {xianshi[0] = "石头";xianshi[1] = "布";xianshi[2] = "剪刀";userxinxi = "布";}Intent intent = new Intent(MainActivity.this,Result.class);intent.setFlags(RESULT_CANCELED);if(diannao > user){intent.putExtra("result", "玩家:"+userxinxi+" vs "+"电脑:"+xianshi[diannao] + "\n电脑赢");}else if(diannao == user){intent.putExtra("result", "玩家:"+userxinxi+" vs "+"电脑:"+xianshi[diannao] + "\n双方是平手");}else if(diannao < user){intent.putExtra("result", "玩家:"+userxinxi+" vs "+"电脑:"+xianshi[diannao] + "\n玩家赢");}startActivity(intent);}});}
3.显示结果:
Intent intent = getIntent();String result = intent.getStringExtra("result");TextView jieguo = (TextView) findViewById(R.id.jieguo);jieguo.setText(result);
好了,就这么多了。。