java - 在 Android CountDownTimer 中正确显示分钟

标签 java android timer long-integer countdowntimer

我有一个带有计时器的 Android 应用程序。默认情况下,显示为 00:00:00(小时、分钟、秒)。用户可以按 H M S 并围绕圆圈拖动 slider 来更改小时、分钟或秒,具体取决于单击的哪一个。我试图通过解析可更改的 TextView 中的小时、分钟和秒的数字来创建一个 CountDownTimer,将其全部转换为秒,然后启动计时器;然而,问题是它显示的是总分钟数,而不是保持在 59 及以下。例如,如果我倒计时 3 小时。显示 2 小时 179 分 59 秒。当我想要 2 小时时。 59 分 59 秒。

enter image description here

我不确定为什么会收到此错误。我在代码中获得了所有正确的值。

这是我的类中 CountDownTimer 的 setActionListener 代码,我对我认为可能出现错误的位置发表了评论。:

private void setActionListeners() {

    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);




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

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

            hourint = Integer.valueOf(number_text.getText().toString());

            minuteint = Integer.valueOf(minute_text.getText().toString());

            secondint = Integer.valueOf(second_text.getText().toString());

            Log.i("YourActivity", "Hours: " + hourint);

            Log.i("YourActivity", "Minutes: " + minuteint);

            Log.i("YourActivity", "Seconds: " + secondint);

            totalTimeCountInMilliseconds = ((hourint*60*60) +(minuteint*60) + (secondint)) * 1000;      // time count for 3 minutes = 180 seconds
            timeBlinkInMilliseconds = totalTimeCountInMilliseconds/1000;

            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;
                    mSeekArc.setVisibility(View.INVISIBLE);


                    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
                    }

                    second_text.setText(String.format("%02d", seconds % 60));  


                    //THE ERROR MIGHT BE HERE FOR THE MINUTE
                    minute_text.setText(String.format("%02d", seconds / 60));


                    number_text.setText(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!");
                    number_text.setVisibility(View.VISIBLE);
                    minute_text.setVisibility(View.VISIBLE);
                    second_text.setVisibility(View.VISIBLE);
                    mSeekArc.setVisibility(View.VISIBLE);
                }

            }.start();

        }
    });

我曾尝试篡改该值并找到它的值一段时间,但这是一个非常持久的问题,只是无法正确显示。

看起来我做的一切都是正确的:

Countdown timer in HH:MM:SS format in Android

问题到底是什么以及如何解决?

如果需要的话,这是我的类(class)的完整代码:

http://pastebin.com/yquQPtG5

最佳答案

正如您所怀疑的,您的错误就在这里。

minute_text.setText(String.format("%02d", seconds / 60));

您需要减去小时数。您还可以使用您的格式对余数进行模运算,如下所示

minute_text.setText(String.format("%02d", (seconds / 60) % 60));

更好的方法是提前计算值,然后对占用的值进行格式化。

int just_hours = seconds / 3600;
int just_minutes = (seconds/60) % 60;
int just_seconds = seconds % 60;

关于java - 在 Android CountDownTimer 中正确显示分钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25314562/

相关文章:

Excel VBA : Application. OnTime 未正确执行

java - 从其他类继承方法

java - 将单元格合并到 MigLayout?

java - 为什么 Lombok @Builder 与此构造函数不兼容?

c++ - Boost timer - 如何设置细节级别,我想要微秒?

java - JPanel GUI 正在 self 更新

java - 解析 XML 时出错 : unbound prefix Admob

java - Android SQLite 在使用 onCreate() 方法首次加载插入 ContentValues 后为空

android - 如何在 Activity 重新启动时保留复杂对象?

android - 如何使 MultiAutoCompleteTextView 搜索子字符串