安卓倒数计时器到日期 [英] Android Countdown Timer to Date

查看:174
本文介绍了安卓倒数计时器到日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个倒计时的游戏/日期的机器人。我想创建一个显示天,小时,分钟和秒,我用最后一个变量指定日期的计时器。然后定时器设置文本视图以日,时,分,秒显示给用户。

I am trying to make a countdown timer for a game/date in android. I want to create a timer that displays the days, hours, minutes, and seconds to a date I specify with a final variable. The timer then sets text views to show the days, hours, minutes, and seconds to the user.

有关如何我可以$ C C此$有什么建议?

Any suggestions about how I could code this?

推荐答案

CountDownTimer,将显示格式化,以小时,分钟,天,秒的时间。

CountDownTimer that will display the time formatted to hours,minute,days,and seconds.

 public class DemotimerActivity extends Activity {
        /** Called when the activity is first created. */
        TextView tv;
        long diff;
        long milliseconds;
        long endTime;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            tv = new TextView(this);
            this.setContentView(tv);
            SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm");
            formatter.setLenient(false);


            String oldTime = "21.10.2013, 12:00";
            Date oldDate;
            try {
                oldDate = formatter.parse(oldTime);
                 milliseconds = oldDate.getTime();

                //long startTime = System.currentTimeMillis();
                // do your work...
                long endTime=System.currentTimeMillis();

                 diff = endTime-milliseconds;       

                Log.e("day", "miliday"+diff);
                long seconds = (long) (diff / 1000) % 60 ;
                Log.e("secnd", "miliday"+seconds);
                long minutes = (long) ((diff / (1000*60)) % 60);
                Log.e("minute", "miliday"+minutes);
                long hours   = (long) ((diff / (1000*60*60)) % 24);
                Log.e("hour", "miliday"+hours);
                long days = (int)((diff / (1000*60*60*24)) % 365);
                Log.e("days", "miliday"+days);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            Long serverUptimeSeconds = (System.currentTimeMillis() - milliseconds) / 1000;


                String serverUptimeText = String.format("%d days %d hours %d minutes %d seconds",
                serverUptimeSeconds / 86400,
                ( serverUptimeSeconds % 86400) / 3600 ,
                ((serverUptimeSeconds % 86400) % 3600 ) / 60,
                ((serverUptimeSeconds % 86400) % 3600 ) % 60
                );


            Log.v("jjj", "miliday"+serverUptimeText);
            MyCount counter = new MyCount(milliseconds,1000);
            counter.start();


        }


        // countdowntimer is an abstract class, so extend it and fill in methods
        public class MyCount extends CountDownTimer {
            public MyCount(long millisInFuture, long countDownInterval) {
                super(millisInFuture, countDownInterval);
            }

            @Override
            public void onFinish() {
                tv.setText("done!");
            }

            @Override
            public void onTick(long millisUntilFinished) {
                //tv.setText("Left: " + millisUntilFinished / 1000);

                long diff = endTime - millisUntilFinished; 
                Log.e("left", "miliday"+diff);
                long seconds = (long) (diff / 1000) % 60 ;
                //Log.e("secnd", "miliday"+seconds);
                long minutes = (long) ((diff / (1000*60)) % 60);
                //Log.e("minute", "miliday"+minutes);
                long hours   = (long) ((diff / (1000*60*60)) % 24);
                //Log.e("hour", "miliday"+hours);
                int days = (int)((diff / (1000*60*60*24)) % 365);
                Log.v("days", "miliday"+days);


                Long serverUptimeSeconds = 
                        (System.currentTimeMillis() - millisUntilFinished) / 1000;


                    String serverUptimeText = 
                    String.format("%d days %d hours %d minutes %d seconds",
                    serverUptimeSeconds / 86400,
                    ( serverUptimeSeconds % 86400) / 3600 ,
                    ((serverUptimeSeconds % 86400) % 3600 ) / 60,
                    ((serverUptimeSeconds % 86400) % 3600 ) % 60
                    );  

                    Log.v("new its", "miliday"+serverUptimeText);

                 // tv.setText(days +":"+hours+":"+minutes + ":" + seconds);

                    tv.setText(serverUptimeText);
            }
        }
    }

这篇关于安卓倒数计时器到日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