24 小时倒计时计时器,带有三个单独的文本视图,分别显示小时、分钟、秒

Posted

技术标签:

【中文标题】24 小时倒计时计时器,带有三个单独的文本视图,分别显示小时、分钟、秒【英文标题】:24 Hour CountDown Timer with three separate textviews for hours, minutes, second 【发布时间】:2014-07-25 01:07:50 【问题描述】:

我正在尝试使用 三个单独的文本视图实现一个 24 小时倒计时计时器,一个保持小时,一个保持分钟,一个保持秒。

按下开始时间按钮时,我希望计时器倒计时。

我对此进行了大量研究并尝试实施许多示例,从文档中学习并根据自己的喜好对其进行修改,但我无法产生可靠的解决方案。

Countdown timer in HH:MM:SS format in android

Display timer in textview contain days, hours, minutes and seconds in android

问题在于,对于 android 中的倒数计时器,它通常只使用一个 textview 来实现,将其视为带有时间的长字符串,然后从那里开始倒计时。我不确定我将如何处理 24 小时倒数计时器,因为我有三个单独的文本视图。

我很惊讶我找不到太多关于这个问题的信息。

I.E.

例如,我有一个倒数计时器,但这仅使用一个文本视图。我将如何制作一些考虑到小时、分钟和秒的三个单独文本视图并相应地倒计时的东西。

    Button buttonStartTime;                 // clicking this button will start time count down 
    TextView textViewShowTime;              // will show the time 
    CountDownTimer countDownTimer;          // built in android class CountDownTimer
    long totalTimeCountInMilliseconds;      // total count down time in milliseconds 
    long timeBlinkInMilliseconds;           // start time of start blinking 
    boolean blink;                          // controls the blinking .. on and off 

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        getReferenceOfViews ();                         // get all views 
        setActionListeners ();                          // set action listerns 

        totalTimeCountInMilliseconds = 60 * 1000;      // time count for 3 minutes = 180 seconds
        timeBlinkInMilliseconds = 30 * 1000;            // blink starts at 1 minutes = 60 seconds
    

    private void setActionListeners() 

        buttonStartTime.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 

                textViewShowTime.setTextAppearance(getApplicationContext(), R.style.normalText);

                countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 500) 
                    // 500 means, onTick function will be called at every 500 milliseconds 

                    @Override
                    public void onTick(long leftTimeInMilliseconds) 
                        long seconds = leftTimeInMilliseconds / 1000;

                        if ( leftTimeInMilliseconds < timeBlinkInMilliseconds ) 
                            textViewShowTime.setTextAppearance(getApplicationContext(), R.style.blinkText);
                            // change the style of the textview .. giving a red alert style 

                            if ( blink ) 
                                textViewShowTime.setVisibility(View.VISIBLE);
                                // if blink is true, textview will be visible
                             else 
                                textViewShowTime.setVisibility(View.INVISIBLE);
                            

                            blink = !blink;         // toggle the value of blink
                        

                        textViewShowTime.setText(String.format("%02d", seconds / 60) + ":" + String.format("%02d", seconds % 60));
                        // format the textview to show the easily readable format
                    

                    @Override
                    public void onFinish() 
                        // this function will be called when the timecount is finished
                        textViewShowTime.setText("Time up!");
                        textViewShowTime.setVisibility(View.VISIBLE);
                    

                .start();

            
        );
    

    private void getReferenceOfViews() 

        buttonStartTime = (Button) findViewById(R.id.btnStartTime);
        textViewShowTime = (TextView) findViewById(R.id.tvTimeCount);
    

编辑:现在我有这个:

