欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2018年01月18日 08:56:48   作者:H_Yi_  
這篇文章主要介紹了基于Android實現(xiàn)答題倒計時功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

講一下我在做一個答題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;
  /**
   * 在這里實現(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 游標工廠,多數(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組設點擊事件
    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
  }
*/

}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Android編程實現(xiàn)簡單流量管理功能實例

    Android編程實現(xiàn)簡單流量管理功能實例

    這篇文章主要介紹了Android編程實現(xiàn)簡單流量管理功能的方法,結合實例形式分析了Android實現(xiàn)流量監(jiān)控所涉及的功能模塊與布局技巧,需要的朋友可以參考下
    2016-02-02
  • Flutter中網(wǎng)絡圖片加載和緩存的實現(xiàn)

    Flutter中網(wǎng)絡圖片加載和緩存的實現(xiàn)

    這篇文章主要介紹了Flutter中網(wǎng)絡圖片加載和緩存的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-03-03
  • 基于Android掃描sd卡與系統(tǒng)文件的介紹

    基于Android掃描sd卡與系統(tǒng)文件的介紹

    本篇文章是對Android掃描sd卡與系統(tǒng)文件進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • Android AlertDialog六種創(chuàng)建方式案例詳解

    Android AlertDialog六種創(chuàng)建方式案例詳解

    這篇文章主要介紹了Android AlertDialog六種創(chuàng)建方式案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Android用過TextView實現(xiàn)跑馬燈效果的示例

    Android用過TextView實現(xiàn)跑馬燈效果的示例

    本篇文章主要介紹了Android用過TextView實現(xiàn)跑馬燈效果的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • Android 中Crash時如何獲取異常信息詳解及實例

    Android 中Crash時如何獲取異常信息詳解及實例

    這篇文章主要介紹了Android 中Crash時如何獲取異常信息詳解及實例的相關資料,需要的朋友可以參考下
    2017-02-02
  • viewpager+photoview實現(xiàn)圖片查看器

    viewpager+photoview實現(xiàn)圖片查看器

    這篇文章主要為大家詳細介紹了viewpager+photoview實現(xiàn)圖片查看器,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android短信發(fā)送器實現(xiàn)方法

    Android短信發(fā)送器實現(xiàn)方法

    這篇文章主要介紹了Android短信發(fā)送器實現(xiàn)方法,以實例形式較為詳細的分析了Android短信發(fā)送器從界面布局到功能實現(xiàn)的完整步驟與相關技巧,需要的朋友可以參考下
    2015-09-09
  • Android 通知欄的使用方法

    Android 通知欄的使用方法

    不同版本通知欄的創(chuàng)建方式不盡相同,當前官方推薦使用 NotificationCompat 相關的API,兼容到Android 4.0,但是部分新功能,比如內(nèi)嵌回復操作,舊版本是無法支持的。
    2021-05-05
  • Android實現(xiàn)View的拖拽

    Android實現(xiàn)View的拖拽

    這篇文章主要為大家詳細介紹了Android實現(xiàn)View的拖拽,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-05-05

最新評論