基于Android實(shí)現(xiàn)答題倒計(jì)時(shí)功能
講一下我在做一個(gè)答題APP時(shí)涉及到倒計(jì)時(shí)時(shí)遇到的一個(gè)問(wèn)題吧。
碎片(Fragment)+CountDownTimer組成的一個(gè)答題,其中遇到的一個(gè)問(wèn)題就是,這個(gè)題的倒計(jì)時(shí)在你手動(dòng)滑動(dòng)下一個(gè)題的時(shí)候卻用在了下一個(gè)題的時(shí)間
解決這個(gè)問(wèn)題運(yùn)用的就是懶加載來(lái)控制倒計(jì)時(shí)的開(kāi)始和取消
首先你要先定義一個(gè)抽象類繼承Fragment 再讓你的答題那個(gè)碎片的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;
/**
* 在這里實(shí)現(xiàn)Fragment數(shù)據(jù)的緩加載.
* @param isVisibleToUser
*/
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(getUserVisibleHint()) {
//可見(jiàn)時(shí)調(diào)用
isVisible = true;
onVisible();
} else {
isVisible = false;
onInvisible();
}
}
protected abstract void onVisible();
//protected abstract void lazyLoad();
protected abstract void onInvisible();
}
這是答題的Activity 在這里你要繼承剛剛自己寫(xiě)的抽象類
這個(gè)類里面包含了數(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);
//找到問(wèn)題和答案的控件
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);
//倒計(jì)時(shí)
countdowntimer = new MyCountdownTimer(10000, 1000);
//綁定值 獲取頁(yè)面的監(jiān)聽(tīng)的i 傳過(guò)來(lái)改變
isPrepared = true;
//懶加載
getvalue(this.i);
onVisible();//可見(jiàn)
onInvisible();//不可見(jiàn)
// lazyLoad();
return v;
}
public void getvalue(int i){
//查詢數(shù)據(jù)
/**
* @param context 上下文
* @param name 名字(數(shù)據(jù)庫(kù)名),文件名
* @param factory 游標(biāo)工廠,多數(shù)情況:null
* @param version 數(shù)據(jù)庫(kù)版本
*/
//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";
}*/
//將查詢出來(lái)的數(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
* // 倒計(jì)時(shí)總數(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("倒計(jì)時(shí): " + millisUntilFinished / 1000);
}
@Override
public void onFinish() {
//count_fragment_down.setText("Turning");
FightActivity.getNext(null);
}
}
//fragment的懶加載 重寫(xiě)
@Override
protected void onVisible() {
//可見(jiàn)的
if(!isPrepared || !isVisible) {
//判斷isPrepared和isVisible只要有一個(gè)不為true就不往下執(zhí)行
Log.i("isPrepared",isPrepared+"____________"+isVisible);
return;
}
/**
* 倒計(jì)時(shí)
*/
countdowntimer.start();//開(kāi)始倒計(jì)時(shí)
Log.i("isPrepared",this.i+"_______4");
}
@Override
protected void onInvisible() {
//不可見(jiàn)的
if(!isPrepared || isVisible) {
return;
}
Log.i("isPrepared","____________我取消了"+this.i);
countdowntimer.cancel();//將倒計(jì)時(shí)取消
}
/*
//主頁(yè)面
public void loadUI(Class c){
//啟動(dòng)之后跳著頁(yè)面
// 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
}
*/
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android APP編寫(xiě)簡(jiǎn)單答題器
- Android實(shí)現(xiàn)簡(jiǎn)單的答題系統(tǒng)
- Android答題APP的設(shè)計(jì)與實(shí)現(xiàn)
- Android通過(guò)手勢(shì)實(shí)現(xiàn)答題器翻頁(yè)效果
- android自定義倒計(jì)時(shí)控件示例
- android實(shí)現(xiàn)倒計(jì)時(shí)功能代碼
- Android實(shí)現(xiàn)計(jì)時(shí)與倒計(jì)時(shí)的常用方法小結(jié)
- Android實(shí)現(xiàn)加載廣告圖片和倒計(jì)時(shí)的開(kāi)屏布局
- Android 實(shí)現(xiàn)閃屏頁(yè)和右上角的倒計(jì)時(shí)跳轉(zhuǎn)實(shí)例代碼
- Android中使用TextView實(shí)現(xiàn)高仿京東淘寶各種倒計(jì)時(shí)效果
- Android賬號(hào)注冊(cè)實(shí)現(xiàn)點(diǎn)擊獲取驗(yàn)證碼倒計(jì)時(shí)效果
相關(guān)文章
Android編程實(shí)現(xiàn)簡(jiǎn)單流量管理功能實(shí)例
這篇文章主要介紹了Android編程實(shí)現(xiàn)簡(jiǎn)單流量管理功能的方法,結(jié)合實(shí)例形式分析了Android實(shí)現(xiàn)流量監(jiān)控所涉及的功能模塊與布局技巧,需要的朋友可以參考下2016-02-02
Flutter中網(wǎng)絡(luò)圖片加載和緩存的實(shí)現(xiàn)
這篇文章主要介紹了Flutter中網(wǎng)絡(luò)圖片加載和緩存的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Android AlertDialog六種創(chuàng)建方式案例詳解
這篇文章主要介紹了Android AlertDialog六種創(chuàng)建方式案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
Android用過(guò)TextView實(shí)現(xiàn)跑馬燈效果的示例
本篇文章主要介紹了Android用過(guò)TextView實(shí)現(xiàn)跑馬燈效果的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
Android 中Crash時(shí)如何獲取異常信息詳解及實(shí)例
這篇文章主要介紹了Android 中Crash時(shí)如何獲取異常信息詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-02-02
viewpager+photoview實(shí)現(xiàn)圖片查看器
這篇文章主要為大家詳細(xì)介紹了viewpager+photoview實(shí)現(xiàn)圖片查看器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android短信發(fā)送器實(shí)現(xiàn)方法
這篇文章主要介紹了Android短信發(fā)送器實(shí)現(xiàn)方法,以實(shí)例形式較為詳細(xì)的分析了Android短信發(fā)送器從界面布局到功能實(shí)現(xiàn)的完整步驟與相關(guān)技巧,需要的朋友可以參考下2015-09-09

