Android项目实战系列—基于博学谷(六)习题模块

Stella981
• 阅读 610

Android项目实战系列—基于博学谷(六)习题模块

由于这个模块内容较多,篇幅较长,请耐心阅读。


习题模块分为两个部分

  • [x] 习题列表界面
  • [x] 习题详情界面

一、习题列表界面

1、习题界面

(1)、创建习题界面

res/layout文件夹中,新建一个布局文件,命名为main_view_exercises

(2)、导入界面图片

将习题界面所需图片 exercises_bg_1.pngexercises_bg_2.pngexercises_bg_3.pngexercises_bg_4.png导入drawable文件夹中。

(3)、习题界面代码main_view_exercises.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <ListView
        android:id="@+id/lv_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginBottom="55dp"
        android:divider="#E4E4E4"
        android:dividerHeight="1dp"/>
</LinearLayout>

2、习题界面Item

由于习题界面使用了ListView控件,所以需要创建一个Item界面。在res/layout文件夹中,新建一个布局文件,命名为exercises_list_item。代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:paddingBottom="15dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="15dp">
    <TextView
        android:id="@+id/tv_order"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="10dp"
        android:background="@drawable/exercises_bg_1"
        android:gravity="center"
        android:textColor="@android:color/white"
        android:textSize="16sp"/>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="15dp"
        android:gravity="center_vertical"
        android:orientation="vertical">
        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textColor="#000000"
            android:textSize="14sp"/>
        <TextView
            android:id="@+id/tv_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dp"
            android:singleLine="true"
            android:textColor="#a3a3a3"
            android:textSize="12sp"/>
    </LinearLayout>
</LinearLayout>

3、创建ExerciseBean类

com.bocuegu.bean包中新建一个java类,命名为ExerciseBean。用来创建习题所有的属性。代码如下:
package com.boxuegu.bean;

public class ExercisesBean {
    public int id;              //每章习题ID
    public String title;        //每章习题标题
    public String content;      //每章习题的数目
    public int background;      //每章习题前边的序列背景
    public int subjectId;       //每道习题的ID
    public String subject;      //每道习题的题干
    public String a;            //每道习题的A选项
    public String b;            //每道习题的B选项
    public String c;            //每道习题的C选项
    public String d;            //每道习题的D选项
    public int answer;          //每道习题的正确答案
    /*
     * select为 0,1,2,3,4,的含义:
     * 0时表示所选选项是对的 
     * 1表示选项中的A是错误的 
     * 2表示选项中的B是错误的 
     * 3表示选项中的C是错误的 
     * 4表示选项中的D是错误的
     */
    public int select;
}

4、习题界面Adapter

选择com.boxuegu包,创建adapter包。在adapter包中创建一个Java类,命名为ExercisesAdapter。由于ExercisesAdapter类继承自BaseAdapter类,所以要修改主类,代码如下:
package com.boxuegu.adapter;

import android.content.Context;
import android.content.Intent;
import com.boxuegu.R;
import com.boxuegu.activity.ExercisesDetailActivity;
import com.boxuegu.bean.ExercisesBean;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

//ExercisesAdapter类继承自BaseAdapter类
public class ExercisesAdapter extends BaseAdapter {
    private Context mContext;
    private List<ExercisesBean> ebl;
    public ExercisesAdapter(Context context){
        this.mContext = context;
    }
    
    //设置数据更新界面
    public void setData(List<ExercisesBean> ebl){
        this.ebl = ebl;
        notifyDataSetChanged();
    }
    
    //获取Item的总数
    @Override
    public int getCount() {
        return ebl == null?0:ebl.size();
    }

    //根据position得到对应Item的对象
    @Override
    public ExercisesBean getItem(int position) {
        return ebl == null ? null : ebl.get(position);
    }

  //根据position得到对应Item的ID
    @Override
    public long getItemId(int position) {
        return position;
    }

  //根据position得到对应Item的视图,position是当前Item的位置
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder vh;
        //复用convertView
        if (convertView == null){
            vh = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.exercises_list_item,null);
            vh.title = (TextView) convertView.findViewById(R.id.tv_title);
            vh.content = (TextView) convertView.findViewById(R.id.tv_content);
            vh.order = (TextView) convertView.findViewById(R.id.tv_order);
            convertView.setTag(vh);
        }else {
            vh = (ViewHolder) convertView.getTag();
        }
        //获取position对应的Item的数据对象
        final ExercisesBean bean = getItem(position);
        if (bean != null){
            vh.order.setText(position + 1 + "");
            vh.title.setText(bean.title);
            vh.content.setText(bean.content);
            vh.order.setBackgroundResource(bean.background);
        }
        //每个Item的点击事件
        convertView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                if (bean == null)
                    return;
                //跳转到习题详情界面  后面添加
                Intent intent = new Intent(mContext, ExercisesDetailActivity.class);
                intent.putExtra("id",bean.id);
                intent.putExtra("title",bean.title);
                mContext.startActivity(intent);
            }
        });
        return convertView;
    }
    class ViewHolder{
        public TextView title,content;
        public TextView order;
    }
}

