基于Android实现答题倒计时功能
讲一下我在做一个答题APP时涉及到倒计时时遇到的一个问题吧。
碎片(Fragment)+CountDownTimer组成的一个答题,其中遇到的一个问题就是,这个题的倒计时在你手动滑动下一个题的时候却用在了下一个题的时间
解决这个问题运用的就是懒加载来控制倒计时的开始和取消
首先你要先定义一个抽象类继承Fragment再让你的答题那个碎片的Activity继承
packagecom.zking.sun.dao; importandroid.support.v4.app.Fragment; importandroid.util.Log; /** *Createdbysunon2017/1/11. */ publicabstractclassLazyFragmentextendsFragment{ protectedbooleanisVisible; /** *在这里实现Fragment数据的缓加载. *@paramisVisibleToUser */ @Override publicvoidsetUserVisibleHint(booleanisVisibleToUser){ super.setUserVisibleHint(isVisibleToUser); if(getUserVisibleHint()){ //可见时调用 isVisible=true; onVisible(); }else{ isVisible=false; onInvisible(); } } protectedabstractvoidonVisible(); //protectedabstractvoidlazyLoad(); protectedabstractvoidonInvisible(); }
这是答题的Activity在这里你要继承刚刚自己写的抽象类
这个类里面包含了数据的加载什么的,有需要的童鞋可以看看,就不删了哈。
packagecom.zking.sun.android_06_project; importandroid.content.Intent; importandroid.os.Bundle; importandroid.os.CountDownTimer; importandroid.os.Handler; importandroid.support.annotation.Nullable; importandroid.support.v4.app.Fragment; importandroid.support.v4.view.ViewPager; importandroid.util.Log; importandroid.view.LayoutInflater; importandroid.view.View; importandroid.view.ViewGroup; importandroid.widget.Button; importandroid.widget.RadioButton; importandroid.widget.RadioGroup; importandroid.widget.TextView; importcom.zking.sun.dao.LazyFragment; importcom.zking.sun.dao.QusetionDao; importcom.zking.sun.entity.QuestionEntity; importjava.util.List; importstaticcom.zking.sun.android_06_project.R.id.tv_splash_01; /** *Createdbysunon2016/12/21. */ publicclassFragmentActivityextendsLazyFragment{ privateViewPagerviewpager_main_01; privateTextViewquestion_fragment_tv; privateRadioButtonanswer_fragment_01,answer_fragment_02,answer_fragment_03,answer_fragment_04; privateQusetionDaoqusetionDao=newQusetionDao(); privateinti; privateRadioGrouprg_fragment_qu; privateStringright_answer; privateTextViewcount_fragment_down; privateintSPLASH_DISPLAY_LENGHT=10000;//延迟多少秒 privateTextViewtv_splash_01; privateHandlerhandler=newHandler(); privateRunnablerunnbale; privateIntentintent; privateMyCountdownTimercountdowntimer; privateTextViewquestionR_fragment_tv; privatebooleanisPrepared; publicFragmentActivity(){ } publicFragmentActivity(inti){ this.i=i; } publicintgetI(){ returni; } publicvoidsetI(inti){ this.i=i; } @Nullable @Override publicViewonCreateView(LayoutInflaterinflater,@NullableViewGroupcontainer,@NullableBundlesavedInstanceState){ Viewv=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=newMyCountdownTimer(10000,1000); //绑定值获取页面的监听的i传过来改变 isPrepared=true; //懒加载 getvalue(this.i); onVisible();//可见 onInvisible();//不可见 //lazyLoad(); returnv; } publicvoidgetvalue(inti){ //查询数据 /** *@paramcontext上下文 *@paramname名字(数据库名),文件名 *@paramfactory游标工厂,多数情况:null *@paramversion数据库版本 */ //DBHeplerdbHepler=newDBHepler(this,"questions.db",null,1); ListquestionEntityList=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()); Stringthis04=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(newRadioGroup.OnCheckedChangeListener(){ @Override publicvoidonCheckedChanged(RadioGroupgroup,intcheckedId){ rg_fragment_qu.setEnabled(false); intselectRadio=group.getCheckedRadioButtonId(); switch(selectRadio){ caseR.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; caseR.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; caseR.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; caseR.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一次 *@authorDaiZhenWei * */ protectedclassMyCountdownTimerextendsCountDownTimer{ publicMyCountdownTimer(longmillisInFuture,longcountDownInterval){ super(millisInFuture,countDownInterval); } @Override publicvoidonTick(longmillisUntilFinished){ count_fragment_down.setText("倒计时:"+millisUntilFinished/1000); } @Override publicvoidonFinish(){ //count_fragment_down.setText("Turning"); FightActivity.getNext(null); } } //fragment的懒加载重写 @Override protectedvoidonVisible(){ //可见的 if(!isPrepared||!isVisible){ //判断isPrepared和isVisible只要有一个不为true就不往下执行 Log.i("isPrepared",isPrepared+"____________"+isVisible); return; } /** *倒计时 */ countdowntimer.start();//开始倒计时 Log.i("isPrepared",this.i+"_______4"); } @Override protectedvoidonInvisible(){ //不可见的 if(!isPrepared||isVisible){ return; } Log.i("isPrepared","____________我取消了"+this.i); countdowntimer.cancel();//将倒计时取消 } /* //主页面 publicvoidloadUI(Classc){ //启动之后跳著页面 //Intentintent=newIntent(SplashActivity.this,MainActivity.class); Intentintent=newIntent(FragmentActivity.this.getContext(),c); //SplashActivity.this.startActivity(intent); //SplashActivity.this.finish();//Toast.LENGTH_LONG } */ }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。