Android如何实现简单的答题系统

本文实例为大家分享了Android实现答题系统的具体代码,供大家参考,具体内容如下 Question:package com example lenovo ex

本文实例为大家分享了Android实现答题系统的具体代码,供大家参考,具体内容如下

Question:

package com.example.lenovo.exam; 
 
/** 
 * Created by lenovo on 2017/12/4. 
 */ 
//保存从数据库中读取的记录 
public class Question { 
 public String question; 
 public String answerA; 
 public String answerB; 
 public String answerC; 
 public String answerD; 
 public int answer; 
 public String explaination; 
 public int ID ; 
 //用户选择的答案 
 public int selectedAnswer; 
} 

DBService:

使用android自带数据库,下载SQLite Database Browser对数据库进行操作
src main文件夹下创建assests文件夹(此文件夹下的内容在应用被打包成apk时不会被压缩)(存放试题数据库)

在数据库启动时,将该数据库文件复制到数据库目录下

package com.example.lenovo.exam; 
 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
 
import java.util.ArrayList; 
import java.util.List; 
 
/** 
 * Created by lenovo on 2017/12/4. 
 */ 
//连接数据库,并从数据库中获取所需数据 
public class DBService { 
 private SQLiteDatabase db; 
 //在构造函数中打开指定数据库,并把它的引用指向db 
 public DBService(){ 
 db=SQLiteDatabase.openDatabase("/data/data/com.example.lenovo.exam/databases/question.db" + 
  "",null,SQLiteDatabase.OPEN_READWRITE); 
 } 
 //获取数据库中的问题 
 public List<Question> getQuestion(){ 
 List<Question> list=new ArrayList<Question>(); 
 /* 
  Cursor是结果集游标,用于对结果集进行随机访问,其实Cursor与JDBC中的ResultSet作用很相似。 
  rawQuery()方法的第一个参数为select语句;第二个参数为select语句中占位符参数的值,如果select语句没有使用占位符,该参数可以设置为null。*/ 
 Cursor cursor =db.rawQuery("select * from question",null); 
 if(cursor.getCount()>0){ 
  cursor.moveToFirst();//将cursor移动到第一个光标上 
  int count=cursor.getCount(); 
  //将cursor中的每一条记录生成一个question对象,并将该question对象添加到list中 
  for(int i=0;i<count;i++){ 
  cursor.moveToPosition(i); 
  Question question =new Question(); 
  question.ID=cursor.getInt(cursor.getColumnIndex("ID")); 
  question.question=cursor.getString(cursor.getColumnIndex("question")); 
  question.answerA=cursor.getString(cursor.getColumnIndex("answerA")); 
  question.answerB=cursor.getString(cursor.getColumnIndex("answerB")); 
  question.answerC=cursor.getString(cursor.getColumnIndex("answerC")); 
  question.answerD=cursor.getString(cursor.getColumnIndex("answerD")); 
  question.answer=cursor.getInt(cursor.getColumnIndex("answer")); 
 
  question.explaination=cursor.getString(cursor.getColumnIndex("explanation")); 
  //表示没有选择任何选项 
  question.selectedAnswer=-1; 
  list.add(question); 
  } 
 } 
 return list; 
 } 
 
} 

ExamActivity:

package com.example.lenovo.exam; 
 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
import android.widget.TextView; 
 
import java.util.ArrayList; 
import java.util.List; 
 
/** 
 * Created by lenovo on 2017/12/4. 
 */ 
 
public class ExamActivity extends Activity { 
 
 private int count; 
 private int current; 
 private boolean wrongMode;//标志变量,判断是否进入错题模式 
 