5、习题界面逻辑代码

com.boxuegu.view包中创建一个ExercisesView的Java类。
package com.boxuegu.view;

import android.app.Activity;
import com.boxuegu.R;
import com.boxuegu.adapter.ExercisesAdapter;
import com.boxuegu.bean.ExercisesBean;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;


public class ExercisesView {
    private ListView lv_list;
    private ExercisesAdapter adapter;
    private List<ExercisesBean> ebl;
    private Activity mContext;
    private LayoutInflater mInflater;
    private View mCurrentView;
    public ExercisesView(Activity context){
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
    }
    private void createView(){
        initView();
    }
    private void initView(){
        mCurrentView = mInflater.inflate(R.layout.main_view_exercises,null);
        lv_list = (ListView) mCurrentView.findViewById(R.id.lv_list);
        adapter = new ExercisesAdapter(mContext);
        initData();
        adapter.setData(ebl);
        lv_list.setAdapter(adapter);
    }
    
    //设置数据
    private void initData(){
        ebl = new ArrayList<ExercisesBean>();
        for (int i=0;i<10;i++){
            ExercisesBean bean = new ExercisesBean();
            bean.id = (i+1);
            switch (i){
                case 0:
                    bean.title = "第1章 博客(网站)简介";
                    bean.content="共计5题";
                    bean.background=(R.drawable.exercises_bg_1);
                    break;
                case 1:
                    bean.title = "第2章 认识WordPress";
                    bean.content="共计5题";
                    bean.background=(R.drawable.exercises_bg_2);
                    break;
                case 2:
                    bean.title = "第3章 WordPress基础";
                    bean.content="共计5题";
                    bean.background=(R.drawable.exercises_bg_3);
                    break;
                case 3:
                    bean.title = "第4章 熟悉Linux系统";
                    bean.content="共计5题";
                    bean.background=(R.drawable.exercises_bg_4);
                    break;
                case 4:
                    bean.title = "第5章 PHP基本语法";
                    bean.content="共计5题";
                    bean.background=(R.drawable.exercises_bg_1);
                    break;
                case 5:
                    bean.title = "第6章 MySQL数据库";
                    bean.content="共计5题";
                    bean.background=(R.drawable.exercises_bg_2);
                    break;
                case 6:
                    bean.title = "第7章 Nginx服务器";
                    bean.content="共计5题";
                    bean.background=(R.drawable.exercises_bg_3);
                    break;
                case 7:
                    bean.title = "第8章 云服务器简介";
                    bean.content="共计5题";
                    bean.background=(R.drawable.exercises_bg_4);
                    break;
                case 8:
                    bean.title = "第9章 环境搭建过程";
                    bean.content="共计5题";
                    bean.background=(R.drawable.exercises_bg_1);
                    break;
                case 9:
                    bean.title = "第10章 WordPress主题";
                    bean.content="共计5题";
                    bean.background=(R.drawable.exercises_bg_2);
                    break;
                default:
                    break;
            }
            ebl.add(bean);
        }
    }
    
    //获取当前在导航栏上方显示对应的view
    public View getView(){
        if (mCurrentView == null){
            createView();
        }
        return mCurrentView;
    }
    
    //显示当前导航栏上方所对应的view界面
    public void showView(){
        if (mCurrentView == null) {
            createView();
        }
        mCurrentView.setVisibility(View.VISIBLE);
    }

}

6、修改底部导航栏代码

因为习题界面是通过底部导航栏进行跳转的,找到MainActivity.java文件的private RelativeLayout rl_title_bar;,在下方添加:
private ExercisesView mExerciseView;
找到MainActivity.java文件的createView()方法,在注释//习题界面下方添加如下代码:
if (mExerciseView == null){
                    mExerciseView = new ExercisesView(this);
                    mBodyLayout.addView(mExerciseView.getView());
                }else{
                    mExerciseView.getView();
                }
                mExerciseView.showView();

二、习题详情界面

1、创建界面

(1)、创建习题详情界面

