基于Android實現(xiàn)答題倒計時功能
講一下我在做一個答題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è)點(diǎn)擊事件
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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android APP編寫簡單答題器
- Android實現(xiàn)簡單的答題系統(tǒng)
- Android答題APP的設(shè)計與實現(xiàn)
- Android通過手勢實現(xiàn)答題器翻頁效果
- android自定義倒計時控件示例
- android實現(xiàn)倒計時功能代碼
- Android實現(xiàn)計時與倒計時的常用方法小結(jié)
- Android實現(xiàn)加載廣告圖片和倒計時的開屏布局
- Android 實現(xiàn)閃屏頁和右上角的倒計時跳轉(zhuǎn)實例代碼
- Android中使用TextView實現(xiàn)高仿京東淘寶各種倒計時效果
- Android賬號注冊實現(xiàn)點(diǎn)擊獲取驗證碼倒計時效果
相關(guān)文章
Flutter中網(wǎng)絡(luò)圖片加載和緩存的實現(xiàn)
這篇文章主要介紹了Flutter中網(wǎng)絡(luò)圖片加載和緩存的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Android AlertDialog六種創(chuàng)建方式案例詳解
這篇文章主要介紹了Android AlertDialog六種創(chuàng)建方式案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
Android用過TextView實現(xiàn)跑馬燈效果的示例
本篇文章主要介紹了Android用過TextView實現(xiàn)跑馬燈效果的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
viewpager+photoview實現(xiàn)圖片查看器
這篇文章主要為大家詳細(xì)介紹了viewpager+photoview實現(xiàn)圖片查看器,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12

