请帮忙,我有一个需要暂停和恢复的倒数计时器.
我设法通过在onPause中使用.cancel()来暂停计时器,但是我无法使其正确重启.
我有两种情况:
场景1-它暂停了,但是在重新启动时,即使取消了初始计时器,它似乎也重新启动了两个倒数计时器.
public voID playAgain (VIEw vIEw) { score = 0; numberOfQuestions = 0; timerTextVIEw.setText("60s"); pointsTextVIEw.setText("0/0"); resultTextVIEw.setText(""); playAgainbutton.setVisibility(VIEw.INVISIBLE); returnToMenu.setVisibility(VIEw.INVISIBLE); resultdisplayTextVIEw.setVisibility(VIEw.INVISIBLE); countDownText.setVisibility(VIEw.VISIBLE); button0.setVisibility(VIEw.INVISIBLE); button1.setVisibility(VIEw.INVISIBLE); button2.setVisibility(VIEw.INVISIBLE); button3.setVisibility(VIEw.INVISIBLE); sumTextVIEw.setVisibility(VIEw.INVISIBLE); countDownTimer = new CountDownTimer(millisInFuture, countDownInterval) { @OverrIDe public voID onTick(long timeRemain) { millisInFuture = timeRemain; countDownText.setText(String.valueOf(timeRemain / 1000 - 60)); if (timeRemain <= 61000) { timerTextVIEw.setText(String.valueOf(timeRemain / 1000) + "s"); button0.setEnabled(true); button1.setEnabled(true); button2.setEnabled(true); button3.setEnabled(true); button0.setVisibility(VIEw.VISIBLE); button1.setVisibility(VIEw.VISIBLE); button2.setVisibility(VIEw.VISIBLE); button3.setVisibility(VIEw.VISIBLE); sumTextVIEw.setVisibility(VIEw.VISIBLE); countDownText.setVisibility(VIEw.INVISIBLE); } } @OverrIDe public voID onFinish() { timerTextVIEw.setText("0s"); if (score > 0) { percentage = score * 100 / numberOfQuestions; } else { percentage = 0; } button0.setEnabled(false); button1.setEnabled(false); button2.setEnabled(false); button3.setEnabled(false); playAgainbutton.setVisibility(VIEw.VISIBLE); returnToMenu.setVisibility(VIEw.VISIBLE); resultdisplayTextVIEw.setVisibility(VIEw.VISIBLE); resultTextVIEw.setText(" "); } }.start(); generateQuestion(); } @OverrIDe public voID onPause() { countDownTimer.cancel(); super.onPause(); } @OverrIDe public voID onResume() { super.onResume();CountDownTimer countDownTimer1 = new CountDownTimer(millisInFuture, countDownInterval) { @OverrIDe public voID onTick(long timeRemain) { millisInFuture = timeRemain; countDownText.setText(String.valueOf(timeRemain / 1000 - 60)); if (timeRemain <= 61000) { timerTextVIEw.setText(String.valueOf(timeRemain / 1000) + "s"); button0.setEnabled(true); button1.setEnabled(true); button2.setEnabled(true); button3.setEnabled(true); button0.setVisibility(VIEw.VISIBLE); button1.setVisibility(VIEw.VISIBLE); button2.setVisibility(VIEw.VISIBLE); button3.setVisibility(VIEw.VISIBLE); sumTextVIEw.setVisibility(VIEw.VISIBLE); countDownText.setVisibility(VIEw.INVISIBLE); } } @OverrIDe public voID onFinish() { timerTextVIEw.setText("0s"); if (score > 0) { percentage = score * 100 / numberOfQuestions; } else { percentage = 0; } button0.setEnabled(false); button1.setEnabled(false); button2.setEnabled(false); button3.setEnabled(false); playAgainbutton.setVisibility(VIEw.VISIBLE); returnToMenu.setVisibility(VIEw.VISIBLE); resultdisplayTextVIEw.setVisibility(VIEw.VISIBLE); resultTextVIEw.setText(" ");} }.start();}
第二种情况只是使系统崩溃:
public voID playAgain (VIEw vIEw) { score = 0; numberOfQuestions = 0; timerTextVIEw.setText("60s"); pointsTextVIEw.setText("0/0"); resultTextVIEw.setText(""); playAgainbutton.setVisibility(VIEw.INVISIBLE); returnToMenu.setVisibility(VIEw.INVISIBLE); resultdisplayTextVIEw.setVisibility(VIEw.INVISIBLE); countDownText.setVisibility(VIEw.VISIBLE); button0.setVisibility(VIEw.INVISIBLE); button1.setVisibility(VIEw.INVISIBLE); button2.setVisibility(VIEw.INVISIBLE); button3.setVisibility(VIEw.INVISIBLE); sumTextVIEw.setVisibility(VIEw.INVISIBLE); countDownTimer = new CountDownTimer(millisInFuture, countDownInterval) { @OverrIDe public voID onTick(long timeRemain) { millisInFuture = timeRemain; countDownText.setText(String.valueOf(timeRemain / 1000 - 60)); if (timeRemain <= 61000) { timerTextVIEw.setText(String.valueOf(timeRemain / 1000) + "s"); button0.setEnabled(true); button1.setEnabled(true); button2.setEnabled(true); button3.setEnabled(true); button0.setVisibility(VIEw.VISIBLE); button1.setVisibility(VIEw.VISIBLE); button2.setVisibility(VIEw.VISIBLE); button3.setVisibility(VIEw.VISIBLE); sumTextVIEw.setVisibility(VIEw.VISIBLE); countDownText.setVisibility(VIEw.INVISIBLE); } } @OverrIDe public voID onFinish() { timerTextVIEw.setText("0s"); if (score > 0) { percentage = score * 100 / numberOfQuestions; } else { percentage = 0; } button0.setEnabled(false); button1.setEnabled(false); button2.setEnabled(false); button3.setEnabled(false); playAgainbutton.setVisibility(VIEw.VISIBLE); returnToMenu.setVisibility(VIEw.VISIBLE); resultdisplayTextVIEw.setVisibility(VIEw.VISIBLE); resultTextVIEw.setText(" "); } }.start(); generateQuestion(); } @OverrIDe public voID onPause() { countDownTimer.cancel(); super.onPause(); } @OverrIDe public voID onResume() { super.onResume();countDownTimer.start();}
我已经用谷歌搜索,似乎找不到有效的解决方案.
请帮助,除非此功能有效,否则galaxy store拒绝上传我的apk.
解决方法:
事实是,CountDownTimer是一个相对有限的类.它只有两个方法start()和cancel(),以及两个回调onTick()和onFinish().没有内置的暂停/恢复功能.
我使用TextVIEw设置了一个非常基本的Activity,以可视化CountDownTimer的行为,并且它可以按您想要的方式工作-离开活动时暂停,返回时恢复.我在这里发布了简单的代码:
import androID.os.CountDownTimer;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.Widget.TextVIEw;import java.util.Locale;public class MainActivity extends AppCompatActivity { private TextVIEw mTextVIEw; private CountDownTimer mCountDownTimer; private static final long INTERVAL = 1000L; private long timeRemaining = 60000L; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); mTextVIEw = (TextVIEw)findVIEwByID(R.ID.text_vIEw); } @OverrIDe protected voID onResume() { super.onResume(); mCountDownTimer = new CountDownTimer(timeRemaining, INTERVAL) { @OverrIDe public voID onTick(long l) { mTextVIEw.setText(String.format(Locale.getDefault(), "%d sec.", l / 1000L)); timeRemaining = l; } @OverrIDe public voID onFinish() { mTextVIEw.setText("Done."); } }; mCountDownTimer.start(); } @OverrIDe protected voID onPause() { super.onPause(); mCountDownTimer.cancel(); }}
我将从检查您是否可以运行此代码开始,然后将基本思想集成到您的项目中.本质上,您必须保持CountDownTimer对象之外的剩余时间.
总结以上是内存溢出为你收集整理的java-如何在Activity中正确设置CountDownTimer全部内容,希望文章能够帮你解决java-如何在Activity中正确设置CountDownTimer所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)