倒计时器在android中无法正常工作

时间:2016-09-06 08:37:56

标签: android timer count

我实施了倒计时器(2分钟)1:59 - > 00:00 但过了一段时间它不能正常工作并变得疯狂! 像这样:01:46 - 01:45 - 01:46 - 01:43 - 01:42 ...... 出了什么问题?编辑:计时器在片段中 提前谢谢。

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ApplicationLauncher.mainComponent().inject(this);
    setActionBarHidden(true);
    setDrawerLocked(true);
    countDownTimer = new CountDownTimer(120000, 1000) {

        public void onTick(long millisUntilFinished) {
            mTimerTv.setText(formatTime(millisUntilFinished));
        }
        public void onFinish() {
            mTimerTv.setVisibility(View.INVISIBLE);
            mSendAgainBtn.setClickable(true);
            mSendAgainBtn.setBackground(getActivity().getResources().getDrawable(R.drawable.brown_btn_selector));
            mSendAgainBtn.setOnClickListener(onSendAgainButtonClick);
        }

    }.start();
}

    private String formatTime(long millis) {
    output = "";
    seconds = millis / 1000;
    long minutes = seconds / 60;

    seconds = seconds % 60;
    minutes = minutes % 60;

    String secondsD = String.valueOf(seconds);
    String minutesD = String.valueOf(minutes);
    if (seconds < 10)
        secondsD = "0" + seconds;
    if (minutes < 10)
        minutesD = "0" + minutes;
    output =  minutesD+ " : " + secondsD;
    return output;
}

1 个答案:

答案 0 :(得分:0)

您可能正在使用片段或viewPager编写此代码。 countDown Timer被多次调用。尝试将timer.start()放在任何按钮的onClickListner中并检查。它非常有效。