热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

基于Android实现答题倒计时功能

这篇文章主要介绍了基于Android实现答题倒计时功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

讲一下我在做一个答题APP时涉及到倒计时时遇到的一个问题吧。
碎片(Fragment)+CountDownTimer组成的一个答题,其中遇到的一个问题就是,这个题的倒计时在你手动滑动下一个题的时候却用在了下一个题的时间
解决这个问题运用的就是懒加载来控制倒计时的开始和取消

首先你要先定义一个抽象类继承Fragment 再让你的答题那个碎片的Activity继承

package com.zking.sun.dao;

import android.support.v4.app.Fragment;
import android.util.Log;

/**
 * Created by sun on 2017/1/11.
 */

public abstract class LazyFragment extends Fragment {
  protected boolean isVisible;
  /**
   * 在这里实现Fragment数据的缓加载.
   * @param isVisibleToUser
   */
  @Override
  public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if(getUserVisibleHint()) {
      //可见时调用
      isVisible = true;
      onVisible();
    } else {
      isVisible = false;
      onInvisible();
    }
  }
  protected abstract void onVisible();
  //protected abstract void lazyLoad();
  protected abstract void onInvisible();
}

这是答题的Activity 在这里你要继承刚刚自己写的抽象类
这个类里面包含了数据的加载什么的,有需要的童鞋可以看看,就不删了哈。

package com.zking.sun.android_06_project;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.zking.sun.dao.LazyFragment;
import com.zking.sun.dao.QusetionDao;
import com.zking.sun.entity.QuestionEntity;

import java.util.List;

import static com.zking.sun.android_06_project.R.id.tv_splash_01;

/**
 * Created by sun on 2016/12/21.
 */

public class FragmentActivity extends LazyFragment {
  private ViewPager viewpager_main_01;
  private TextView question_fragment_tv;
  private RadioButton answer_fragment_01,answer_fragment_02,answer_fragment_03,answer_fragment_04;
  private QusetionDao qusetiOnDao=new QusetionDao();
  private int i;
  private RadioGroup rg_fragment_qu;
  private String right_answer;
  private TextView count_fragment_down;
  private int SPLASH_DISPLAY_LENGHT = 10000; //延迟多少秒
  private TextView tv_splash_01;
  private Handler handler = new Handler();
  private Runnable runnbale ;
  private Intent intent;
  private MyCountdownTimer countdowntimer;
  private TextView questionR_fragment_tv;
  private boolean isPrepared;

  public FragmentActivity(){
  }
  public FragmentActivity(int i){
    this.i=i;
  }
  public int getI() {
    return i;
  }
  public void setI(int i) {
    this.i = i;
  }


  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v=inflater.inflate(R.layout.fragment_1,null);
    //找到问题和答案的控件 
    question_fragment_tv = (TextView) v.findViewById(R.id.question_fragment_tv);
    questionR_fragment_tv = (TextView) v.findViewById(R.id.questionR_fragment_tv);
    questionR_fragment_tv.setVisibility(View.INVISIBLE);
    answer_fragment_01 = (RadioButton) v.findViewById(R.id.answer_fragment_01);
    answer_fragment_02 = (RadioButton) v.findViewById(R.id.answer_fragment_02);
    answer_fragment_03 = (RadioButton) v.findViewById(R.id.answer_fragment_03);
    answer_fragment_04 = (RadioButton) v.findViewById(R.id.answer_fragment_04);
    rg_fragment_qu = (RadioGroup) v.findViewById(R.id.rg_fragment_qu);
    count_fragment_down = (TextView) v.findViewById(R.id.count_fragment_down);
    //倒计时
    countdowntimer = new MyCountdownTimer(10000, 1000);
    //绑定值 获取页面的监听的i 传过来改变
    isPrepared = true;
    //懒加载
    getvalue(this.i);
    onVisible();//可见
    onInvisible();//不可见
    // lazyLoad();

