一、实验要求
实现一个足球调查表,第一题答案为巴西,第二题答案为法国国家队和巴西国家队,每题1分
二、让我们先来看一下运行结果,方便我们清楚实验需求
三、源码分析(见注释)
2.1 布局文件(这里不用@string封装数据了,方便读者阅读)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:padding="10px">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="球王'贝利'是哪个国家的人:"
android:textSize="26sp"/>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/rd_baxi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="巴西"
android:textSize="20sp"/>
<RadioButton
android:id="@+id/rd_deguo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="德国"
android:textSize="20sp"/>
<RadioButton
android:id="@+id/rd_meiguo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="美国"
android:textSize="20sp"/>
<RadioButton
android:id="@+id/rd_faguo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="法国"
android:textSize="20sp"/>
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下列球队中,哪些队获得过世界杯冠军:"
android:textSize="26sp"
android:layout_marginTop="10px"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/ch_faguo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="法国国家队"/>
<CheckBox
android:id="@+id/ch_zhongguo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="中国国家队"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/ch_baxi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="巴西国家队"/>
<CheckBox
android:id="@+id/ch_meiguo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="美国国家队"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10px">
<Button
android:id="@+id/btn_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26sp"
android:text="提交" />
<Button
android:id="@+id/btn_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26sp"
android:text="重置" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="10px">
<TextView
android:id="@+id/msg_score"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="20sp"/>
<TextView
android:id="@+id/msg_one"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="20sp"/>
<TextView
android:id="@+id/msg_two"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="20sp"/>
<TextView
android:id="@+id/msg_hide"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="20sp"/>
</LinearLayout>
</LinearLayout>
2.2控制文件
package hp480.example.football;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
RadioButton r1,r2,r3,r4,rb;
TextView msg_score,msg_one,msg_two,msg_hide;
CheckBox ch1,ch2,ch3,ch4;
int score=0;
Button submit,reset;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
msg_score=(TextView)findViewById(R.id.msg_score);
msg_one=(TextView)findViewById(R.id.msg_one);
msg_two=(TextView)findViewById(R.id.msg_two);
msg_hide=(TextView)findViewById(R.id.msg_hide);
r1=(RadioButton) findViewById(R.id.rd_baxi);
r2=(RadioButton)findViewById(R.id.rd_deguo);
r3=(RadioButton)findViewById(R.id.rd_meiguo);
r4=(RadioButton)findViewById(R.id.rd_faguo);
ch1=(CheckBox)findViewById(R.id.ch_faguo);
ch2=(CheckBox)findViewById(R.id.ch_zhongguo);
ch3=(CheckBox)findViewById(R.id.ch_baxi);
ch4=(CheckBox)findViewById(R.id.ch_meiguo);
submit=(Button)findViewById(R.id.btn_submit);
reset=(Button)findViewById(R.id.btn_reset);
submit.setOnClickListener(new click1());
reset.setOnClickListener(new click2());
}
class click1 implements View.OnClickListener{
@Override
public void onClick(View v){
//处理单选框
msg_score.setText("你的得分:");
msg_one.setText("你提交的答案一:");
msg_two.setText("你提交的答案二:");
msg_hide.setText("你提交的隐藏信息:");
if(r1.isChecked()){
score++;
msg_one.append(r1.getText()+" ");
}else if(r2.isChecked()){
msg_one.append(r2.getText()+" ");
}else if(r3.isChecked()){
msg_one.append(r3.getText()+" ");
}else if(r4.isChecked()){
msg_one.append(r4.getText());
}
// 处理复选框
if(ch1.isChecked()&&ch3.isChecked()&&!ch2.isChecked()&&!ch4.isChecked()){
score++;
}
if(ch1.isChecked()){
msg_two.append(ch1.getText()+" ");
}
if(ch2.isChecked()){
msg_two.append(ch2.getText()+" ");
}
if(ch3.isChecked()){
msg_two.append(ch3.getText()+" ");
}
if(ch4.isChecked()){
msg_two.append(ch4.getText()+" ");
}
// 处理分数
msg_score.append(String.valueOf(score));
// 处理隐藏框
msg_hide.append("喜欢世界杯!");
score=0;
}
}
class click2 implements View.OnClickListener{
public void onClick(View v){
//处理单选框
if(r1.isChecked()){
r1.setChecked(false);
}else if(r2.isChecked()){
r2.setChecked(false);
}else if(r3.isChecked()){
r3.setChecked(false);
}else if(r4.isChecked()){
r4.setChecked(false);
}
//处理复选框
ch1.setChecked(false);
ch2.setChecked(false);
ch3.setChecked(false);
ch4.setChecked(false);
//处理文本框
msg_score.setText("");
msg_one.setText("");
msg_two.setText("");
msg_hide.setText("");
//处理分数
score=0;
}
}
}