基于Android實現(xiàn)答題倒計時功能

這篇文章主要介紹了基于Android實現(xiàn)答題倒計時功能,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計、成都做網(wǎng)站、孝南網(wǎng)絡(luò)推廣、微信平臺小程序開發(fā)、孝南網(wǎng)絡(luò)營銷、孝南企業(yè)策劃、孝南品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供孝南建站搭建服務(wù),24小時服務(wù)熱線:18980820575,官方網(wǎng)址:muchs.cn

講一下我在做一個答題APP時涉及到倒計時時遇到的一個問題吧。
碎片(Fragment)+CountDownTimer組成的一個答題,其中遇到的一個問題就是,這個題的倒計時在你手動滑動下一個題的時候卻用在了下一個題的時間
解決這個問題運(yùn)用的就是懶加載來控制倒計時的開始和取消

首先你要先定義一個抽象類繼承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;
  /**
   * 在這里實現(xiàn)Fragment數(shù)據(jù)的緩加載.
   * @param isVisibleToUser
   */
  @Override
  public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if(getUserVisibleHint()) {
      //可見時調(diào)用
      isVisible = true;
      onVisible();
    } else {
      isVisible = false;
      onInvisible();
    }
  }
  protected abstract void onVisible();
  //protected abstract void lazyLoad();
  protected abstract void onInvisible();
}

這是答題的Activity 在這里你要繼承剛剛自己寫的抽象類
這個類里面包含了數(shù)據(jù)的加載什么的,有需要的童鞋可以看看,就不刪了哈。

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);
    //綁定值 獲取頁面的監(jiān)聽的i 傳過來改變
    isPrepared = true;
    //懶加載
    getvalue(this.i);
    onVisible();//可見
    onInvisible();//不可見
    // lazyLoad();

    return v;
  }


  public void getvalue(int i){
    //查詢數(shù)據(jù)
    /**
     * @param context 上下文
     * @param name  名字(數(shù)據(jù)庫名),文件名
     * @param factory 游標(biāo)工廠,多數(shù)情況:null
     * @param version 數(shù)據(jù)庫版本
     */
    //DBHepler dbHepler=new DBHepler(this,"questions.db",null,1);
    List<QuestionEntity> 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";
    }*/


    //將查詢出來的數(shù)據(jù)放到控件里面
    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組設(shè)點擊事件
    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
   *      // 倒計時總數(shù),單位為毫秒。
   * @param
   *      // 每隔多久調(diào)用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就不往下執(zhí)行
      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
  }
*/

}

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“基于Android實現(xiàn)答題倒計時功能”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

本文題目:基于Android實現(xiàn)答題倒計時功能
當(dāng)前地址:http://muchs.cn/article10/jojddo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計公司微信公眾號、全網(wǎng)營銷推廣響應(yīng)式網(wǎng)站、做網(wǎng)站、服務(wù)器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)