com.boxuegu.activity中创建一个Java类,命名为ExercisesDetailActivity。在res/layout文件夹下创建一个布局文件,命名为activity_exercises_detail

(2)、导入界面图片 exercises_a.pngexercises_b.pngexercises_c.pngexercises_d.pngexercises_error_icon.pngexercises_right_icon.png

(3)、界面代码activity_exercises_detail.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <include layout="@layout/main_title_bar" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="15dp"
        android:text="一、选择题"
        android:textColor="#000000"
        android:textSize="16sp"
        android:textStyle="bold"
        android:visibility="gone"/>
    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/lv_list"
        android:divider="@null"/>

</LinearLayout>

2、习题详情界面Item

res/layout文件夹中创建一个布局文件,命名为exercises_detail_list_item。代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/white"
    android:padding="15dp">

    <TextView
        android:id="@+id/tv_subject"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:lineSpacingMultiplier="1.5"
        android:textColor="#000000"
        android:textSize="14sp"/>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:orientation="horizontal">
        <ImageView
            android:id="@+id/iv_a"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:src="@drawable/exercises_a"/>
        <TextView
            android:id="@+id/tv_a"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="8dp"
            android:lineSpacingMultiplier="1.5"
            android:textColor="#000000"
            android:textSize="12sp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:orientation="horizontal">
        <ImageView
            android:id="@+id/iv_b"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:src="@drawable/exercises_b"/>
        <TextView
            android:id="@+id/tv_b"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="8dp"
            android:lineSpacingMultiplier="1.5"
            android:textColor="#000000"
            android:textSize="12sp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:orientation="horizontal">
        <ImageView
            android:id="@+id/iv_c"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:src="@drawable/exercises_c"/>
        <TextView
            android:id="@+id/tv_c"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="8dp"
            android:lineSpacingMultiplier="1.5"
            android:textColor="#000000"
            android:textSize="12sp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:orientation="horizontal">
        <ImageView
            android:id="@+id/iv_d"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:src="@drawable/exercises_d"/>
        <TextView
            android:id="@+id/tv_d"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="8dp"
            android:lineSpacingMultiplier="1.5"
            android:textColor="#000000"
            android:textSize="12sp"/>
    </LinearLayout>
</LinearLayout>

3、习题数据的存放

(1)、如果没有assets文件夹则创建一个。

(2)、在assets文件夹下创建xml文件用来存放习题内容。

因为有10个章节的内容,所以要创建10个xml文件。这里演示一个,其余9个依次类推。注意,这里创建的是xml文件,不是android xml文件。

Android项目实战系列—基于博学谷(六)习题模块

<?xml version="1.0" encoding="UTF-8"?>
<infos>
  <exercises id="1">
      <subject>1.下列哪个选项属于一个网站()。</subject>
      <a>百度APP</a>
      <b>腾讯网</b>
      <c>谷歌浏览器</c>
      <d>Eclipse</d>
      <answer>2</answer>
  </exercises>
  <exercises id="2">
      <subject>2.个人博客的英文简称()。</subject>
      <a>Eclipse</a>
      <b>SDK</b>
      <c>SDK Manager.exe</c>
      <d>blog</d>
      <answer>4</answer>
  </exercises>
  <exercises id="3">
      <subject>3.CMS系统属于什么类型的网站系统()。</subject>
      <a>纯静态</a>
      <b>纯动态</b>
      <c>伪静态</c>
      <d>以上都不对</d>
      <answer>2</answer>
  </exercises>
  <exercises id="4">
      <subject>4.个人博客与网站的关系()。</subject>
      <a>博客属于网站</a>
      <b>网站属于博客</b>
      <c>静态网站属于博客</c>
      <d>静态博客属于网站</d>
      <answer>1</answer>
  </exercises>
  <exercises id="5">
      <subject>5.动态博客有哪些功能()。</subject>
      <a>发布文章</a>
      <b>留言评论</b>
      <c>后台管理</c>
      <d>以上都对</d>
      <answer>4</answer>
  </exercises>
</infos>

(3)、解析xml文件

