用android studio 实现上图的内容,首先是设计界面,线性布局用LinearLayout,布局中有android:orientation属性控制排列方向,horizontal为水平,vertical为垂直方向;第一题为单选题,用RadioGroup和RadioButton结合使用,第二题为多选题,使用CheckBox按钮;两个普通Button按钮;三个文本框,第一第二个显示问题,第三个显示结果。
.xml布局代码为
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tex1"
android:text="球王“贝利”是哪个国家的人:"
/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rg">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b11"
android:text="巴西"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b12"
android:text="德国"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b13"
android:text="美国"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b14"
android:text="法国"/>
</RadioGroup>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tex2"
android:text="下面足球队中,哪些队曾获得过世界杯冠军:"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b21"
android:text="法国国家队"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b22"
android:text="中国国家队"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b23"
android:text="巴西国家队"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b24"
android:text="美国国家队"
/>
<LinearLayout
android:id="@+id/pr1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ok"
android:text="提交"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/clear"
android:text="重置"
/></LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tex3"/>
</LinearLayout>
代码要实现第一题选中巴西时,分数加一,第二题选中巴西和法国,分数加二,当点击确定按钮是,显示最终得分和所选内容;当点击重置按钮是,将分数清零,所有选项回到未选中状态,结果清空。
java代码如下:
package com.example.jspexample4_6;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private RadioGroup radio;
private RadioButton but11, but12, but13, but14, butt;
private CheckBox but21, but22, but23, but24;
private Button bu1, bu2;
private TextView txt1, txt2, txt3;
String str, str1="" ,str2;
int score = 0;//计算分值
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but11 = (RadioButton) findViewById(R.id.b11);
but12 = (RadioButton) findViewById(R.id.b12);
but13 = (RadioButton) findViewById(R.id.b13);
but14 = (RadioButton) findViewById(R.id.b14);
but21 = (CheckBox) findViewById(R.id.b21);
but22 = (CheckBox) findViewById(R.id.b22);
but23 = (CheckBox) findViewById(R.id.b23);
but24 = (CheckBox) findViewById(R.id.b24);
txt1 = (TextView) findViewById(R.id.tex1);
txt2 = (TextView) findViewById(R.id.tex2);
txt3 = (TextView) findViewById(R.id.tex3);
bu1 = (Button) findViewById(R.id.ok);
bu2 = (Button) findViewById(R.id.clear);
radio = (RadioGroup) findViewById(R.id.rg);//程序组件与用户界面组件相关联
bu1.setOnClickListener(new check1());//监控点击按钮事件
bu2.setOnClickListener(new check2());//重置清空
}
class check1 implements View.OnClickListener {
public void onClick(View view) {
RadioButton radioButton = findViewById(radio.getCheckedRadioButtonId());//获取单选项中被选中的按钮id
str = radioButton.getText().toString();//得到被选中按钮的内容
if (str.equals("巴西")) { //内容是(等于)巴西
score++; //分数+1
}
if (but21.isChecked()) { //判断but21是否被选中
str1 += but21.getText() + " "; //but21的内容被加入到str1字符串中
}
if (but22.isChecked()) {
str1 += but22.getText() + " ";
}
if (but23.isChecked()) {
str1 += but23.getText() + " ";
}
if (but24.isChecked()) {
str1 += but24.getText() + " ";
}
if (str1.contains("法国") && str1.contains("巴西")) { //判断str1字符串是否包含法国和巴西
if (!str1.contains("中国") && !str1.contains("美国")) { //str1字符串不能包含中国和美国
score = score + 2; //如果仅选中法国和巴西,则分数加2
}
}
str2="你提交的隐藏信息:喜欢世界杯"; //隐藏信息,可用以统计用户喜好
txt3.setText("\n"+"你的得分是:"+score+"\n"+"你提交的答案一是:"+str+"\n"+"你提交的答案二是:"+str1+"\n"+str2);//注意分行是\n
}
}
class check2 implements View.OnClickListener {
public void onClick(View view) {
but11.setChecked(false);
but12.setChecked(false);
but13.setChecked(false);
but14.setChecked(false);
but21.setChecked(false);
but22.setChecked(false);
but23.setChecked(false);
but24.setChecked(false);//将所有选项重置为未选中状态
score=0; //分数清0
str=str1=str2=""; //字符串清空
txt3.setText(""); //结果清空
}
}
}
重点:1.获取单选题的选中项,
RadioButton radioButton = findViewById(radio.getCheckedRadioButtonId());
获取单选项的内容
String str = radioButton.getText().toString;
2.获取多选项的选中项,点击确定按钮时,触发onclick()事件,isChecked()判断选项是否被选中。
3.选项重置:.setChecked(false)未选中,.setChecked(true)选中