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

android项目实战-背呗单词DEV07-单词测试实现

时间:2016-05-18 12:34:01      阅读:375      评论:0      收藏:0      [点我收藏+]

标签:

该篇说明

该篇我们进行单词测试实现讲解

也就是单词测试界面WordExamActivity中功能

布局也需要在xml中实现

但是也需要有单词测试视图类来设置一些值具体查看对照源码

在该界面中,测试完后会更新数据

 

效果如图

技术分享

 

流程图

 技术分享

 

单词测试界面功能流程详解

1> 显示单词测试信息

a> 首先要加载单词测试数据

需要调用单词测试管理模块加载数据功能

private void loadWordExamData() {
      loadingExamDataProgressBar = (ProgressBar)
         findViewById(R.id.loadingExamDataProgressBar);
      loadingExamDataProgressBar.setVisibility(View.VISIBLE);

      new Thread() {
            public void run() {
                new WordExamManager().loadData();
                SystemUtil.sleepvd(2000);
                myHandler.sendEmptyMessage(0);
            }
      }.start();
   }

b> handler处理0的消息并初始及处理逻辑

@SuppressLint("HandlerLeak")
   private void initHandler() {
      myHandler = new Handler() {
         public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 0:
                   wordExamLinearLayout.setVisibility(View.VISIBLE);
                   loadingExamDataProgressBar.setVisibility(View.GONE);
                   init();
                   logic();
                   break;
            }
         }
      };
   }

c> 接着在初始中加载显示单词测试界面

private void loadView() {
      //正确选项随机数
      Global.rightOptionRandom = Math.floor(Math.random() * 4 + 1);
      WordDet wordDet = Global.wordDetList.get(Global.studyedWordId - 1);

      //单词测试标题
      wordExamTitelTextView.setText(wordDet.wordStr + String.format(" /%s/", wordDet.pron));
      WordExamView.initOptions(this);
      WordExamView.setOptionsData(wordDet);
}

 

2> 四个选项添加监听

来实现点击判断选择的正确还是错误。进一步确定执行

监听类请自行查看源码

private void logic() {
      for(int i=1; i<=4; i++) {
         LinearLayout wordExamOptionll = WordExamView.weOptionLLayoutList.get(i-1);
         wordExamOptionll.setOnClickListener(new WordExamOptionClickListener(wordExamOptionll, i));
      }
}

 

3> 测试正确后下一个

a> 跳转下一个

单选正确后自动跳转到下一个

如果是已是最后一个,那么更新数据

public void examNext() {
      Global.studyedWordId += 1;

      if(Global.studyedWordId <= Global.wordList.size()) {
         timer.schedule(new TimerTask() {
            @Override
            public void run() {
                myHandler.sendEmptyMessage(1);
            }
         }, 1000);
      } else if((Global.studyedWordId > Global.wordList.size())) {
         updateData();
      }
}

b> 使用计时器控制1秒后跳转

Handler异步任务处理1的消息测试下个单词

@SuppressLint("HandlerLeak")
   private void initHandler() {
      myHandler = new Handler() {
         public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 1:
                   if(timer != null) {
                      timer.cancel();
                   }
                   timer = new Timer();

                   showNextView();
                   break;
            }
         }
      };
 }

c> 更新数据

如果测试完最后一个,就进行更新数据。

如果从单词背诵界面跳转的,才更新记录次数

private void updateData() {
      clear();

      if(parentStr != null && parentStr.equals("wordDet")) {
         updateDataProgressDialog.setMessage("正在更新数据");
         updateDataProgressDialog.show();     

         new Thread(){
            @Override
            public void run() {
                updateRecordTimes();
            }
         }.start();
      } else if(parentStr.equals("wordList")) {
            Toast.makeText(context,
            Global.selectUnitGroupFlagStr + "单元测组试完,回到单词列表界面!!!",          
            Toast.LENGTH_LONG).show(); 
            toNextView(WordListActivity.class); 
      }
 }

d> 更新次数

> 没有完成次数进度才可设置需要更新数据

> 如果结果记录次数等于需要背诵的次数,则返回到今天任务

进度归0(会存储到数据库),同时需要清空单词记录表及单词表

> 否则返回到单词列表,递增进度(不保存到数据库)

private void updateRecordTimes() {
int recitedtimes = Integer.parseInt(Global.studyPlanObj.lastReciteProgressStr.split("/")[0]); int resultRtimes = 0; if(recitedtimes != Global.defaultReciteTimes) { resultRtimes = recitedtimes + 1; Global.studyPlanObj.lastReciteProgressStr = resultRtimes + "/" + Global.defaultReciteTimes; if(resultRtimes == Global.defaultReciteTimes) { new ReciteManager().updateData(); SystemUtil.sleepvd(2000); myHandler.sendEmptyMessage(2); } else { new ReciteManager().updateReciteProgress(Global.studyPlanObj.lastReciteProgressStr); SystemUtil.sleepvd(2000); myHandler.sendEmptyMessage(3); } } }

 