    return v;
  }


  public void getvalue(int i){
    //查询数据
    /**
     * @param context 上下文
     * @param name  名字(数据库名),文件名
     * @param factory 游标工厂,多数情况:null
     * @param version 数据库版本
     */
    //DBHepler dbHepler=new DBHepler(this,"questions.db",null,1);
    List questiOnEntityList=qusetionDao.findAll(getContext());
    right_answer = questionEntityList.get(i).getRight_answer();
    questionR_fragment_tv.setText("答案:"+right_answer);
    /* if (right_answer.equalsIgnoreCase("A")){
      right_answer = "answer_fragment_01";
    }*/


    //将查询出来的数据放到控件里面
    question_fragment_tv.setText(questionEntityList.get(i).getQusetion());
    answer_fragment_01.setText(questionEntityList.get(i).getAnswera());
    answer_fragment_02.setText(questionEntityList.get(i).getAnswerb());
    answer_fragment_03.setText(questionEntityList.get(i).getAnswerc());

    String this04=questionEntityList.get(i).getAnswerd()+"";
    Log.i("answer_fragment_04","_____________"+this04+"_____________");
    if(this04.equals("")||this04.equals("null")){
      answer_fragment_04.setVisibility(View.INVISIBLE);
    }
    else{
      answer_fragment_04.setText(questionEntityList.get(i).getAnswerd());
      answer_fragment_04.setVisibility(View.VISIBLE);
    }



    //get组设点击事件
    rg_fragment_qu.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(RadioGroup group, int checkedId) {
        rg_fragment_qu.setEnabled(false);
        int selectRadio = group.getCheckedRadioButtonId();
        switch (selectRadio){
          case R.id.answer_fragment_01:
            // countdowntimer.cancel();
            if (right_answer.equalsIgnoreCase("A")){
              answer_fragment_01.setBackgroundResource(R.drawable.examtxt_btn_right);
            }
            else{
              answer_fragment_01.setBackgroundResource(R.drawable.examtxt_btn_wrong);
              questionR_fragment_tv.setVisibility(View.VISIBLE);
            }
            answer_fragment_02.setEnabled(false);
            answer_fragment_03.setEnabled(false);
            answer_fragment_04.setEnabled(false);
            break;
          case R.id.answer_fragment_02:
            //countdowntimer.cancel();
            if (right_answer.equalsIgnoreCase("B")){
              answer_fragment_02.setBackgroundResource(R.drawable.examtxt_btn_right);
            }
            else{
              answer_fragment_02.setBackgroundResource(R.drawable.examtxt_btn_wrong);
              questionR_fragment_tv.setVisibility(View.VISIBLE);
            }
            answer_fragment_01.setEnabled(false);
            answer_fragment_03.setEnabled(false);
            answer_fragment_04.setEnabled(false);
            break;
          case R.id.answer_fragment_03:
            //countdowntimer.cancel();
            if (right_answer.equalsIgnoreCase("C")){
              answer_fragment_03.setBackgroundResource(R.drawable.examtxt_btn_right);
            }
            else{
              answer_fragment_03.setBackgroundResource(R.drawable.examtxt_btn_wrong);
              questionR_fragment_tv.setVisibility(View.VISIBLE);
            }
            answer_fragment_02.setEnabled(false);
            answer_fragment_01.setEnabled(false);
            answer_fragment_04.setEnabled(false);
            break;
          case R.id.answer_fragment_04:
            //countdowntimer.cancel();
            if (right_answer.equalsIgnoreCase("D")){
              answer_fragment_04.setBackgroundResource(R.drawable.examtxt_btn_right);
            }
            else{
              answer_fragment_04.setBackgroundResource(R.drawable.examtxt_btn_wrong);
              questionR_fragment_tv.setVisibility(View.VISIBLE);
            }
            answer_fragment_02.setEnabled(false);
            answer_fragment_03.setEnabled(false);
            answer_fragment_01.setEnabled(false);
            break;
        }


      }
    });
  }




  /**
   * Rewrite 'CountDownTimer' method.
   *
   * @param
   *      // 倒计时总数,单位为毫秒。
   * @param
   *      // 每隔多久调用onTick一次
   * @author DaiZhenWei
   *
   */
    protected class MyCountdownTimer extends CountDownTimer {

      public MyCountdownTimer(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
      }
      @Override
      public void onTick(long millisUntilFinished) {
        count_fragment_down.setText("倒计时: " + millisUntilFinished / 1000);
      }
      @Override
      public void onFinish() {
        //count_fragment_down.setText("Turning");
        FightActivity.getNext(null);
      }
    }

  //fragment的懒加载 重写
  @Override
  protected void onVisible() {
    //可见的
    if(!isPrepared || !isVisible) {
      //判断isPrepared和isVisible只要有一个不为true就不往下执行
      Log.i("isPrepared",isPrepared+"____________"+isVisible);
      return;
    }
    /**
     * 倒计时
     */
    countdowntimer.start();//开始倒计时
    Log.i("isPrepared",this.i+"_______4");
  }
  @Override
  protected void onInvisible() {
    //不可见的
    if(!isPrepared || isVisible) {
      return;
    }
    Log.i("isPrepared","____________我取消了"+this.i);
    countdowntimer.cancel();//将倒计时取消

  }
