首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >展示白天,时间..。从Android的约会开始就已经过去了(Chronometer)

展示白天,时间..。从Android的约会开始就已经过去了(Chronometer)
EN

Stack Overflow用户
提问于 2016-12-31 04:07:41
回答 2查看 1.2K关注 0票数 1

我已经使用CountDownTimer和这个答案中的很多代码对未来的日期(剩余的几天、小时、分钟、秒)进行了倒计时:现在,我想做完全相同的事情,但从过去的日期开始计数。我的TextView应该每秒钟刷新一次,并显示经过的时间。

我尝试了什么:

我试图操纵CountDownTimer,使其以相反的顺序工作。将间隔更改为-1000或每秒钟向倒计时添加2000毫秒。两个人都没用。

然后我想我应该用Chr测时的课程。据我所知,标准的计时钟只显示小时、分钟和秒。所以没有日子。然后,我按照我在前面找到的CountDownTimer答案的样式编写了以下代码,用所需的数据更新TextView:

代码语言:javascript
复制
    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。我觉得这不是天文钟的工作方式,但我不知道我做错了什么。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-12-31 04:12:55

编辑:

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

鉴于此,

对onTick(long)的调用与此对象同步,以便在完成前一个回调之前不会发生对onTick(long)的调用。

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

代码语言:javascript
复制
tv.setText(stopwatchDisplay);

代码语言:javascript
复制
tv.post(new Runnable() {
    public void run() {
        tv.setText(stopwatchDisplay);
    });
票数 1
EN

Stack Overflow用户

发布于 2016-12-31 04:16:07

请使用处理程序.。

代码语言:javascript
复制
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..Month从0th Index开始

FestCountdownTimer类

代码语言:javascript
复制
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;
    }
}

希望能帮上忙..。:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41405371

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档