小編給大家分享一下Android實現(xiàn)簡單的答題系統(tǒng),相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:申請域名、虛擬空間、營銷軟件、網(wǎng)站建設(shè)、西峽網(wǎng)站維護、網(wǎng)站推廣。
具體內(nèi)容如下
Question:
package com.example.lenovo.exam; /** * Created by lenovo on 2017/12/4. */ //保存從數(shù)據(jù)庫中讀取的記錄 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自帶數(shù)據(jù)庫,下載SQLite Database Browser對數(shù)據(jù)庫進行操作
src main文件夾下創(chuàng)建assests文件夾(此文件夾下的內(nèi)容在應(yīng)用被打包成apk時不會被壓縮)(存放試題數(shù)據(jù)庫)
在數(shù)據(jù)庫啟動時,將該數(shù)據(jù)庫文件復制到數(shù)據(jù)庫目錄下
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. */ //連接數(shù)據(jù)庫,并從數(shù)據(jù)庫中獲取所需數(shù)據(jù) public class DBService { private SQLiteDatabase db; //在構(gòu)造函數(shù)中打開指定數(shù)據(jù)庫,并把它的引用指向db public DBService(){ db=SQLiteDatabase.openDatabase("/data/data/com.example.lenovo.exam/databases/question.db" + "",null,SQLiteDatabase.OPEN_READWRITE); } //獲取數(shù)據(jù)庫中的問題 public ListgetQuestion(){ List list=new ArrayList (); /* Cursor是結(jié)果集游標,用于對結(jié)果集進行隨機訪問,其實Cursor與JDBC中的ResultSet作用很相似。 rawQuery()方法的第一個參數(shù)為select語句;第二個參數(shù)為select語句中占位符參數(shù)的值,如果select語句沒有使用占位符,該參數(shù)可以設(shè)置為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 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 Listlist = 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按鈕跳轉(zhuǎn)到下一題;否則不響應(yīng) 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); //若之前已經(jīng)選擇過,則應(yīng)記錄選擇 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("已經(jīng)到達最后一題,是否退出?") .setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { ExamActivity.this.finish(); } }) .setNegativeButton("取消",null) .show(); } else{ //當前題目為最后一題時,告知用戶作答正確的數(shù)量和作答錯誤的數(shù)量,并詢問用戶是否要查看錯題 final List 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 newList=new ArrayList (); //將錯誤題目復制到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 0)//若當前題目不為第一題,點擊previous按鈕跳轉(zhuǎn)到上一題;否則不響應(yīng) { 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); //若之前已經(jīng)選擇過,則應(yīng)記錄選擇 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,返回給調(diào)用者 */ private List checkAnswer(List list) { List wrongList = new ArrayList (); for(int i=0;i 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"; //應(yīng)用啟動時,判斷數(shù)據(jù)庫是否存在,不存在則將提前打包好的數(shù)據(jù)庫文件復制到數(shù)據(jù)庫目錄下 //數(shù)據(jù)庫目錄不存在時,創(chuàng)建數(shù)據(jù)庫目錄 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); } //刷新輸出流,關(guān)閉輸入輸出流 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實現(xiàn)簡單的答題系統(tǒng)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)頁題目:Android實現(xiàn)簡單的答題系統(tǒng)
網(wǎng)頁URL:http://weahome.cn/article/gosodg.html