您好,登錄后才能下訂單哦!
小編給大家分享一下Android實現簡單的答題系統(tǒng),相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
具體內容如下
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文件夾下創(chuàng)建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"; //應用啟動時,判斷數據庫是否存在,不存在則將提前打包好的數據庫文件復制到數據庫目錄下 //數據庫目錄不存在時,創(chuàng)建數據庫目錄 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); } }); } }
運行截圖
以上是“Android實現簡單的答題系統(tǒng)”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。