码迷,mamicode.com
首页 > 移动开发 > 详细

android答题系统(三):答题部分的实现

时间:2015-07-24 09:11:32      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

界面布局exam.xml

技术分享
<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

技术分享
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

技术分享
<?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

 

android答题系统(三):答题部分的实现

标签:

原文地址:http://www.cnblogs.com/2015android/p/4672346.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!