/*
   //主页面
  public void loadUI(Class c){
    //启动之后跳著页面
//    Intent intent=new Intent(SplashActivity.this,MainActivity.class);
    Intent intent=new Intent(FragmentActivity.this.getContext(),c);
//    SplashActivity.this.startActivity(intent);
//    SplashActivity.this.finish();//Toast.LENGTH_LONG
  }
*/

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


推荐阅读
  • 2023 ARM嵌入式系统全国技术巡讲旨在分享ARM公司在半导体知识产权(IP)领域的最新进展。作为全球领先的IP提供商,ARM在嵌入式处理器市场占据主导地位,其产品广泛应用于90%以上的嵌入式设备中。此次巡讲将邀请来自ARM、飞思卡尔以及华清远见教育集团的行业专家,共同探讨当前嵌入式系统的前沿技术和应用。 ... [详细]
  • Android 九宫格布局详解及实现:人人网应用示例
    本文深入探讨了人人网Android应用中独特的九宫格布局设计,解析其背后的GridView实现原理,并提供详细的代码示例。这种布局方式不仅美观大方,而且在现代Android应用中较为少见,值得开发者借鉴。 ... [详细]
  • CSS 布局:液态三栏混合宽度布局
    本文介绍了如何使用 CSS 实现液态的三栏布局,其中各栏具有不同的宽度设置。通过调整容器和内容区域的属性,可以实现灵活且响应式的网页设计。 ... [详细]
  • Linux 系统启动故障排除指南:MBR 和 GRUB 问题
    本文详细介绍了 Linux 系统启动过程中常见的 MBR 扇区和 GRUB 引导程序故障及其解决方案,涵盖从备份、模拟故障到恢复的具体步骤。 ... [详细]
  • 本文介绍了如何使用jQuery根据元素的类型(如复选框)和标签名(如段落)来获取DOM对象。这有助于更高效地操作网页中的特定元素。 ... [详细]
  • 本文将详细介绍如何使用剪映应用中的镜像功能,帮助用户轻松实现视频的镜像效果。通过简单的步骤,您可以快速掌握这一实用技巧。 ... [详细]
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • 本文介绍如何在 Xcode 中使用快捷键和菜单命令对多行代码进行缩进,包括右缩进和左缩进的具体操作方法。 ... [详细]
  • 本文介绍了一款用于自动化部署 Linux 服务的 Bash 脚本。该脚本不仅涵盖了基本的文件复制和目录创建,还处理了系统服务的配置和启动,确保在多种 Linux 发行版上都能顺利运行。 ... [详细]
  • 在Linux系统中配置并启动ActiveMQ
    本文详细介绍了如何在Linux环境中安装和配置ActiveMQ,包括端口开放及防火墙设置。通过本文,您可以掌握完整的ActiveMQ部署流程,确保其在网络环境中正常运行。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • 如何在WPS Office for Mac中调整Word文档的文字排列方向
    本文将详细介绍如何使用最新版WPS Office for Mac调整Word文档中的文字排列方向。通过这些步骤,用户可以轻松更改文本的水平或垂直排列方式,以满足不同的排版需求。 ... [详细]
  • 本文总结了在使用Ionic 5进行Android平台APK打包时遇到的问题,特别是针对QRScanner插件的改造。通过详细分析和提供具体的解决方法,帮助开发者顺利打包并优化应用性能。 ... [详细]
  • 理解存储器的层次结构有助于程序员优化程序性能,通过合理安排数据在不同层级的存储位置,提升CPU的数据访问速度。本文详细探讨了静态随机访问存储器(SRAM)和动态随机访问存储器(DRAM)的工作原理及其应用场景,并介绍了存储器模块中的数据存取过程及局部性原理。 ... [详细]
  • 360SRC安全应急响应:从漏洞提交到修复的全过程
    本文详细介绍了360SRC平台处理一起关键安全事件的过程,涵盖从漏洞提交、验证、排查到最终修复的各个环节。通过这一案例,展示了360在安全应急响应方面的专业能力和严谨态度。 ... [详细]
author-avatar
用释怀来成全悲伤_490_905_560
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有