private void setActionListeners() 

        start_timer.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 

               // textViewShowTime.setTextAppearance(getApplicationContext(), R.style.normalText);

                countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 500) 
                    // 500 means, onTick function will be called at every 500 milliseconds

                    @Override
                    public void onTick(long leftTimeInMilliseconds) 
                        int seconds = leftTimeInMilliseconds / 1000 % 60;
                        int minutes = leftTimeInMilliseconds / 60000 % 60;
                        int hours = leftTimeInMilliseconds / 3600000;




                        if ( leftTimeInMilliseconds < timeBlinkInMilliseconds ) 
                           // textViewShowTime.setTextAppearance(getApplicationContext(), R.style.blinkText);
                            // change the style of the textview .. giving a red alert style

                            if ( blink ) 
                                number_text.setVisibility(View.VISIBLE);
                                minute_text.setVisibility(View.VISIBLE);
                                second_text.setVisibility(View.VISIBLE);


                                // if blink is true, textview will be visible
                             else 
                                number_text.setVisibility(View.INVISIBLE);
                                minute_text.setVisibility(View.INVISIBLE);
                                second_text.setVisibility(View.INVISIBLE);


                            

                            blink = !blink;         // toggle the value of blink
                        

                        number_text = String.format("%02d", seconds % 60);
                        minute_text = String.format("%02d", seconds / 60);
                        second_text = String.format("%02d", seconds / 3600);                        // format the textview to show the easily readable format
                    

                    @Override
                    public void onFinish() 
                        // this function will be called when the timecount is finished
                        //textViewShowTime.setText("Time up!");
                        //textViewShowTime.setVisibility(View.VISIBLE);
                    

                .start();

            
        );
    

    private void getReferenceOfViews() 

        start_timer = (Button) findViewById(R.id.start_button);
        number_text = (TextView) findViewById(R.id.hour_progress_number);
        minute_text = (TextView) findViewById(R.id.minute_progress_number);
        second_text = (TextView) findViewById(R.id.second_progress_number);


    

我收到这些错误。

【问题讨论】:

【参考方案1】:

在你的 onTick() 里面,而不是:

textViewShowTime.setText(String.format("%02d", seconds / 60) + ":" + String.format("%02d", seconds % 60));

做:

textViewSeconds.setText(String.format("%02d", seconds % 60));
textViewMinutes.setText(String.format("%02d", seconds / 60));
textViewHours.setText(String.format("%02d", seconds / 3600));

【讨论】:

好的,我会测试一下 它说 textview 是必需的,但找到了字符串 这是什么类型的错误?你能粘贴你的logcat输出吗?您是否声明了 3 个文本视图? 我进行了编辑。包括我的新代码和错误。我想知道我是否会使用您下面答案中的代码...? 当我一直在运行它,但我不确定为什么会收到 java.lang.RuntimeException: Unable to start activity ComponentInfocom.spicycurryman.getdisciplined10.app.dev/com.spicycurryman. getdisciplined10.app.MainActivity:java.lang.NullPointerException【参考方案2】:

我知道现在回答这个问题为时已晚,但我现在已经完成了,而且它可能会帮助别人,因为它不像我那样花费数小时。

仅编码为 24 小时倒计时。

   long startTime = System.currentTimeMillis();

   //adding 24 hours milliseconds with current time of milliseconds to make it 24 hours milliseconds.
   milliseconds = System.currentTimeMillis() + 86400000; // 24 hours = 86400000 milliseconds 

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

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

    // now you repalce txtViewHours,  txtViewMinutes, txtViewSeconds by your textView.      

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

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

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


    

    @Override
    public void onFinish() 
        Log.i(TAG, "Timer finished");
    
;

cdt.start();

【讨论】:

【参考方案3】:

要分别显示小时、分钟和秒,您必须使用模运算符和乘法来解析出所需的信息。

由于 onTick 以毫秒为单位为您提供剩余的倒计时时间,而一秒是 1000 毫秒,我们可以将剩余的总毫秒数除以 1000。这就是剩余的总秒数。但是,由于我们只想显示当前分钟的剩余秒数,我们可以将剩余的秒数取模 60。总而言之,这看起来像:

int secs = leftTimeInMilliseconds / 1000 % 60;

分钟和小时的过程非常相似。一分钟有 60,000 毫秒,一小时有 60 分钟,所以

int mins = leftTimeInMilliseconds / 60000 % 60;

那么几个小时,一小时有 3,600,000 毫秒:

int hours = leftTimeInMilliseconds / 3600000;

然后只需设置相应的文本视图以在您的 onTick 方法中显示这些数字。

【讨论】:

以上是关于24 小时倒计时计时器,带有三个单独的文本视图,分别显示小时、分钟、秒的主要内容,如果未能解决你的问题,请参考以下文章

启动 24 小时计时器,当 24 小时结束时通知用户

vue-实现倒计时功能

JS 倒计时实现代码(时、分,秒)

java 24小时倒计时案例

倒计时

python 计时累积超过24小时时继续往上累加