Android学习开始
主要应用RadioGroup和button的OnClickListener事件,实现猜拳双方自动判断输赢。
效果如下:
代码如下:
xml
<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=".MainActivity" >
<!-- 梦工厂 -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="猜拳游戏"
android:textSize="30dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
//first
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="250dp"
android:orientation="vertical"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/xb1"
android:layout_gravity="center"
android:layout_weight="1"
/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:layout_weight="4">
<RadioButton
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="石头"
android:textSize="30dp"
android:layout_weight="1"/>
<RadioButton
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="剪刀"
android:textSize="30dp"
android:layout_weight="1"/>
<RadioButton
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="布"
android:textSize="30dp"
android:layout_weight="1"/>
</RadioGroup>
</LinearLayout>
//second
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="250dp"
android:orientation="vertical"
android:layout_weight="1" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/raider2"
android:layout_gravity="center"
android:layout_weight="1"
/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:layout_weight="4">
<RadioButton
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="石头"
android:textSize="30dp"
android:layout_weight="1"/>
<RadioButton
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="剪刀"
android:textSize="30dp"
android:layout_weight="1"/>
<RadioButton
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="布"
android:textSize="30dp"
android:layout_weight="1"/>
</RadioGroup>
</LinearLayout>
</LinearLayout>
<!--点击Button,进行比赛 -->
<Button
android:id="@+id/button10"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="Go!!!"
/>
<!--TextView显示双方的结果 -->
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="输赢"
/>
</LinearLayout>
public class MainActivity extends Activity {
private RadioButton button1,button2,button3,button4,button5,button6;
private Button go;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inint();//初始化
setListener();//设置Button的点击事件监听器
}
private void inint() {
// TODO Auto-generated method stub
textView = (TextView)findViewById(R.id.textView);
button1 = (RadioButton)findViewById(R.id.button1);
button2 = (RadioButton)findViewById(R.id.button2);
button3 = (RadioButton)findViewById(R.id.button3);
button4 = (RadioButton)findViewById(R.id.button4);
button5 = (RadioButton)findViewById(R.id.button5);
button6 = (RadioButton)findViewById(R.id.button6);
go = (Button)findViewById(R.id.button10);
}
private void setListener() {
// TODO Auto-generated method stub
go.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
begin();
win();
}});}
//判断胜利
private void win() {
// TODO Auto-generated method stub
if(button1.isChecked()==true&&button4.isChecked()==true||button2.isChecked()==true&&button5.isChecked()==true||button3.isChecked()==true&&button6.isChecked()==true)
textView.setText("平局");
if(button1.isChecked()==true&&button5.isChecked()==true||button2.isChecked()==true&&button6.isChecked()==true||button3.isChecked()==true&&button4.isChecked()==true)
textView.setText("甲方胜利!");
if(button1.isChecked()==true&&button6.isChecked()==true||button2.isChecked()==true&&button4.isChecked()==true||button3.isChecked()==true&&button5.isChecked()==true)
textView.setText("乙方胜利!");
}
//产生随机数,分配石头剪刀布的显示
private void begin() {
// TODO Auto-generated method stub
textView.setText("结果");
int a1 = (int)((Math.random())*3);
int a2 = (int)((Math.random())*3);
switch(a1){
case 1:
button1.setChecked(true);
break;
case 2:
button2.setChecked(true);
break;
case 0:
button3.setChecked(true);
break;
}
switch(a2){
case 1:
button4.setChecked(true);
break;
case 2:
button5.setChecked(true);
break;
case 0:
button6.setChecked(true);
break;
}
}
@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;
}
}