在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例Android平台开发 → Android问卷或试题Demo,支持单项、多选、判断

Android问卷或试题Demo,支持单项、多选、判断

Android平台开发

下载此实例
  • 开发语言:Java
  • 实例大小:29.21M
  • 下载次数:33
  • 浏览次数:313
  • 发布时间:2019-12-01
  • 实例类别:Android平台开发
  • 发 布 人:robot666
  • 文件格式:.zip
  • 所需积分:2
 相关标签:

实例介绍

【实例简介】Android问卷或试题Demo,支持单项、多选、判断,代码清晰简单。

【实例截图】

from clipboard

【核心代码】

package com.example.administrator.testdemo;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.testdemo.model.Answer;
import com.example.administrator.testdemo.model.Page;
import com.example.administrator.testdemo.model.Quesition;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class MainActivity extends Activity {
    private String resultJosn = "{\"result\":\"1\",\"type\":\"1\",\"data\":[{\"type\":\"3\",\"eid\":\"11\",\"problem\":\"1、 什么样的团队更容易获得成功?\",\"trueanswer\":\"B\",\"optionData\":[{\"option\":\"团结\",\"isChecked\":\"0\"},{\"option\":\"精英\",\"isChecked\":\"0\"},{\"option\":\"聪明\",\"isChecked\":\"0\"},{\"option\":\"领导\",\"isChecked\":\"0\"}]},{\"type\":\"3\",\"eid\":\"12\",\"problem\":\"2、 学习态度\",\"trueanswer\":\"C\",\"optionData\":[{\"option\":\"好\",\"isChecked\":\"0\"},{\"option\":\"一般\",\"isChecked\":\"0\"},{\"option\":\"很好\",\"isChecked\":\"0\"},{\"option\":\"差\",\"isChecked\":\"0\"}]},{\"type\":\"2\",\"eid\":\"13\",\"problem\":\"3、 这件事真的是对的吗\",\"trueanswer\":\"A\",\"optionData\":[{\"option\":\"正确\",\"isChecked\":\"0\"},{\"option\":\"错误\",\"isChecked\":\"0\"}]},{\"type\":\"2\",\"eid\":\"14\",\"problem\":\"4、 这个道理真的对吗\",\"trueanswer\":\"B\",\"optionData\":[{\"option\":\"正确\",\"isChecked\":\"0\"},{\"option\":\"错误\",\"isChecked\":\"0\"}]},{\"type\":\"1\",\"eid\":\"15\",\"problem\":\"5、 老师姓什么?\",\"trueanswer\":\"D\",\"optionData\":[{\"option\":\"赵\",\"isChecked\":\"0\"},{\"option\":\"钱\",\"isChecked\":\"0\"},{\"option\":\"孙\",\"isChecked\":\"0\"},{\"option\":\"李\",\"isChecked\":\"0\"}]},{\"type\":\"1\",\"eid\":\"16\",\"problem\":\"6、 这个要多上钱\",\"trueanswer\":\"C\",\"optionData\":[{\"option\":\"100\",\"isChecked\":\"0\"},{\"option\":\"200\",\"isChecked\":\"0\"},{\"option\":\"300\",\"isChecked\":\"0\"},{\"option\":\"400\",\"isChecked\":\"0\"}]},{\"type\":\"2\",\"eid\":\"17\",\"problem\":\"7、 我不用学这个课程也能成功\",\"trueanswer\":\"B\",\"optionData\":[{\"option\":\"正确\",\"isChecked\":\"0\"},{\"option\":\"错误\",\"isChecked\":\"0\"}]},{\"type\":\"2\",\"eid\":\"18\",\"problem\":\"8、 学习人数已经由300人了\",\"trueanswer\":\"A\",\"optionData\":[{\"option\":\"正确\",\"isChecked\":\"0\"},{\"option\":\"错误\",\"isChecked\":\"0\"}]},{\"type\":\"1\",\"eid\":\"19\",\"problem\":\"9、 该课程要上多久\",\"trueanswer\":\"A\",\"optionData\":[{\"option\":\"10分钟\",\"isChecked\":\"0\"},{\"option\":\"20分钟\",\"isChecked\":\"0\"},{\"option\":\"30分钟\",\"isChecked\":\"0\"},{\"option\":\"40分钟\",\"isChecked\":\"0\"}]},{\"type\":\"2\",\"eid\":\"20\",\"problem\":\"10、 学过这个课程的都成功了\",\"trueanswer\":\"B\",\"optionData\":[{\"option\":\"正确\",\"isChecked\":\"0\"},{\"option\":\"错误\",\"isChecked\":\"0\"}]}]}\n";
//    private String resultJosn = "{\"result\":\"1\",\"type\":\"1\",\"data\":[{\"type\":\"2\",\"eid\":\"6\",\"problem\":\"1、 真不拿\",\"trueanswer\":\"A\",\"optionData\":[{\"option\":\"正确\",\"isChecked\":\"0\"},{\"option\":\"错误\",\"isChecked\":\"0\"}]},{\"type\":\"2\",\"eid\":\"7\",\"problem\":\"2、 这个怎么做\",\"trueanswer\":\"A\",\"optionData\":[{\"option\":\"1\",\"isChecked\":\"1\"},{\"option\":\"2\",\"isChecked\":\"0\"},{\"option\":\"3\",\"isChecked\":\"0\"},{\"option\":\"4\",\"isChecked\":\"0\"}]},{\"type\":\"2\",\"eid\":\"8\",\"problem\":\"3、 wgergwe\",\"trueanswer\":\"B\",\"optionData\":[{\"option\":\"正确\",\"isChecked\":\"0\"},{\"option\":\"错误\",\"isChecked\":\"0\"}]},{\"type\":\"3\",\"eid\":\"9\",\"problem\":\"4、 szdfasdf\",\"trueanswer\":\"C\",\"optionData\":[{\"option\":\"112\",\"isChecked\":\"0\"},{\"option\":\"22\",\"isChecked\":\"0\"},{\"option\":\"23\",\"isChecked\":\"0\"},{\"option\":\"43\",\"isChecked\":\"1\"}]},{\"type\":\"2\",\"eid\":\"10\",\"problem\":\"5、 rqwerqw\",\"trueanswer\":\"B\",\"optionData\":[{\"option\":\"正确\",\"isChecked\":\"0\"},{\"option\":\"错误\",\"isChecked\":\"0\"}]}]}";
    private LinearLayout test_layout;
    private Page the_page;
    // 答案列表
    private ArrayList<Answer> the_answer_list;
    // 问题列表
    private ArrayList<Quesition> the_quesition_list;
    // 问题所在的View
    private View que_view;
    // 答案所在的View
    private View ans_view;
    private LayoutInflater xInflater;
    private Page page;
    // 下面这两个list是为了实现点击的时候改变图片,因为单选多选时情况不一样,为了方便控制
    // 存每个问题下的TextView
    private ArrayList<ArrayList<TextView>> textlist = new ArrayList<ArrayList<TextView>>();
    // 存每个答案的TextView
    private ArrayList<TextView> textlist2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        xInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // 假数据
        initDate();
        // 提交按钮
        TextView button = (TextView) findViewById(R.id.tv_commit);
        button.setOnClickListener(new submitOnClickListener(page));
    }

    private void initDate() {
        ArrayList<Quesition> quesitionsList = null;
        try {
            quesitionsList = new ArrayList<>();//问题列表

            JSONObject resultJson = new JSONObject(resultJosn);
            JSONArray arrayJson = resultJson.optJSONArray("data");

            for (int i=0;i<arrayJson.length();i  ){
                JSONObject subObject = arrayJson.getJSONObject(i);

                ArrayList<Answer> answers = new ArrayList<>();

                JSONArray arrayAnswerJson = subObject.optJSONArray("optionData");//问题的答案
                for (int j=0; j< arrayAnswerJson.length(); j  ) {
                    JSONObject answerObject = arrayAnswerJson.getJSONObject(j);
                    Answer a_answer = new Answer();
                    a_answer.setAnswerId("" j);
                    a_answer.setAnswer_content(answerObject.getString("option"));
                    a_answer.setAns_state(Integer.parseInt(answerObject.getString("isChecked")));

                    answers.add(a_answer);
                }

                Quesition q_quesition = new Quesition();
                q_quesition.setQuesitionId(subObject.getString("eid"));//问题的id
                q_quesition.setType(subObject.getString("type"));//类型,1判断 2单选 3不定项
                q_quesition.setContent(subObject.getString("problem"));//问题
                q_quesition.setAnswers(answers);
                q_quesition.setQue_state(0);

                quesitionsList.add(q_quesition);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        page = new Page();
        page.setPageId("1");
        page.setStatus("0");
        page.setTitle("精英问卷");
        page.setQuesitions(quesitionsList);
        // 加载布局
        initView(page);
    }

    private void initView(Page page) {
        // TODO Auto-generated method stub
        // 这是要把问题的动态布局加入的布局
        test_layout = (LinearLayout) findViewById(R.id.lly_test);
        TextView page_txt = (TextView) findViewById(R.id.txt_title);
        page_txt.setText(page.getTitle());
        // 获得问题即第二层的数据
        the_quesition_list = page.getQuesitions();
        // 根据第二层问题的多少,来动态加载布局
        for (int i = 0; i < the_quesition_list.size(); i  ) {
            que_view = xInflater.inflate(R.layout.quesition_layout, null);
            ImageView iv_type = (ImageView) que_view.findViewById(R.id.iv_type);
            TextView txt_que = (TextView) que_view.findViewById(R.id.txt_question_item);
            // 这是第三层布局要加入的地方
            LinearLayout add_layout = (LinearLayout) que_view.findViewById(R.id.lly_answer);

            View v_line = (View) que_view.findViewById(R.id.v_line);
            if(i == 0){//第一道题目的分隔条不用显示
                v_line.setVisibility(View.GONE);
            }

            //类型:1选择题;2判断题 3不定项
            if (the_quesition_list.get(i).getType().equals("1")) {
                iv_type.setImageResource(R.mipmap.single_menu);
            } else if (the_quesition_list.get(i).getType().equals("2")){
                iv_type.setImageResource(R.mipmap.judge_menu);
            } else if (the_quesition_list.get(i).getType().equals("3")){
                iv_type.setImageResource(R.mipmap.more_menu);
            }

            txt_que.setText(the_quesition_list.get(i).getContent());//设置问题题目
            // 获得答案即第三层数据
            the_answer_list = the_quesition_list.get(i).getAnswers();
            textlist2 = new ArrayList<>();
            for (int j = 0; j < the_answer_list.size(); j  ) {

                ans_view = xInflater.inflate(R.layout.answer_layout, null);
                TextView txt_ans = (TextView) ans_view.findViewById(R.id.txt_answer_item);
                TextView tv_menu = (TextView) ans_view.findViewById(R.id.tv_menu);

                //自己手动为每个问题的选项加上相应的ABCD...
                if (j==0){
                    tv_menu.setText("A");
                }else if (j==1){
                    tv_menu.setText("B");
                }else if (j==2){
                    tv_menu.setText("C");
                }else if (j==3){
                    tv_menu.setText("D");
                }else if (j==4){
                    tv_menu.setText("E");
                }else if (j==5){
                    tv_menu.setText("F");
                }else if (j==6){
                    tv_menu.setText("G");
                }
                // 判断哪个答案已选
                if (the_answer_list.get(j).getAns_state() == 1) {
//                    Toast.makeText(getApplicationContext(),"1",Toast.LENGTH_SHORT).show();
                    the_quesition_list.get(i).setQue_state(1);
                    tv_menu.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_red));
                    tv_menu.setTextColor(getResources().getColor(R.color.colorWhite));
                } else {
//                    Toast.makeText(getApplicationContext(),"0",Toast.LENGTH_SHORT).show();
                    tv_menu.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_white));
                    tv_menu.setTextColor(getResources().getColor(R.color.colorblack));
                }

                textlist2.add(tv_menu);
                txt_ans.setText(the_answer_list.get(j).getAnswer_content());
                LinearLayout lly_answer_size = (LinearLayout) ans_view.findViewById(R.id.lly_answer_size);

                if (j%2!=0){//为了美观了,将答案的背景隔开一下
                    lly_answer_size.setBackgroundColor(getResources().getColor(R.color.colorWhite));
                }
                //设置点击事件
                lly_answer_size .setOnClickListener(new answerItemOnClickListener(i, j, the_answer_list, txt_ans));
                add_layout.addView(ans_view);
            }
            textlist.add(textlist2);

            test_layout.addView(que_view);//将生成的问题都添加到一个布局文件中
        }
    }

    /**
     * 试卷各个选项的点击事件
     */
    class answerItemOnClickListener implements View.OnClickListener {
        private int i;
        private int j;
        private TextView txt;
        private ArrayList<Answer> the_answer_lists;

        public answerItemOnClickListener(int i, int j, ArrayList<Answer> the_answer_list, TextView text) {
            this.i = i;
            this.j = j;
            this.the_answer_lists = the_answer_list;
            this.txt = text;
        }

        // 实现点击选项后改变选中状态以及对应图片
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            // 判断当前问题是单选还是多选
            if (the_quesition_list.get(i).getType().equals("3")) {//1选择题;2判断题 3不定项

                for(int z=0;z<the_answer_list.size();z  ){
                    if(the_answer_list.get(z).getAns_state()==1){
                        the_quesition_list.get(i).setQue_state(1);
//                        return;
                    }else{
                        the_quesition_list.get(i).setQue_state(0);
                    }
                }
                if (the_answer_lists.get(j).getAns_state() == 0) {
//                    Toast.makeText(getApplication(), "0", Toast.LENGTH_SHORT).show();
                    textlist.get(i).get(j).setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_red));
                    textlist.get(i).get(j).setTextColor(getResources().getColor(R.color.colorWhite));
                    the_answer_lists.get(j).setAns_state(1);
                    the_quesition_list.get(i).setQue_state(1);
                } else {
//                    Toast.makeText(getApplication(), "1", Toast.LENGTH_SHORT).show();
                    textlist.get(i).get(j).setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_white));
                    textlist.get(i).get(j).setTextColor(getResources().getColor(R.color.colorblack));
                    the_answer_lists.get(j).setAns_state(0);
                    the_quesition_list.get(i).setQue_state(0);
                }
            } else {// 单选
                for (int z = 0; z < the_answer_lists.size(); z  ) {
                    if (z == j) {
//                        Toast.makeText(getApplication(), "3", Toast.LENGTH_SHORT).show();
                        // 如果当前未被选中
                        textlist.get(i).get(j).setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_red));
                        textlist.get(i).get(j).setTextColor(getResources().getColor(R.color.colorWhite));
                        the_answer_lists.get(z).setAns_state(1);
                        the_quesition_list.get(i).setQue_state(1);
                    } else {
//                        Toast.makeText(getApplication(), "4", Toast.LENGTH_SHORT).show();
                        the_answer_lists.get(z).setAns_state(0);
                        the_quesition_list.get(i).setQue_state(1);
                        textlist.get(i).get(z).setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_white));
                        textlist.get(i).get(z).setTextColor(getResources().getColor(R.color.colorblack));
                    }
                }
            }
        }
    }

    /**
     * 提交按钮事件处理
     */
    class submitOnClickListener implements View.OnClickListener {

        private Page page;

        public submitOnClickListener(Page page) {
            this.page = page;
        }

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            // 判断是否答完题
            boolean isState = true;
            // 最终要的json数组
            JSONArray jsonArray = new JSONArray();
            // 点击提交的时候,先判断状态,如果有未答完的就提示,如果没有再把每条答案提交(包含问卷ID 问题ID 及答案ID)
            // 注:不用管是否是一个问题的答案,就以答案的个数为准来提交上述格式的数据
            for (int i = 0; i < the_quesition_list.size(); i  ) {
                the_answer_list = the_quesition_list.get(i).getAnswers();
                // 判断是否有题没答完
                if (the_quesition_list.get(i).getQue_state() == 0) {
                    Toast.makeText(getApplicationContext(), "您第"   (i   1)   "题没有答完", Toast.LENGTH_LONG).show();
                    jsonArray = null;
                    isState = false;
                    return;
                } else {
                    JSONObject json = new JSONObject();
                    String answers2 = "";
                    String answers = "";
                    for (int j = 0; j < the_answer_list.size(); j  ) {
                        if (the_answer_list.get(j).getAns_state() == 1) {
                            try {
                                answers2 = the_quesition_list.get(i).getQuesitionId();
                                if (answers.length()==0){
                                    answers = answers  j;
                                }else {
                                    answers = answers  "-"  j;
                                }

                                //===为不定项拼接答案================================
                                if (answers.contains("0")) {
                                    answers = answers.replace("0", "A");
                                }
                                if (answers.contains("1")) {
                                    answers = answers.replace("1", "B");
                                }
                                if (answers.contains("2")) {
                                    answers = answers.replace("2", "C");
                                }
                                if (answers.contains("3")) {
                                    answers = answers.replace("3", "D");
                                }
                                if (answers.contains("4")) {
                                    answers = answers.replace("4", "E");
                                }
                                if (answers.contains("5")) {
                                    answers = answers.replace("5", "F");
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    try {
                        json.put("answer", answers);
                        json.put("eid", answers2);
//                        Toast.makeText(getApplicationContext(), json   "", Toast.LENGTH_SHORT).show();
                        jsonArray.put(json);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
            Log.d("jsonArray-->", ""   jsonArray);
            Toast.makeText(getApplicationContext(), "提交的数据:"   jsonArray, Toast.LENGTH_SHORT).show();
        }
    }
}

标签:

实例下载地址

Android问卷或试题Demo,支持单项、多选、判断

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警