 protected void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_exam); 
 
 DBService dbService = new DBService(); 
 final List<Question> list = dbService.getQuestion(); 
 
 count = list.size(); 
 current = 0; 
 wrongMode=false;//默认情况 
 
 final TextView tv_question = findViewById(R.id.question); 
 final RadioButton[] radioButtons = new RadioButton[4]; 
 radioButtons[0] = findViewById(R.id.answerA); 
 radioButtons[1] = findViewById(R.id.answerB); 
 radioButtons[2] = findViewById(R.id.answerC); 
 radioButtons[3] = findViewById(R.id.answerD); 
 Button btn_previous = findViewById(R.id.btn_previous); 
 Button btn_next = findViewById(R.id.btn_next); 
 final TextView tv_explaination = findViewById(R.id.explaination); 
 final RadioGroup radioGroup = findViewById(R.id.radioGroup); 
 //为控件赋值 
 Question q = list.get(0); 
 tv_question.setText(q.question); 
 tv_explaination.setText(q.explaination); 
 radioButtons[0].setText(q.answerA); 
 radioButtons[1].setText(q.answerB); 
 radioButtons[2].setText(q.answerC); 
 radioButtons[3].setText(q.answerD); 
 
 btn_next.setOnClickListener(new View.OnClickListener() { 
 
  @Override 
  public void onClick(View view) { 
  if (current < count - 1) {//若当前题目不为最后一题,点击next按钮跳转到下一题;否则不响应 
   current++; 
   //更新题目 
   Question q = list.get(current); 
   tv_question.setText(q.question); 
   radioButtons[0].setText(q.answerA); 
   radioButtons[1].setText(q.answerB); 
   radioButtons[2].setText(q.answerC); 
   radioButtons[3].setText(q.answerD); 
   tv_explaination.setText(q.explaination); 
 
   //若之前已经选择过,则应记录选择 
   radioGroup.clearCheck(); 
   if (q.selectedAnswer != -1) { 
   radioButtons[q.selectedAnswer].setChecked(true); 
   } 
 
  } 
  //错题模式的最后一题 
  else if(current==count-1&& wrongMode==true){ 
   new AlertDialog.Builder(ExamActivity.this) 
    .setTitle("提示") 
    .setMessage("已经到达最后一题,是否退出?") 
    .setPositiveButton("确定", new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialogInterface, int i) { 
     ExamActivity.this.finish(); 
    } 
    }) 
    .setNegativeButton("取消",null) 
    .show(); 
 
  } 
  else{ 
   //当前题目为最后一题时,告知用户作答正确的数量和作答错误的数量,并询问用户是否要查看错题 
   final List<Integer> wrongList=checkAnswer(list); 
   //作对所有题目 
   if(wrongList.size()==0){ 
   new AlertDialog.Builder(ExamActivity.this) 
    .setTitle("提示") 
    .setMessage("恭喜你全部回答正确!") 
    .setPositiveButton("确定", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialogInterface, int i) { 
     ExamActivity.this.finish(); 
     } 
    }).show(); 
 
   } 
   else 
   new AlertDialog.Builder(ExamActivity.this) 
    .setTitle("提示") 
    .setMessage("您答对了"+(list.size()-wrongList.size())+ 
     "道题目;答错了"+wrongList.size()+"道题目。是否查看错题?") 
    .setPositiveButton("确定", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialogInterface, int which) { 
 
     //判断进入错题模式 
     wrongMode=true; 
     List<Question> newList=new ArrayList<Question>(); 
     //将错误题目复制到newList中 
     for(int i=0;i< wrongList.size();i++){ 
      newList.add(list.get(wrongList.get(i))); 
     } 
     //将原来的list清空 
     list.clear(); 
     //将错误题目添加到原来的list中 
     for(int i=0;i<newList.size();i++){ 
      list.add(newList.get(i)); 
     } 
     current=0; 
     count=list.size(); 
     //更新显示时的内容 
     Question q = list.get(current); 
     tv_question.setText(q.question); 
     radioButtons[0].setText(q.answerA); 
     radioButtons[1].setText(q.answerB); 
     radioButtons[2].setText(q.answerC); 
     radioButtons[3].setText(q.answerD); 
     tv_explaination.setText(q.explaination); 
     //显示解析 
     tv_explaination.setVisibility(View.VISIBLE); 
     } 
    }) 
    .setNegativeButton("取消", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialogInterface, int which) { 
//点击取消时,关闭当前activity 
     ExamActivity.this.finish(); 
     } 
    }).show(); 
  } 
  } 
 }); 
 btn_previous.setOnClickListener(new View.OnClickListener() { 
  @Override 
  public void onClick(View view) { 
  if (current > 0)//若当前题目不为第一题,点击previous按钮跳转到上一题;否则不响应 
  { 
   current--; 
   Question q = list.get(current); 
   tv_question.setText(q.question); 
   radioButtons[0].setText(q.answerA); 
   radioButtons[1].setText(q.answerB); 
   radioButtons[2].setText(q.answerC); 
   radioButtons[3].setText(q.answerD); 
   tv_explaination.setText(q.explaination); 
 
 
   //若之前已经选择过,则应记录选择 
   radioGroup.clearCheck(); 
   if (q.selectedAnswer != -1) { 
   radioButtons[q.selectedAnswer].setChecked(true); 
   } 
 
  } 
 
  } 
 }); 
 //选择选项时更新选择 
 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
  @Override 
  public void onCheckedChanged(RadioGroup radioGroup, int checkedId) { 
  for (int i = 0; i < 4; i++) { 
   if (radioButtons[i].isChecked() == true) { 
   list.get(current).selectedAnswer = i; 
   break; 
   } 
  } 
 
  } 
 }); 
 } 
 
 /* 
判断用户作答是否正确,并将作答错误题目的下标生成list,返回给调用者 
 */ 
 private List<Integer> checkAnswer(List<Question> list) { 
 List<Integer> wrongList = new ArrayList<Integer>(); 
 for(int i=0;i<list.size();i++) 
 { 
  if(list.get(i).answer!=list.get(i).selectedAnswer){ 
  wrongList.add(i); 
  } 
 } 
 return wrongList; 
 } 
} 

MainActivity:

package com.example.lenovo.exam; 
 
import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
 
 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
 
public class MainActivity extends AppCompatActivity { 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_main); 
 String DB_PATH = "/data/data/com.example.lenovo.exam/databases/"; 
 String DB_NAME = "question.db"; 
 //应用启动时,判断数据库是否存在,不存在则将提前打包好的数据库文件复制到数据库目录下 
 //数据库目录不存在时,创建数据库目录 
 if ((new File(DB_PATH + DB_NAME).exists()) == false) { 
  File dir = new File(DB_PATH); 
  if (!dir.exists()) { 
  dir.mkdir(); 
  } 
 } 
//定义输入输出流,用于复制文件 
 try { 
  InputStream is = getBaseContext().getAssets().open(DB_NAME); 
  OutputStream os = new FileOutputStream(DB_PATH + DB_NAME); 
  byte[] buffer = new byte[1024]; 
  int length; 
  while ((length = is.read(buffer)) > 0) { 
  os.write(buffer, 0, length); 
  } 
  //刷新输出流,关闭输入输出流 
  os.flush(); 
  os.close(); 
  os.close(); 
 
 
 } catch (IOException e) { 
  e.printStackTrace(); 
 } 
 
 
 Button btn = findViewById(R.id.button); 
 btn.setOnClickListener(new View.OnClickListener() 
 
 { 
  public void onClick(View v) { 
  Intent intent = new Intent(MainActivity.this, ExamActivity.class); 
  startActivity(intent); 
 
 
  } 
 }); 
 
 
 } 
} 

运行截图

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持好代码网。