显示倒数计时器

时间:2019-12-29 11:23:15

标签: android chronometer

我想以yy-MM-dd h:m:s格式在我的Android应用中显示递增计时器

我尝试过的事情: 我尝试操纵CountDownTimer,以便它以相反的顺序工作。将时间间隔更改为-1000或每秒钟将2000毫秒添加到倒计时。两者都不起作用。 然后我想应该使用Chronometer类。就我而言,标准的计时器只显示小时,分钟和秒。因此,没有几天,没有几个月,也没有几年。

  • 如果时间少于1分钟,则计时器仅显示秒数

    00秒

  • 如果少于1小时,则只有几分钟和几秒钟,那么格式应该是

    00分钟00秒

  • 如果少于1天,则只有几小时,几分钟和几秒钟

    00小时00分00秒

  • 如果少于1个月,则仅显示天,小时,分钟,秒

    0天00小时00分00秒

  • 如果少于一年,则仅显示月份,天,小时,分钟,秒

    0个月0天00小时00分钟00秒

  • 如果超过一年,则显示为

    0年0个月0天00小时00分钟00秒

我编写的代码

  chronometer.setFormat("%s");
    chronometer.setBase(SystemClock.elapsedRealtime());
    chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
        @Override
        public void onChronometerTick(Chronometer chronometer) {

            String cTextSize = chronometer.getText().toString();

           if ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 60000) {
               chronometer.setText(cTextSize.substring(3,5)+" sec");

            }
            else if (((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 60000)  &&  ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 3600000)){
                chronometer.setText(cTextSize.substring(0,2)+" mins "+cTextSize.substring(3,5)+" sec");
            }
            else if (((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 3600000)  &&  ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 86400000)){
                chronometer.setText(cTextSize.substring(0,2)+" hours "+cTextSize.substring(3,5)+" mins "+cTextSize.substring(6,8)+" sec");
            }
}

这里的代码可以正常运行,直到显示小时数,但现在我想如果小时数大于24,则显示1天而不是25小时

我们将非常感谢您的帮助,如果您不知道答案,请不要对它投反对票

1 个答案:

答案 0 :(得分:0)

非常感谢所有Google认证的开发人员为我提供的帮助。无论如何,这就是我解决问题的方式

chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
        @Override
        public void onChronometerTick(Chronometer chronometer) {

            String cTextSize = chronometer.getText().toString();


            if ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 60000) {
                chronometer.setText(cTextSize.substring(3, 5) + " sec");

            } 
            else if (((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 60000) && ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 3600000)) {
                chronometer.setText(cTextSize.substring(0, 2) + " mins " + cTextSize.substring(3, 5) + " sec");
            } 
            else if (((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 3600000) && ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 86400000)) {
                if (chronometer.getText().length() == 8){
                    chronometer.setText(cTextSize.substring(0, 2) + " hours " + cTextSize.substring(3, 5) + " mins " + cTextSize.substring(6, 8) + " sec");
            }
        }
            else if(((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 86400000) && ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 2592000000L))
            {
                Toast.makeText(getActivity(), "day", Toast.LENGTH_SHORT).show();

                long dy = TimeUnit.MILLISECONDS.toDays(SystemClock.elapsedRealtime() - chronometer.getBase());
                final long hr = TimeUnit.MILLISECONDS.toHours(SystemClock.elapsedRealtime() - chronometer.getBase())
                        - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(SystemClock.elapsedRealtime() - chronometer.getBase()));
                final long min = TimeUnit.MILLISECONDS.toMinutes(SystemClock.elapsedRealtime() - chronometer.getBase())
                        - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(SystemClock.elapsedRealtime() - chronometer.getBase()));
                final long sec = TimeUnit.MILLISECONDS.toSeconds(SystemClock.elapsedRealtime() - chronometer.getBase())
                        - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(SystemClock.elapsedRealtime() - chronometer.getBase()));
                if(dy==1) {
                    chronometer.setText(dy + " day " + hr + " hours " + min + " mins " + sec + " sec ");
                }else if(dy>1) {
                    chronometer.setText(dy + " days " + hr + " hours " + min + " mins " + sec + " sec ");
                }
                }
            else if(((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 2592000000L) && ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 31556952000L))
            {
                Toast.makeText(getActivity(), "month", Toast.LENGTH_SHORT).show();

                long dy = TimeUnit.MILLISECONDS.toDays(SystemClock.elapsedRealtime() - chronometer.getBase());
                final long mn = dy / 30;
                dy %= 30;
                final long hr = TimeUnit.MILLISECONDS.toHours(SystemClock.elapsedRealtime() - chronometer.getBase())
                        - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(SystemClock.elapsedRealtime() - chronometer.getBase()));
                final long min = TimeUnit.MILLISECONDS.toMinutes(SystemClock.elapsedRealtime() - chronometer.getBase())
                        - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(SystemClock.elapsedRealtime() - chronometer.getBase()));
                final long sec = TimeUnit.MILLISECONDS.toSeconds(SystemClock.elapsedRealtime() - chronometer.getBase())
                        - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(SystemClock.elapsedRealtime() - chronometer.getBase()));
                    chronometer.setText(mn+" months "+dy + " days " + hr + " hours " + min + " mins " + sec + " sec ");
            }

            else if(((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 31556952000L))
            {
                Toast.makeText(getActivity(), "year", Toast.LENGTH_SHORT).show();
                long dy = TimeUnit.MILLISECONDS.toDays(SystemClock.elapsedRealtime() - chronometer.getBase());
                final long yr = dy / 365;
                dy %= 365;

                final long mn = dy / 30;
                dy %= 30;
                final long hr = TimeUnit.MILLISECONDS.toHours(SystemClock.elapsedRealtime() - chronometer.getBase())
                        - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(SystemClock.elapsedRealtime() - chronometer.getBase()));
                final long min = TimeUnit.MILLISECONDS.toMinutes(SystemClock.elapsedRealtime() - chronometer.getBase())
                        - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(SystemClock.elapsedRealtime() - chronometer.getBase()));
                final long sec = TimeUnit.MILLISECONDS.toSeconds(SystemClock.elapsedRealtime() - chronometer.getBase())
                        - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(SystemClock.elapsedRealtime() - chronometer.getBase()));
                    chronometer.setText(yr+" years "+mn+" months "+dy + " days " + hr + " hours " + min + " mins " + sec + " sec ");
            }

        }
    });