android猜拳游戏简易版
。。
选好你要出的,之后结果显示为
电脑出什么是随机的。。。。
下面是代码部分。
1.布局:
01.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
02. xmlns:tools="http://schemas.android.com/tools"
03. android:layout_width="match_parent"
04. android:layout_height="match_parent"
05. android:orientation="vertical"
06. >
07.
08. <RadioGroup
09. android:layout_width="fill_parent"
10. android:layout_height="wrap_content"
11. android:orientation="horizontal"
12. >
13.
14. <RadioButton
15. android:id="@+id/shitou"
16. android:layout_width="wrap_content"
17. android:layout_height="wrap_content"
18. android:text="石头"
19. android:checked="true"
20. />
21.
22. <RadioButton
23. android:id="@+id/jiandao"
24. android:layout_width="wrap_content"
25. android:layout_height="wrap_content"
26. android:text="剪刀"
27. />
28. <RadioButton
29. android:id="@+id/bu"
30. android:layout_width="wrap_content"
31. android:layout_height="wrap_content"
32. android:text="布"
33. />
34. </RadioGroup>
35. <Button
36. android:id="@+id/chuquan"
37. android:layout_width="wrap_content"
38. android:layout_height="wrap_content"
39. android:text="出拳"
40. />
41.</LinearLayout>
2.Activity 层
[java] view plaincopyprint?
01.RadioButton shitou,jiandao,bu;
02.Button chuquan;
03.int user,diannao;
04.String [] xianshi = new String[3]{"剪刀","石头","布"};
05.Random random = new Random();
06. @Override
07. public void onCreate(Bundle savedInstanceState) {
08. super.onCreate(savedInstanceState);
09. setContentView(R.layout.activity_main);
10. shitou = (RadioButton) findViewById(R.id.shitou);
11. jiandao = (RadioButton) findViewById(R.id.jiandao);
12. bu = (RadioButton) findViewById(R.id.bu);
13. chuquan = (Button) findViewById(R.id.chuquan);
14. chuquan.setOnClickListener(new OnClickListener() {
15. @Override
16. public void onClick(View v) {
17. diannao = Math.abs(random.nextInt() % 3);
18. user = 1;
19. String userxinxi = null;
20. if(shitou.isChecked()){
21. userxinxi = "石头";
25. }else if (jiandao.isChecked()) {
26.
29. userxinxi = "剪刀";
30. }else if (bu.isChecked()) {
31.
34. userxinxi = "布";
35. }
36.
37. Intent intent = new Intent(MainActivity.this,Result.class);
38. intent.setFlags(RESULT_CANCELED);//设置自动刷新
39. if(diannao > user){
40. intent.putExtra("result", "玩家:"+userxinxi+" vs "+"电脑:"+xianshi[diannao] + "\n电脑赢");
41. }else if(diannao == user){
42. intent.putExtra("result", "玩家:"+userxinxi+" vs "+"电脑:"+xianshi[diannao] + "\n双方是平手");
43. }else if(diannao < user){
44. intent.putExtra("result", "玩家:"+userxinxi+" vs "+"电脑:"+xianshi[diannao] + "\n玩家赢");
45. }
46. startActivity(intent);
47. }
48. });
49. }
3.显示结果
01.Intent intent = getIntent();
02. String result = intent.getStringExtra("result");
03. TextView jieguo = (TextView) findViewById(R.id.jieguo);
04. jieguo.setText(result);
四.
有兴趣的同学可以研究一下图形化界面,挺好的挺管用的,也有利于的对android的研究。