栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

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

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

package com.zking.sun.dao;

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



public abstract class LazyFragment extends Fragment {
  protected boolean isVisible;
  
  @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;



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){
    //查询数据
    
    //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);
    


    //将查询出来的数据放到控件里面
    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;
 }


      }
    });
  }




  
    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();//将倒计时取消

  }


}

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/156630.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号