您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要介绍了基于Android实现答题倒计时功能,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
讲一下我在做一个答题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 } */ }
感谢你能够认真阅读完这篇文章,希望小编分享的“基于Android实现答题倒计时功能”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。