国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

基于Android實現答題倒計時功能

2019-10-22 18:16:51
字體:
來源:轉載
供稿:網友

講一下我在做一個答題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<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";    }*/    //將查詢出來的數據放到控件里面    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  }*/}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 历史| 田林县| 翁牛特旗| 靖远县| 尚义县| 交城县| 武胜县| 西乌| 津南区| 永嘉县| 九江市| 柘荣县| 灌云县| 阿拉善左旗| 永济市| 马边| 绵竹市| 周口市| 太和县| 洛浦县| 邵阳县| 固阳县| 黑河市| 屯昌县| 和田县| 洛南县| 靖远县| 新宾| 巴东县| 南召县| 英山县| 钟山县| 昌黎县| 青河县| 株洲县| 江达县| 景宁| 广元市| 西华县| 铁岭县| 萍乡市|