找到AnalysisUtils.java文件,添加以下代码用来解析XML文件:
//解析XML文件
     public static List<ExercisesBean> getExercisesInfos(InputStream is) throws Exception{
            XmlPullParser parser = Xml.newPullParser();
            parser.setInput(is,"utf-8");
            List<ExercisesBean> exercisesInfos = null;
            ExercisesBean exercisesInfo = null;
            int type = parser.getEventType();
            while (type!=XmlPullParser.END_DOCUMENT){
                switch (type){
                    case XmlPullParser.START_TAG:
                        if ("infos".equals(parser.getName())){
                            exercisesInfos = new ArrayList<ExercisesBean>();
                        }else if ("exercises".equals(parser.getName())){
                            exercisesInfo = new ExercisesBean();
                            String ids = parser.getAttributeValue(0);
                            exercisesInfo.subjectId = Integer.parseInt(ids);
                        }else if ("subject".equals(parser.getName())){
                            String subject = parser.nextText();
                            exercisesInfo.subject=subject;
                        }else if ("a".equals(parser.getName())){
                            String a = parser.nextText();
                            exercisesInfo.a=a;
                        }else if ("b".equals(parser.getName())){
                            String b = parser.nextText();
                            exercisesInfo.b=b;
                        }else if ("c".equals(parser.getName())){
                            String c = parser.nextText();
                            exercisesInfo.c=c;
                        }else if ("d".equals(parser.getName())){
                            String d = parser.nextText();
                            exercisesInfo.d=d;
                        }else if ("answer".equals(parser.getName())){
                            String answer = parser.nextText();
                            exercisesInfo.answer=Integer.parseInt(answer);
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if ("exercises".equals(parser.getName())){
                            exercisesInfos.add(exercisesInfo);
                            exercisesInfo=null;
                        }
                        break;
                }
                type=parser.next();
            }
            return exercisesInfos;
        }

4、习题详情界面Adapter

com.boxuegu.adapter包中创建一个Java类,命名为ExrecisesDetailAdapter。代码如下:
package com.boxuegu.adapter;

import android.content.Context;
import com.boxuegu.R;
import com.boxuegu.bean.ExercisesBean;
import com.boxuegu.utils.AnalysisUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


public class ExrecisesDetailAdapter extends BaseAdapter {
    private Context mContext;
    private List<ExercisesBean> ebl;
    private OnSelectListener onSelectListener;
    public ExrecisesDetailAdapter(Context context,OnSelectListener onSelectListener){
        this.mContext = context;
        this.onSelectListener = onSelectListener;
    }
    public void setData(List<ExercisesBean> ebl){
        this.ebl = ebl;
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return ebl == null ? 0 : ebl.size();
    }

    @Override
    public ExercisesBean getItem(int position) {
        return ebl == null?null:ebl.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    
    //记录点击的位置
    private ArrayList<String> selectedPosition = new ArrayList<String>();

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        final ViewHolder vh;
        if (convertView == null){
            vh = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.exercises_detail_list_item,null);
            vh.subject = (TextView) convertView.findViewById(R.id.tv_subject);
            vh.tv_a = (TextView) convertView.findViewById(R.id.tv_a);
            vh.tv_b = (TextView) convertView.findViewById(R.id.tv_b);
            vh.tv_c = (TextView) convertView.findViewById(R.id.tv_c);
            vh.tv_d = (TextView) convertView.findViewById(R.id.tv_d);
            vh.iv_a = (ImageView) convertView.findViewById(R.id.iv_a);
            vh.iv_b = (ImageView) convertView.findViewById(R.id.iv_b);
            vh.iv_c = (ImageView) convertView.findViewById(R.id.iv_c);
            vh.iv_d = (ImageView) convertView.findViewById(R.id.iv_d);
            convertView.setTag(vh);
        }else {
            vh=(ViewHolder) convertView.getTag();
        }
        final ExercisesBean bean = getItem(position);
        if (bean != null){
            vh.subject.setText(bean.subject);
            vh.tv_a.setText(bean.a);
            vh.tv_b.setText(bean.b);
            vh.tv_c.setText(bean.c);
            vh.tv_d.setText(bean.d);
        }
        if (!selectedPosition.contains("" + position)){
            vh.iv_a.setImageResource(R.drawable.exercises_a);
            vh.iv_b.setImageResource(R.drawable.exercises_b);
            vh.iv_c.setImageResource(R.drawable.exercises_c);
            vh.iv_d.setImageResource(R.drawable.exercises_d);
            AnalysisUtils.setABCDEnable(true,vh.iv_a,vh.iv_b,vh.iv_c,vh.iv_d);
        }else {
            AnalysisUtils.setABCDEnable(false,vh.iv_a,vh.iv_b,vh.iv_c,vh.iv_d);
            switch (bean.select){
                case 0:  //用户所选选项是正确的
                    if (bean.answer == 1){
                        vh.iv_a.setImageResource(R.drawable.exercises_right_icon);
                        vh.iv_b.setImageResource(R.drawable.exercises_b);
                        vh.iv_c.setImageResource(R.drawable.exercises_c);
                        vh.iv_d.setImageResource(R.drawable.exercises_d);
                    }else if (bean.answer == 2){
                        vh.iv_a.setImageResource(R.drawable.exercises_a);
                        vh.iv_b.setImageResource(R.drawable.exercises_right_icon);
                        vh.iv_c.setImageResource(R.drawable.exercises_c);
                        vh.iv_d.setImageResource(R.drawable.exercises_d);
                    }else if (bean.answer == 3){
                        vh.iv_a.setImageResource(R.drawable.exercises_a);
                        vh.iv_b.setImageResource(R.drawable.exercises_b);
                        vh.iv_c.setImageResource(R.drawable.exercises_right_icon);
                        vh.iv_d.setImageResource(R.drawable.exercises_d);
                    }else if (bean.answer == 4){
                        vh.iv_a.setImageResource(R.drawable.exercises_a);
                        vh.iv_b.setImageResource(R.drawable.exercises_b);
                        vh.iv_c.setImageResource(R.drawable.exercises_c);
                        vh.iv_d.setImageResource(R.drawable.exercises_right_icon);
                    }
                    break;
                case 1:  //用户所选选项A是错误的
                    vh.iv_a.setImageResource(R.drawable.exercises_error_icon);
                    if (bean.answer == 2){
                        vh.iv_b.setImageResource(R.drawable.exercises_right_icon);
                        vh.iv_c.setImageResource(R.drawable.exercises_c);
                        vh.iv_d.setImageResource(R.drawable.exercises_d);
                    }else if (bean.answer == 3){
                        vh.iv_b.setImageResource(R.drawable.exercises_b);
                        vh.iv_c.setImageResource(R.drawable.exercises_right_icon);
                        vh.iv_d.setImageResource(R.drawable.exercises_d);
                    }else if (bean.answer == 4){
                        vh.iv_b.setImageResource(R.drawable.exercises_b);
                        vh.iv_c.setImageResource(R.drawable.exercises_c);
                        vh.iv_d.setImageResource(R.drawable.exercises_right_icon);
                    }
                    break;
                case 2:  //用户所选选项B是错误的
                    vh.iv_b.setImageResource(R.drawable.exercises_error_icon);
                    if (bean.answer == 1){
                        vh.iv_a.setImageResource(R.drawable.exercises_right_icon);
                        vh.iv_c.setImageResource(R.drawable.exercises_c);
                        vh.iv_d.setImageResource(R.drawable.exercises_d);
                    }else if (bean.answer == 3){
                        vh.iv_a.setImageResource(R.drawable.exercises_a);
                        vh.iv_c.setImageResource(R.drawable.exercises_right_icon);
                        vh.iv_d.setImageResource(R.drawable.exercises_d);
                    }else if (bean.answer == 4){
                        vh.iv_a.setImageResource(R.drawable.exercises_a);
                        vh.iv_c.setImageResource(R.drawable.exercises_c);
                        vh.iv_d.setImageResource(R.drawable.exercises_right_icon);
                    }
                    break;
                case 3:  //用户所选选项C是错误的
                    vh.iv_c.setImageResource(R.drawable.exercises_error_icon);
                    if (bean.answer == 1){
                        vh.iv_a.setImageResource(R.drawable.exercises_right_icon);
                        vh.iv_b.setImageResource(R.drawable.exercises_b);
                        vh.iv_d.setImageResource(R.drawable.exercises_d);
                    }else if (bean.answer == 2){
                        vh.iv_a.setImageResource(R.drawable.exercises_a);
                        vh.iv_b.setImageResource(R.drawable.exercises_right_icon);
                        vh.iv_d.setImageResource(R.drawable.exercises_d);
                    }else if (bean.answer == 4){
                        vh.iv_a.setImageResource(R.drawable.exercises_a);
                        vh.iv_b.setImageResource(R.drawable.exercises_b);
                        vh.iv_d.setImageResource(R.drawable.exercises_right_icon);
                    }
                    break;
                case 4:  //用户所选选项D是错误的
                    vh.iv_c.setImageResource(R.drawable.exercises_error_icon);
                    if (bean.answer == 1){
                        vh.iv_a.setImageResource(R.drawable.exercises_right_icon);
                        vh.iv_b.setImageResource(R.drawable.exercises_b);
                        vh.iv_c.setImageResource(R.drawable.exercises_c);
                    }else if (bean.answer == 2){
                        vh.iv_a.setImageResource(R.drawable.exercises_a);
                        vh.iv_b.setImageResource(R.drawable.exercises_right_icon);
                        vh.iv_c.setImageResource(R.drawable.exercises_c);
                    }else if (bean.answer == 3){
                        vh.iv_a.setImageResource(R.drawable.exercises_a);
                        vh.iv_b.setImageResource(R.drawable.exercises_b);
                        vh.iv_c.setImageResource(R.drawable.exercises_right_icon);
                    }
                    break;
                default:
                    break;
            }
        }
        
        //当用户点击A选项的点击事件
        vh.iv_a.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                if (selectedPosition.contains("" + position)){
                    selectedPosition.remove("" + position);
                }else {
                    selectedPosition.add(position + "");
                }
                onSelectListener.onSelectA(position,vh.iv_a,vh.iv_b,vh.iv_c,vh.iv_d);
            }
        });
        
      //当用户点击B选项的点击事件
        vh.iv_b.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                if (selectedPosition.contains("" + position)){
                    selectedPosition.remove("" + position);
                }else {
                    selectedPosition.add(position + "");
                }
                onSelectListener.onSelectA(position,vh.iv_a,vh.iv_b,vh.iv_c,vh.iv_d);
            }
        });
        
      //当用户点击C选项的点击事件
        vh.iv_c.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                if (selectedPosition.contains("" + position)){
                    selectedPosition.remove("" + position);
                }else {
                    selectedPosition.add(position + "");
                }
                onSelectListener.onSelectA(position,vh.iv_a,vh.iv_b,vh.iv_c,vh.iv_d);
            }
        });
        
      //当用户点击D选项的点击事件
        vh.iv_d.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                if (selectedPosition.contains("" + position)){
                    selectedPosition.remove("" + position);
                }else {
                    selectedPosition.add(position + "");
                }
                onSelectListener.onSelectA(position,vh.iv_a,vh.iv_b,vh.iv_c,vh.iv_d);
            }
        });
        return convertView;
    }
    class ViewHolder{
        public TextView subject,tv_a,tv_b,tv_c,tv_d;
        public ImageView iv_a,iv_b,iv_c,iv_d;
    }
    public interface OnSelectListener{
        void onSelectA(int position,ImageView iv_a,ImageView iv_b,ImageView iv_c,ImageView iv_d);
        void onSelectB(int position,ImageView iv_a,ImageView iv_b,ImageView iv_c,ImageView iv_d);
        void onSelectC(int position,ImageView iv_a,ImageView iv_b,ImageView iv_c,ImageView iv_d);
        void onSelectD(int position,ImageView iv_a,ImageView iv_b,ImageView iv_c,ImageView iv_d);
    }
}