4> 测试错误后的操作

测试错误后显示按钮操作

> 返回学习按钮,返回单词列表界面进行学习

> 继续测试按钮,重新开始测试

public void showButtonAfterNotRight() {
      returnToStudyButton.setVisibility(View.VISIBLE);
      continueExamButton.setVisibility(View.VISIBLE);

      returnToStudyButton.setOnClickListener(new OnClickListener() {
         @Override
         public void onClick(View v) {
            clear();
            toNextView(WordListActivity.class);
         }
      });

      continueExamButton.setOnClickListener(new OnClickListener() {
         @Override
         public void onClick(View v) {
            Global.studyedWordId = 1;
            Global.examedWordId = 1;

            returnToStudyButton.setVisibility(View.GONE);
            continueExamButton.setVisibility(View.GONE);
            showNextView();
         }
      });  
}

 

单词测试后台功能详解

在加载数据以及更新次数方法使用到了后台模块

使用到了如下模块功能

> 单词测试管理WordExamManager的加载数据loadData()方法

> 背诵管理ReciteManager的更新背诵次数updateReciteProgress()方法

1> 单词测试管理加载数据详解

a> 首先是加载数据方法

public void loadData() {
      if(Global.examWordDetList == null) {
         if(wordDetManager == null) {
            wordDetManager = new WordDetManager();
         }

         List<Word> tmexamWordList = getExamWordList();
         Global.examWordDetList  = wordDetManager.findMoreWordDet(tmexamWordList);
      }
}

b> 获得测试单词范围

private List<Word> getExamWordList() {
      //确定单元及组
      String unitGroupIntro = Global.todayStudyUnitGroupIntroList.get(Global.studyPlanObj.lastUnitGroupIndex);
      int cunit = Integer.parseInt(unitGroupIntro.split("-")[0]);
      int cgroup = Integer.parseInt(unitGroupIntro.split("-")[1]);
      int scopeBegin = -1;
      int scopeEnd = -1;
 
      //确定范围
      if(cunit == 1 && cgroup <= 3) {
         scopeBegin = (cunit - 1) * ReciteConstant.DUNIT_WORD_COUNT + (cgroup+1) * ReciteConstant.DUNITG_WORD_COUNT;
         scopeEnd = scopeBegin + 3 * ReciteConstant.DUNITG_WORD_COUNT;
      } else {
         scopeEnd = (cunit - 1) * ReciteConstant.DUNIT_WORD_COUNT + cgroup * ReciteConstant.DUNITG_WORD_COUNT;
         if(scopeEnd > Global.studyPlanObj.allWordCount) {
            scopeEnd = Global.studyPlanObj.allWordCount - ReciteConstant.DUNITG_WORD_COUNT;
         }
         scopeBegin = scopeEnd - 3 * ReciteConstant.DUNITG_WORD_COUNT;
      }

     
      if(wordDao == null) {
         wordDao = new WordDao();
      }
      String pathStr = PathConstant.STUPLAN_PATH + "sp_" + Global.studyPlanObj.name;
      return wordDao.findScope(scopeBegin, scopeEnd, pathStr);
}

c> 根据单词列表得到单词详解列表

这里引用单词详解模块WordDetManager的查找更多单词详解方法

请查看源码点到该类

public List<WordDet> findMoreWordDet(List<Word> wordList) {
      List<WordDet> wordDetList = new ArrayList<WordDet>();

      for(Word word : wordList) {
         WordDet wordDet = findDetWord(word.wordStr);
         if(wordDet != null) {
            wordDetList.add(wordDet);
         }
      }
   
return wordDetList; }

查找某个单词的单词详解方法

public WordDet findDetWord(String wordStr) {
      if(dictDao == null) {
         dictDao = new DictDao();
      }

      String wordDefinition = dictDao.findWordDefinition(wordStr);
      WordDet wordDet = jsonDefiToDefWord(wordStr, wordDefinition);
      return wordDet;
}

 

2> 背诵管理更新次数详解

更新背诵次数,直接调用相应的dao即可

public void updateReciteProgress(String reciteProgressStr) {
      if(reciteDao == null) {
         reciteDao = new ReciteDao ();
      }

      String pathStr = PathConstant.STUPLAN_PATH + "sp_" + Global.studyPlanObj.name;
      reciteDao.updateReciteProgress(reciteProgressStr, Global.studyPlanObj.name, pathStr);
}

 

 

到此《android项目实战-背呗单词》重点功能及技术介绍完结

更多请自行下载源码解读

应用下载:请点击

源码下载:请点击

android项目实战-背呗单词DEV07-单词测试实现

标签:

原文地址:http://www.cnblogs.com/vomzoo/p/5504401.html

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