安卓倒数计时器到日期

我正在尝试为Android中的游戏/日期制作倒数计时器。我想创建一个计时器,显示我使用最终变量指定的日期的天,小时,分钟和秒。然后,计时器设置文本视图以向用户显示天、小时、分钟和秒。

关于如何编写此代码的任何建议?


答案 1

倒数计时器,将显示格式化为小时,分钟,天和秒的时间。

 public class DemotimerActivity extends Activity {
        /** Called when the activity is first created. */
         TextView tv;
         long diff;
         long oldLong;
         long NewLong;
        @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");
            String oldTime = "19.02.2018, 12:00";//Timer date 1
            String NewTime = "20.02.2018, 14:00";//Timer date 2
            Date oldDate, newDate;
            try {
                oldDate = formatter.parse(oldTime);
                newDate = formatter.parse(NewTime);
                oldLong = oldDate.getTime();
                NewLong = newDate.getTime();
                diff = NewLong - oldLong;
           } catch (ParseException e) {
                e.printStackTrace();
       }
         MyCount counter = new MyCount(diff, 1000);
         counter.start();
    }


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

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

    @Override
    public void onTick(long millisUntilFinished) {
         long millis = millisUntilFinished;
        String hms = (TimeUnit.MILLISECONDS.toDays(millis)) + "Day "
                + (TimeUnit.MILLISECONDS.toHours(millis) - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(millis)) + ":")
                + (TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)) + ":"
                + (TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))));
        txtNumber1.setText(/*context.getString(R.string.ends_in) + " " +*/ hms);
    }
}

    }

答案 2

试试这个:

SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss");
        formatter.setLenient(false);


        String endTime = "25.06.2017, 15:05:36"

        Date endDate;
        try {
            endDate = formatter.parse(endTime);
            milliseconds = endDate.getTime();

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

         startTime = System.currentTimeMillis();

         diff = milliseconds - startTime;


           mCountDownTimer = new CountDownTimer(milliseconds, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {

                startTime=startTime-1;
                Long serverUptimeSeconds =
                        (millisUntilFinished - startTime) / 1000;

                String daysLeft = String.format("%d", serverUptimeSeconds / 86400);
                txtViewDays.setText(daysLeft);

                String hoursLeft = String.format("%d", (serverUptimeSeconds % 86400) / 3600);
                txtViewHours.setText(hoursLeft);

                String minutesLeft = String.format("%d", ((serverUptimeSeconds % 86400) % 3600) / 60);

                txtViewMinutes.setText(minutesLeft);

                String secondsLeft = String.format("%d", ((serverUptimeSeconds % 86400) % 3600) % 60);
                txtViewSecond.setText(secondsLeft);


            }

            @Override
            public void onFinish() {

            }
        }.start();

    }

推荐