5、习题详情界面逻辑代码——ExercisesDetailActivity.java

package com.boxuegu.activity;

import android.content.pm.ActivityInfo;
import com.boxuegu.R;
import com.boxuegu.adapter.ExrecisesDetailAdapter;
import com.boxuegu.bean.ExercisesBean;
import com.boxuegu.utils.AnalysisUtils;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;



public class ExercisesDetailActivity extends AppCompatActivity {
    private TextView tv_main_title;
    private TextView tv_back;
    private RelativeLayout rl_title_bar;
    private ListView lv_list;
    private String title;
    private int id;
    private List<ExercisesBean> ebl;
    private ExrecisesDetailAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exercises_detail);
        //设置界面为竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        //获取从习题界面传递过来的章节ID
        id = getIntent().getIntExtra("id",0);
      //获取从习题界面传递过来的章节标题
        title = getIntent().getStringExtra("title");
        ebl = new ArrayList<ExercisesBean>();
        initData();
        init();
    }
    private void initData(){
        try {
            //从XML文件中获取习题数据
            InputStream is = getResources().getAssets().open("chapter" + id +".xml");
            ebl = AnalysisUtils.getExercisesInfos(is);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //初始化控件
    private void init(){
        tv_main_title = (TextView) findViewById(R.id.tv_main_title);
        tv_back = (TextView) findViewById(R.id.tv_back);
        rl_title_bar = (RelativeLayout) findViewById(R.id.title_bar);
        rl_title_bar.setBackgroundColor(Color.parseColor("#30B4FF"));
        lv_list = (ListView)findViewById(R.id.lv_list);
        TextView tv = new TextView(this);
        tv.setTextColor(Color.parseColor("#000000"));
        tv.setTextSize(16.0f);
        tv.setText("第一部分、选择题(单选)");
        tv.setPadding(10,15,0,0);
        lv_list.addHeaderView(tv);
        tv_main_title.setText(title);
        tv_back.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                ExercisesDetailActivity.this.finish();
            }
        });
        adapter = new ExrecisesDetailAdapter(ExercisesDetailActivity.this,new ExrecisesDetailAdapter.OnSelectListener(){
            @Override
            public void onSelectD(int postion, ImageView iv_a, ImageView iv_b, ImageView iv_c, ImageView iv_d){
                //判断如果答案不是4即D答案
                if (ebl.get(postion).answer != 4){
                    ebl.get(postion).select=4;
                }else {
                    ebl.get(postion).select = 0;
                }
                switch (ebl.get(postion).answer){
                    case 1:
                        iv_a.setImageResource(R.drawable.exercises_right_icon);
                        iv_d.setImageResource(R.drawable.exercises_error_icon);
                        break;
                    case 2:
                        iv_b.setImageResource(R.drawable.exercises_right_icon);
                        iv_d.setImageResource(R.drawable.exercises_error_icon);
                        break;
                    case 3:
                        iv_c.setImageResource(R.drawable.exercises_right_icon);
                        iv_d.setImageResource(R.drawable.exercises_error_icon);
                        break;
                    case 4:
                        iv_d.setImageResource(R.drawable.exercises_right_icon);
                        break;
                }
                AnalysisUtils.setABCDEnable(false,iv_a,iv_b,iv_c,iv_d);
            }
            @Override
            public void onSelectC(int postion, ImageView iv_a, ImageView iv_b, ImageView iv_c, ImageView iv_d){
                //判断如果答案不是3即C答案
                if (ebl.get(postion).answer != 3){
                    ebl.get(postion).select=3;
                }else {
                    ebl.get(postion).select = 0;
                }
                switch (ebl.get(postion).answer){
                    case 1:
                        iv_a.setImageResource(R.drawable.exercises_right_icon);
                        iv_c.setImageResource(R.drawable.exercises_error_icon);
                        break;
                    case 2:
                        iv_b.setImageResource(R.drawable.exercises_right_icon);
                        iv_c.setImageResource(R.drawable.exercises_error_icon);
                        break;
                    case 3:
                        iv_c.setImageResource(R.drawable.exercises_right_icon);
                        break;
                    case 4:
                        iv_c.setImageResource(R.drawable.exercises_error_icon);
                        iv_d.setImageResource(R.drawable.exercises_right_icon);
                        break;
                }
                AnalysisUtils.setABCDEnable(false,iv_a,iv_b,iv_c,iv_d);
            }
            @Override
            public void onSelectB(int postion, ImageView iv_a, ImageView iv_b, ImageView iv_c, ImageView iv_d){
                //判断如果答案不是2即B答案
                if (ebl.get(postion).answer != 2){
                    ebl.get(postion).select=2;
                }else {
                    ebl.get(postion).select = 0;
                }
                switch (ebl.get(postion).answer){
                    case 1:
                        iv_a.setImageResource(R.drawable.exercises_right_icon);
                        iv_b.setImageResource(R.drawable.exercises_error_icon);
                        break;
                    case 2:
                        iv_b.setImageResource(R.drawable.exercises_right_icon);
                        break;
                    case 3:
                        iv_c.setImageResource(R.drawable.exercises_right_icon);
                        iv_b.setImageResource(R.drawable.exercises_error_icon);
                        break;
                    case 4:
                        iv_d.setImageResource(R.drawable.exercises_right_icon);
                        iv_b.setImageResource(R.drawable.exercises_error_icon);
                        break;
                }
                AnalysisUtils.setABCDEnable(false,iv_a,iv_b,iv_c,iv_d);
            }
            @Override
            public void onSelectA(int postion, ImageView iv_a, ImageView iv_b, ImageView iv_c, ImageView iv_d){
                //判断如果答案不是1即A答案
                if (ebl.get(postion).answer != 1){
                    ebl.get(postion).select=1;
                }else {
                    ebl.get(postion).select = 0;
                }
                switch (ebl.get(postion).answer){
                    case 1:
                        iv_a.setImageResource(R.drawable.exercises_right_icon);
                        break;
                    case 2:
                        iv_b.setImageResource(R.drawable.exercises_right_icon);
                        iv_a.setImageResource(R.drawable.exercises_error_icon);
                        break;
                    case 3:
                        iv_c.setImageResource(R.drawable.exercises_right_icon);
                        iv_a.setImageResource(R.drawable.exercises_error_icon);
                        break;
                    case 4:
                        iv_d.setImageResource(R.drawable.exercises_right_icon);
                        iv_a.setImageResource(R.drawable.exercises_error_icon);
                        break;
                }
                AnalysisUtils.setABCDEnable(false,iv_a,iv_b,iv_c,iv_d);
            }

        });
        adapter.setData(ebl);
        lv_list.setAdapter(adapter);
    }

}

