显示自Android(计时器)日期以来已过去的天数,小时数

时间:2016-12-31 04:07:42

标签: java android

我使用CountDownTimer和来自此答案的大量代码对未来日期(剩余天数,小时数,分钟数,秒数)进行了倒计时:https://stackoverflow.com/a/32773716/3984944 现在我想完全相同,但从过去的日期算起来。我的TextView应该每秒刷新一次并显示已用时间
我尝试了什么:
我尝试操作CountDownTimer,以便它以相反的顺序工作。每秒将间隔更改为-1000或向Countdown添加2000毫秒。两者都不起作用。
然后我想我应该使用Chronometer类。就我而言,标准天文台只显示小时,分钟和秒。所以没有日子。然后我用我之前找到的CountDownTimer答案的样式编写了以下代码,用我们更新了带有所需数据的TextView:

    final Chronometer ch = (Chronometer) findViewById(R.id.ch_chronometer);
    final TextView tv = (TextView) findViewById(R.id.tv_show_stopwatch);

    ch.setBase(endMillis); //endMillis is the date in Milliseconds

    chCountdownSince.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
        public void onChronometerTick(Chronometer cArg) {
            long t = System.currentTimeMillis() - chCountdownSince.getBase();

            long days = TimeUnit.MILLISECONDS.toDays(t);
            t -= TimeUnit.DAYS.toMillis(days);

            long hours = TimeUnit.MILLISECONDS.toHours(t);
            t -= TimeUnit.HOURS.toMillis(hours);

            long minutes = TimeUnit.MILLISECONDS.toMinutes(t);
            t -= TimeUnit.MINUTES.toMillis(minutes);

            long seconds = TimeUnit.MILLISECONDS.toSeconds(t);
            String stopwatchDisplay = "Days: %d Hours: %d Minutes: %d Seconds: %d";
            stopwatchDisplay = String.format(stopwatchDisplay, days, hours, minutes, seconds);
            tv.setText(stopwatchDisplay);
        }
    });

我觉得这个剪切是绝对有意义的,但在执行时它根本不会改变我的TextView。我觉得这不是计时器的工作原理,但我不知道我做错了什么。

2 个答案:

答案 0 :(得分:1)

编辑:

我想你完全忘了启动天文台。

Given that

  

对onTick(long)的调用将同步到此对象   在前一次回调之前不会发生对onTick(long)的调用   完整。

不太可能在UI线程上完成滴答,但这正是您需要设置文本的地方,请尝试更改

tv.setText(stopwatchDisplay);

tv.post(new Runnable() {
    public void run() {
        tv.setText(stopwatchDisplay);
    });

答案 1 :(得分:0)

请使用处理程序..

public void countDownStart() {
    handler = new Handler();
    runnable = new Runnable(){
        @Override
        public void run(){
            handler.postDelayed(this,1000);
            try {
                FestCountdownTimer timer = new FestCountdownTimer(00, 00, 9, 3, 01, 2017);
                new CountDownTimer(timer.getIntervalMillis(), 1000) {
                    @Override
                    public void onTick(long millisUntilFinished){
                        int days = (int) ((millisUntilFinished / 1000) / 86400);
                        int hours = (int) (((millisUntilFinished / 1000)
                                - (days * 86400)) / 3600);
                        int minutes = (int) (((millisUntilFinished / 1000)
                                - (days * 86400) - (hours * 3600)) / 60);
                        int seconds = (int) ((millisUntilFinished / 1000) % 60);
                        String countdown = String.format("%02dd %02dh %02dm %02ds", days,
                                hours, minutes, seconds);
                        txtTimerDay.setText("" + String.format("%02d", days));
                        txtTimerHour.setText("" + String.format("%02d", hours));
                        txtTimerMinute.setText(""
                                + String.format("%02d", minutes));
                        txtTimerSecond.setText(""
                                + String.format("%02d", seconds));
                    }
                    @Override
                    public void onFinish() {
                        textViewGone();
                MainActivity.aSwitch.setChecked(false);
        creditText.setText("Toggle On To Start");
                    }
                }.start();
            } catch (Exception e) {
                e.printStackTrace();                }
        }
    };
    handler.postDelayed(runnable, 1 * 1000);

}

请记住,9 is Hours,3 is date,1 is Febraury Month ..月份从0th Index

开始

FestCountdownTimer类

public class FestCountdownTimer {
    private long intervalMillis;
    public FestCountdownTimer(int second, int minute, int hour, int monthDay, int month, int year) {
        Time futureTime = new Time();
        // Set date to future time
        futureTime.set(second, minute, hour, monthDay, month, year);
        futureTime.normalize(true);
        long futureMillis = futureTime.toMillis(true);
        Time timeNow = new Time();
        // Set date to current time
        timeNow.setToNow();
        timeNow.normalize(true);
        long nowMillis = timeNow.toMillis(true);
        // Subtract current milliseconds time from future milliseconds time to retrieve interval
        intervalMillis = futureMillis - nowMillis;
    }
    public long getIntervalMillis() {
        return intervalMillis;
    }
}

希望它有所帮助.. :)