(一)运行效果
(二)涉及知识点
1、线性布局(LinearLayout)
2、帧式布局(FrameLayout)
3、标签(TextView)
4、视图(View)
5、按钮(Button)
6、单选按钮组(RadioGroup)
7、单选按钮(RadioButton)
8、复选框(CheckBox)
9、数组列表(ArrayList)
(三)实现步骤
1、创建安卓应用【StandardTest】
2、准备背景图片background.jpg,拷贝到mipmap目录里
3、主布局资源文件activity_main.xml
4、字符串资源文件strings.xml
标准化测试程序
正确
错误
下一题
退出
上一题
提交
5、试题实体类Question
package com.example.a0604;
/**
* Created by ASUS on 2018/12/5.
*/
public class Question {
/**
* 编号
*/
private int id;
/**
* 题干
*/
private String stem;
/**
* A选项
*/
private String a;
private String b;
private String c;
private String d;
/**
* 题型
* 1.单选题
* 2.多选题
* 3.判断题
*/
private int type;
/**
* 答案
*/
private String answer;
public int getId() {
return id;
}
public String getStem() {
return stem;
}
public String getA() {
return a;
}
public String getB() {
return b;
}
public String getC() {
return c;
}
public String getD() {
return d;
}
public int getType() {
return type;
}
public String getAnswer() {
return answer;
}
public void setId(int id) {
this.id = id;
}
public void setStem(String stem) {
this.stem = stem;
}
public void setA(String a) {
this.a = a;
}
public void setB(String b) {
this.b = b;
}
public void setC(String c) {
this.c = c;
}
public void setD(String d) {
this.d = d;
}
public void setType(int type) {
this.type = type;
}
public void setAnswer(String answer) {
this.answer = answer;
}
@Override
public String toString() {
return "Question{" +
"id=" + id +
", stem='" + stem + '\'' +
", a='" + a + '\'' +
", b='" + b + '\'' +
", c='" + c + '\'' +
", d='" + d + '\'' +
", type=" + type +
", answer='" + answer + '\'' +
'}';
}
}
6、主界面类MainActivity
package com.example.a0604;
import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends Activity {
private LinearLayout llSingleChoice;
private LinearLayout llMultipleChoice;
private LinearLayout llJudgement;
private TextView tvStem;
private RadioGroup rgSingleChoice;
private RadioButton rbSingleA;
private RadioButton rbSingleB;
private RadioButton rbSingleC;
private RadioButton rbSingleD;
private RadioGroup rgJudgement;
private RadioButton rbJudgementT;
private RadioButton rbJudgementF;
private CheckBox cbMultipleA;
private CheckBox cbMultipleB;
private CheckBox cbMultipleC;
private CheckBox cbMultipleD;
/**
* 实体集合
*/
private ArrayList questions;
private int questionIndex = 0;
//保存已经选择按钮的状态+
private String[] answers;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
llSingleChoice = findViewById(R.id.ll_single_choice);
llMultipleChoice = findViewById(R.id.ll_multiple_choice);
llJudgement = findViewById(R.id.ll_judgement);
tvStem = findViewById(R.id.tv_stem);
rgSingleChoice = findViewById(R.id.rg_single_choice);
rbSingleA = findViewById(R.id.rb_single_a);
rbSingleB = findViewById(R.id.rb_single_b);
rbSingleC = findViewById(R.id.rb_single_c);
rbSingleD = findViewById(R.id.rb_single_d);
rgJudgement = findViewById(R.id.rg_judgement);
rbJudgementT = findViewById(R.id.rb_judgement_t);
rbJudgementF = findViewById(R.id.rb_judgement_f);
cbMultipleA = findViewById(R.id.cb_multiple_a);
cbMultipleB = findViewById(R.id.cb_multiple_b);
cbMultipleC = findViewById(R.id.cb_multiple_c);
cbMultipleD = findViewById(R.id.cb_multiple_d);
// 初始化试题库
questions = getQuestions();
//实例化答案数组
answers = new String[questions.size()];
// 取出第1题
Question question = questions.get(questionIndex);
// 根据题型设置界面
setInterfaceByType(question);
}
private void setInterfaceByType(Question question) {
// 判断题型,选择界面
switch (question.getType()) {
case 1:
llSingleChoice.setVisibility(View.VISIBLE);
llMultipleChoice.setVisibility(View.INVISIBLE);
llJudgement.setVisibility(View.INVISIBLE);
tvStem.setText("[单选] " + question.getId() + ". " + question.getStem());
tvStem.setTextColor(Color.RED);
rbSingleA.setText(question.getA());
rbSingleB.setText(question.getB());
rbSingleC.setText(question.getC());
rbSingleD.setText(question.getD());
// 清空控件选中状态
//rbSingleA.setChecked(false);
//rbSingleB.setChecked(false);
//rbSingleC.setChecked(false);
//rbSingleD.setChecked(false);
//rgSingleChoice.check(-1);
String answer = answers[questionIndex];
if(answer!=null){
//根据用户但来设定单选按钮状态
switch(answers[questionIndex]){
case "A":
rbSingleA.setChecked(true);
break;
case "B":
rbSingleB.setChecked(true);
break;
case "C":
rbSingleC.setChecked(true);
break;
case "D":
rbSingleD.setChecked(true);
break;
default:
rbSingleA.setChecked(false);
rbSingleB.setChecked(false);
rbSingleC.setChecked(false);
rbSingleD.setChecked(false);
break;
}
}else{
rbSingleA.setChecked(false);
rbSingleB.setChecked(false);
rbSingleC.setChecked(false);
rbSingleD.setChecked(false);
}
break;
case 2:
llMultipleChoice.setVisibility(View.VISIBLE);
llSingleChoice.setVisibility(View.INVISIBLE);
llJudgement.setVisibility(View.INVISIBLE);
tvStem.setText("[多选] " + question.getId() + ". " + question.getStem());
tvStem.setTextColor(Color.BLACK);
cbMultipleA.setText(question.getA());
cbMultipleB.setText(question.getB());
cbMultipleC.setText(question.getC());
cbMultipleD.setText(question.getD());
cbMultipleA.setChecked(false);
cbMultipleB.setChecked(false);
cbMultipleC.setChecked(false);
cbMultipleD.setChecked(false);
answer = answers[questionIndex];
if(answer!=null){
for(int i = 0;i getQuestions() {
ArrayList questions = new ArrayList();
// 创建第1题
Question question = new Question();
question.setId(1);
question.setStem("交通信号包括交通信号灯、交通标志、交通标线和交通警察的指挥。");
question.setA("正确");
question.setB("错误");
question.setC("");
question.setD("");
question.setType(3);
question.setAnswer("A");
// 将第1题添加到试题库
questions.add(question);
// 创建第2题
question = new Question();
question.setId(2);
question.setStem("驾驶机动车在道路上违反道路交通安全法的行为,属于什么行为?");
question.setA("违章行为");
question.setB("违法行为");
question.setC("过失行为");
question.setD("违规行为");
question.setType(1);
question.setAnswer("B");
// 将第2题添加到试题库
questions.add(question);
// 创建第3题
question = new Question();
question.setId(3);
question.setStem("机动车驾驶人违法驾驶造成重大交通事故构成犯罪的,依法追究什么责任?");
question.setA("刑事责任");
question.setB("民事责任");
question.setC("经济责任");
question.setD("直接责任");
question.setType(1);
question.setAnswer("A");
// 将第3题添加到试题库
questions.add(question);
// 创建第4题
question = new Question();
question.setId(4);
question.setStem("钱某驾驶大型卧铺客车,乘载45人(核载40人),保持40公里/小时以上的车速行至八宿县境内连续下坡急转弯路段处,翻下100米深的山崖,造成17人死亡、20人受伤。钱某的主要违法行为是什么?");
question.setA("驾驶时接听手持电话");
question.setB("超速行驶");
question.setC("客车超员");
question.setD("疲劳驾驶");
question.setType(2);
question.setAnswer("BC");
// 将第4题添加到试题库
questions.add(question);
// 创建第5题
question = new Question();
question.setId(5);
question.setStem("邹某驾驶大型卧铺客车(核载35人,实载47人),行至京港澳高速公路938公里时,因乘车人携带的大量危险化学品在车厢内突然发生爆燃,造成41人死亡,6人受伤。此事故中的主要违法行为是什么?");
question.setA("客车超员");
question.setB("乘车人携带易燃易爆危险物品");
question.setC("超速行驶");
question.setD("不按规定停车");
question.setType(2);
question.setAnswer("AB");
// 将第5题添加到试题库
questions.add(question);
return questions;
}
/**
* 退出
*
* @param view
*/
public void doExit(View view) {
finish();
}
/**
* 上一题
*
* @param view
*/
public void doPrevious(View view) {
// 判断用户是否做了本题
switch (questions.get(questionIndex).getType()) {
case 1:
if (rbSingleA.isChecked() || rbSingleB.isChecked() || rbSingleC.isChecked() || rbSingleD.isChecked()) {
saveAnswer();
}
break;
case 2:
if (cbMultipleA.isChecked() || cbMultipleB.isChecked() || cbMultipleC.isChecked() || cbMultipleD.isChecked()) {
saveAnswer();
}
break;
case 3:
if (rbJudgementT.isChecked() || rbJudgementF.isChecked()) {
saveAnswer();
}
break;
}
if (questionIndex > 0) {
questionIndex--;
} else {
questionIndex = questions.size() - 1;
}
// 根据题型设置界面
setInterfaceByType(questions.get(questionIndex));
}
/**
* 下一题
*
* @param view
*/
public void doNext(View view) {
// 判断用户是否做了本题
switch (questions.get(questionIndex).getType()) {
case 1:
if (rbSingleA.isChecked() || rbSingleB.isChecked() || rbSingleC.isChecked() || rbSingleD.isChecked()) {
saveAnswer();
}
break;
case 2:
if (cbMultipleA.isChecked() || cbMultipleB.isChecked() || cbMultipleC.isChecked() || cbMultipleD.isChecked()) {
saveAnswer();
}
break;
case 3:
if (rbJudgementT.isChecked() || rbJudgementF.isChecked()) {
saveAnswer();
}
break;
}
if (questionIndex < questions.size() - 1) {
questionIndex++;
} else {
questionIndex = 0;
}
// 根据题型设置界面
setInterfaceByType(questions.get(questionIndex));
}
/**
* 提交事件处理方法,判断分数
* @param view
*/
public void doSubmit(View view){
// 计算分数之前,需要先去保存选中的答案,保存当前题目的答案
saveAnswer();
int points = 0;
for(int i = 0;i=90){
comment = "分数"+score+"\n棒棒的!";
}else if(score>=60){
comment = "分数"+score+"\n还行吧!";
}else {
comment = "分数"+score+"\n好好干吧!";
}
//利用警告对话来显示测试结果
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher_round)
.setTitle("测试结果")
.setMessage(comment)
.setPositiveButton("确定",null)
.create()
.show();
}
private void saveAnswer() {
//保存用户选择的答案
switch(questions.get(questionIndex).getType()){
case 1://单选题
if (rbSingleA.isChecked()) {
answers[questionIndex] = "A";
} else if (rbSingleB.isChecked()) {
answers[questionIndex] = "B";
} else if (rbSingleC.isChecked()) {
answers[questionIndex] = "C";
} else {
answers[questionIndex] = "D";
}
break;
case 2://多选题
StringBuilder builder = new StringBuilder();
if(cbMultipleA.isChecked()){
builder.append("A");
}
if(cbMultipleB.isChecked()){
builder.append("B");
}
if(cbMultipleC.isChecked()){
builder.append("C");
}
if(cbMultipleD.isChecked()){
builder.append("D");
}
answers[questionIndex] = builder.toString();
break;
case 3://判断题
if (rbJudgementT.isChecked()) {
answers[questionIndex] = "A";
} else {
answers[questionIndex] = "B";
}
break;
}
}
}