6、修改界面代码

(1)、修改习题界面数据适配器

找到ExercisesAdapter.java文件,在注释//跳转到习题详情界面下方添加如下代码:
Intent intent = new Intent(mContext, ExercisesDetailActivity.class);
ntent.putExtra("id",bean.id);
intent.putExtra("title",bean.title);
mContext.startActivity(intent);

(2)、设置A,B,C,D四个选项是否可以点击

找到AnalysisUtils.java文件,添加如下代码:
//设置A,B,C,D4个选项是否可以被点击
public static void setABCDEnable(boolean value, ImageView iv_a,ImageView iv_b, ImageView iv_c,ImageView iv_d){
iv_a.setEnabled(value);
iv_b.setEnabled(value);
iv_c.setEnabled(value);
iv_d.setEnabled(value);
        }

三,运行效果

Android项目实战系列—基于博学谷(六)习题模块

Android项目实战系列—基于博学谷 开源地址

Android项目实战系列—基于博学谷(六)习题模块                 Android项目实战系列—基于博学谷(六)习题模块

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
待兔 待兔
4个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Stella981 Stella981
3年前
Android项目实战系列—基于博学谷(七)课程模块(下)
!image(https://www.cztcms.cn/wpcontent/uploads/2020/03/%E5%8D%9A%E5%AD%A6%E8%B0%B7.png)由于这个模块内容较多,分为上、中、下三篇博客分别来讲述,请耐心阅读。课程模块分为四个部分\\课程列表\\课
Stella981 Stella981
3年前
Android项目实战系列—基于博学谷(一)项目综述
!image(https://www.cztcms.cn/wpcontent/uploads/2020/03/%E5%8D%9A%E5%AD%A6%E8%B0%B7.png)一、项目分析1、项目名称WordPress建站APP2、项目概述WordPress建站是一个学习博客建站技术的APP
Stella981 Stella981
3年前
Android项目实战系列—基于博学谷(四)我的模块(上)
!image(https://www.cztcms.cn/wpcontent/uploads/2020/03/%E5%8D%9A%E5%AD%A6%E8%B0%B7.png)由于这个模块内容较多,篇幅较长,请耐心阅读。“我”的模块分为四个部分\x\我的界面\x\设置界面\
Stella981 Stella981
3年前
Android项目实战系列—基于博学谷(四)我的模块(下)
!image(https://www.cztcms.cn/wpcontent/uploads/2020/03/%E5%8D%9A%E5%AD%A6%E8%B0%B7.png)由于这个模块内容较多,篇幅较长,请耐心阅读。“我”的模块分为四个部分\\我的界面\\设置界面\x
Stella981 Stella981
3年前
Android项目实战系列—基于博学谷(三)注册与登录模块
!image(https://www.cztcms.cn/wpcontent/uploads/2020/03/%E5%8D%9A%E5%AD%A6%E8%B0%B7.png)由于这个模块内容较多,篇幅较长,请耐心阅读。注册与登录模块分为三个部分\x\欢迎界面\x\注册界面\
Stella981 Stella981
3年前
Android项目实战系列—基于博学谷(七)课程模块(中)
!image(https://www.cztcms.cn/wpcontent/uploads/2020/03/%E5%8D%9A%E5%AD%A6%E8%B0%B7.png)由于这个模块内容较多,分为上、中、下三篇博客分别来讲述,请耐心阅读。课程模块分为四个部分\\课程列表\x\课
Stella981 Stella981
3年前
Android项目实战系列—基于博学谷(七)课程模块(上)
!image(https://www.cztcms.cn/wpcontent/uploads/2020/03/%E5%8D%9A%E5%AD%A6%E8%B0%B7.png)由于这个模块内容较多,分为上、中、下三篇博客分别来讲述,请耐心阅读。课程模块分为四个部分\x\课程列表\\课
Stella981 Stella981
3年前
Android项目实战系列—基于博学谷(五)个人资料
!image(https://www.cztcms.cn/wpcontent/uploads/2020/03/%E5%8D%9A%E5%AD%A6%E8%B0%B7.png)由于这个模块内容较多,篇幅较长,请耐心阅读。个人资料模块分为两个部分\x\个人资料\x\资料修改