界面布局exam.xml

android答题系统(三):答题部分的实现
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.mytest.exam.ExamActivity" >    <ScrollView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_weight="1" >        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="vertical" >            <TextView                android:id="@+id/ques_question"                android:layout_width="match_parent"                android:layout_height="wrap_content" />            <RadioGroup                android:id="@+id/ques_radiogroup"                android:layout_width="match_parent"                android:layout_height="wrap_content" >                <RadioButton                    android:id="@+id/ques_an1"                    android:layout_width="match_parent"                    android:layout_height="wrap_content" />                <RadioButton                    android:id="@+id/ques_an2"                    android:layout_width="match_parent"                    android:layout_height="wrap_content" />                <RadioButton                    android:id="@+id/ques_an3"                    android:layout_width="match_parent"                    android:layout_height="wrap_content" />                <RadioButton                    android:id="@+id/ques_an4"                    android:layout_width="match_parent"                    android:layout_height="wrap_content" />            </RadioGroup>            <TextView                android:id="@+id/ques_explain"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:text=""                android:visibility="invisible" />        </LinearLayout>    </ScrollView>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_weight="6"        android:orientation="horizontal" >        <Button            android:id="@+id/btn_previous"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="上一题" />        <Button            android:id="@+id/btn_next"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="下一题" />    </LinearLayout></LinearLayout>
View Code

ExamActivity.java

android答题系统(三):答题部分的实现
package com.mytest.exam;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.TextView;public class ExamActivity extends Activity {    private Button btn_next;    private Button btn_previous;    private TextView questionView;    private TextView quesExplainView;    private RadioButton[] radioButtons;    private RadioGroup radioGroup;    private int currQuesIdx = 0;    private List<Question> quesList;    private boolean wrongMode = false; // 是否核对答案    @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        setContentView(R.layout.exam);        questionView = (TextView) this.findViewById(R.id.ques_question);        quesExplainView = (TextView) this.findViewById(R.id.ques_explain);        radioButtons = new RadioButton[4];        radioButtons[0] = (RadioButton) findViewById(R.id.ques_an1);        radioButtons[1] = (RadioButton) findViewById(R.id.ques_an2);        radioButtons[2] = (RadioButton) findViewById(R.id.ques_an3);        radioButtons[3] = (RadioButton) findViewById(R.id.ques_an4);        btn_next = (Button) findViewById(R.id.btn_next);        btn_previous = (Button) findViewById(R.id.btn_previous);        radioGroup = (RadioGroup) findViewById(R.id.ques_radiogroup);        quesList = new DBService().getQuestions();        currQuesIdx = 0;        Question currQues = quesList.get(currQuesIdx);        setQuestionToGadioGroup(currQues);        addListener();    }    private void setQuestionToGadioGroup(Question ques) {        radioButtons[0].setText(ques.getAnswerA());        radioButtons[1].setText(ques.getAnswerB());        radioButtons[2].setText(ques.getAnswerC());        radioButtons[3].setText(ques.getAnswerD());        questionView.setText(ques.getQuestion());        quesExplainView.setText(ques.getExplain());    }    private void addListener() {        // 下一题        btn_next.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Log.v("info", "btn_next,currQuesIdx:" + currQuesIdx);                if (currQuesIdx < quesList.size() - 1) {                    currQuesIdx++;                    Question ques = quesList.get(currQuesIdx);                    setQuestionToGadioGroup(ques);                    radioGroup.clearCheck();                    if (ques.getSelectedAnswer() != -1) {                        radioButtons[ques.getSelectedAnswer()].setChecked(true);                    }                } else if (currQuesIdx == quesList.size() - 1) { // 最后一题                    final List<Integer> wrongList = checkAnswer();                    if (wrongList.size() < 1) {                        new AlertDialog.Builder(ExamActivity.this).setTitle("提示").setMessage("恭喜你全部回答正确!")                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {                            @Override                            public void onClick(DialogInterface dialog, int which) {                                ExamActivity.this.finish();                            }                        }).show();                    } else if (currQuesIdx == quesList.size() - 1 && wrongMode == true) {                        new AlertDialog.Builder(ExamActivity.this).setTitle("提示").setMessage("已经到达最后一题,是否退出?")                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {                            @Override                            public void onClick(DialogInterface dialog, int which) {                                ExamActivity.this.finish();                            }                        }).setNegativeButton("取消", null).show();                    } else {                        new AlertDialog.Builder(ExamActivity.this)                                .setTitle("提示").setMessage("您答对了" + (quesList.size() - wrongList.size()) + "道题目,答错了"                                        + wrongList.size() + "道题目。是否查看错题?")                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {                            @Override                            public void onClick(DialogInterface dialog, int which) {                                wrongMode = true;                                List<Question> newList = new ArrayList<Question>();                                for (int i = 0; i < wrongList.size(); i++) {                                    newList.add(quesList.get(wrongList.get(i)));                                }                                quesList.clear();                                for (int i = 0; i < newList.size(); i++) {                                    quesList.add(newList.get(i));                                }                                currQuesIdx = 0;                                Question ques = quesList.get(currQuesIdx);                                setQuestionToGadioGroup(ques);                                quesExplainView.setVisibility(View.VISIBLE);                            }                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {                            @Override                            public void onClick(DialogInterface dialog, int which) {                                ExamActivity.this.finish();                            }                        }).show();                    }                }            }        });        // 上一题        btn_previous.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Log.v("info", "btn_previous,currQuesIdx:" + currQuesIdx);                if (currQuesIdx > 0) {                    currQuesIdx--;                    Question ques = quesList.get(currQuesIdx);                    setQuestionToGadioGroup(ques);                    radioGroup.clearCheck();                    if (ques.selectedAnswer != -1) {                        radioButtons[ques.selectedAnswer].setChecked(true);                    }                }            }        });        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(RadioGroup group, int checkedId) {                for (int i = 0; i < 4; i++) {                    if (radioButtons[i].isChecked()) {                        quesList.get(currQuesIdx).setSelectedAnswer(i);                    }                }            }        });    }    /**     * 判断是否答对了     *      * @param list     * @return     */    private List<Integer> checkAnswer() {        List<Integer> wrongList = new ArrayList<Integer>();        for (int i = 0; i < quesList.size(); i++) {            if (quesList.get(i).answer != quesList.get(i).selectedAnswer) {                wrongList.add(i);            }        }        return wrongList;    }}
View Code

AndroidMainfest.xml

android答题系统(三):答题部分的实现
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.mytest.exam"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="14"        android:targetSdkVersion="21" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity android:name="com.mytest.exam.ExamActivity" >        </activity>    </application></manifest>
View Code

更多相关文章

  1. Android重启应用程序 && 不重启应用不改变系统语言改变 Android
  2. android实现卸载提示
  3. 分析:Android和Linux正在合并为一种操作系统
  4. Linux/Android——Input系统之InputReader (七)
  5. Android 系统及framework 概述
  6. Android群英传笔记——第九章:Android系统信息和安全机制

随机推荐

  1. VS2013开发Android(安卓)App 环境搭建
  2. android POST请求(https)遇到的问题
  3. android 中文 API- Scroller
  4. Android入门第五篇之TableLayout (二)//
  5. android onMeasure 实现
  6. 面试记录
  7. Android中关联源码的方法
  8. Android应用开发提高系列——Android动态
  9. [Flutter] Flutter之Android开发者教程(
  10. Android(安卓)SimpleAdapter ViewBinder