猜拳小游戏采用比较笨的方法编写 比较电脑跟玩家出的拳。
在其中添加了Menu组件。
具体代码如下:
Mainactivity类代码:
public class MainActivity extends Activity {
private Button clickButton;
private RadioGroup sexGroup;
private RadioButton op1;
private RadioButton op2;
private RadioButton op3;
String chu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initComponnents();
clickButton.setOnClickListener(new ClickHandler());
sexGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == op1.getId()) {
chu = "石头";
}
else if (checkedId == op2.getId()) {
chu = "剪刀";
} else if (checkedId == op3.getId()) {
chu = "布";
}
}
});
}
private void initComponnents() {
sexGroup = (RadioGroup) findViewById(R.id.op);
clickButton = (Button) this.findViewById(R.id.Button);
op1 = (RadioButton) findViewById(R.id.shitou);
op2 = (RadioButton) findViewById(R.id.jiandao);
op3 = (RadioButton) findViewById(R.id.bu);
}
private class ClickHandler implements OnClickListener {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, showActivity.class);
intent.putExtra("chu", chu);
System.out.println(chu);
startActivity(intent);
finish();
}
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
int item_id = item.getItemId();
switch (item_id) {
case R.id.about:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.ok).setMessage("猜拳小游戏\n作者:xxx")
.setPositiveButton("确定", null).create().show();
break;
case R.id.exit:
finish();
break;
}
return true;
}
}
showActivity代码如下:
public class showActivity extends Activity {
private TextView op1;
private TextView op2;
private TextView result;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initComponnents();
// 电脑随机产生0.1.2并赋值为a
String[] readomWord = { "0", "1", "2" };
int readomWordIndex = (int) (Math.random() * 3);
String a = readomWord[readomWordIndex];
System.out.println(a);
// 显示电脑出的,0表示石头,1表示剪刀,2表示布。
if (a.equals("0")) {
op1.setText("石头");
}
if (a.equals("1")) {
op1.setText("剪刀");
}
if (a.equals("2")) {
op1.setText("布");
}
Intent intent=new Intent();
intent=this.getIntent();
String n=intent.getStringExtra("chu");
//显示玩家出的拳:
if (n.equals("石头")) {
op2.setText("石头");
}
if (n.equals("剪刀")) {
op2.setText("剪刀");
}
if (n.equals("布")) {
op2.setText("布");
}
//判断输赢
if(a.equals("0")&&n.equals("剪刀")){result.setText("电脑获胜!");}
else if(a.equals("1")&&n.equals("布")){result.setText("电脑获胜!");}
else if(a.equals("2")&&n.equals("石头")){result.setText("电脑获胜!");}
else if(a.equals("1")&&n.equals("石头")){result.setText("玩家获胜!");}
else if(a.equals("0")&&n.equals("布")){result.setText("玩家获胜!");}
else if(a.equals("2")&&n.equals("剪刀")){result.setText("玩家获胜!");}
else {result.setText("平局");}
}
private void initComponnents() {
op1 = (TextView) findViewById(R.id.op1);
op2 = (TextView) findViewById(R.id.op2);
result = (TextView) findViewById(R.id.result);
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main1, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
int item_id = item.getItemId();
switch (item_id) {
case R.id.about:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.ok).setMessage("猜拳小游戏\n作者:xxx")
.setPositiveButton("确定", null).create().show();
break;
case R.id.exit:
Intent intent =new Intent();
intent.setClass(this, MainActivity.class);
startActivity(intent);
finish();
break;
}
return true;
}
}
关于Menu类:
public class Activity02 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public boolean onCreateOptionsMenu(Menu menu){
menu.add(0,0,0,R.string.ok);
menu.add(0,1,1,R.string.back);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
int item_id=item.getItemId();
switch(item_id){
case 0:
case 1:Intent intent =new Intent();
intent.setClass(Activity02.this,MainActivity.class);
startActivity(intent);
finish();
break;
}
return true;
}
}
布局文件:
activity_main:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<RadioGroup
android:id="@+id/op"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/shitou"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="16dp"
android:text="@string/shitou" />
<RadioButton
android:id="@+id/bu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:text="@string/bu" />
<RadioButton
android:id="@+id/jiandao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/jiandao" />
</RadioGroup>
<Button
android:id="@+id/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chuquan" />
</LinearLayout>
activity_main2:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/computer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/computer" />
<TextView
android:id="@+id/op1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/vs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="VS" />
<TextView
android:id="@+id/person"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/person" />
<TextView
android:id="@+id/op2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/linearLayout1"
android:layout_marginTop="16dp"
android:layout_weight="1"
android:text="输赢" />
</RelativeLayout>
Menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/about"
android:title="关于"/>
<item
android:id="@+id/exit"
android:title="退出"/>
</menu>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/about"
android:title="关于"/>
<item
android:id="@+id/exit"
android:title="返回"/>
</menu>
运行结果: