【问题标题】:Countdown timer in HH:MM:SS format in AndroidAndroid中HH:MM:SS格式的倒数计时器
【发布时间】:2013-03-14 08:14:09
【问题描述】:

我正在尝试在 android 中开发倒数计时器。 在此示例中,我输入了分钟数作为倒数计时器中显示的参数。

问题是当我输入分钟时,例如 65, 那么计数器将是 65:00。我希望柜台显示 1:05:00 即 HH:MM:SS 格式。

public class MainActivity extends Activity implements OnClickListener {

private Button buttonStartTime, buttonStopTime;
private EditText edtTimerValue;
private TextView textViewShowTime; // will show the time
private CountDownTimer countDownTimer; // built in android class
                                        // CountDownTimer
private long totalTimeCountInMilliseconds; // total count down time in
                                            // milliseconds
private long timeBlinkInMilliseconds; // start time of start blinking
private 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);

    buttonStartTime = (Button) findViewById(R.id.btnStartTime);
    buttonStopTime = (Button) findViewById(R.id.btnStopTime);
    textViewShowTime = (TextView) findViewById(R.id.tvTimeCount);
    edtTimerValue = (EditText) findViewById(R.id.edtTimerValue);

    buttonStartTime.setOnClickListener(this);
    buttonStopTime.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    if (v.getId() == R.id.btnStartTime) {
        textViewShowTime.setTextAppearance(getApplicationContext(),
                R.style.normalText);
        setTimer();
        buttonStopTime.setVisibility(View.VISIBLE);
        buttonStartTime.setVisibility(View.GONE);
        edtTimerValue.setVisibility(View.GONE);
        edtTimerValue.setText("");
        startTimer();

    } else if (v.getId() == R.id.btnStopTime) {
        countDownTimer.cancel();
        buttonStartTime.setVisibility(View.VISIBLE);
        buttonStopTime.setVisibility(View.GONE);
        edtTimerValue.setVisibility(View.VISIBLE);
    }
}

private void setTimer() {
    int time = 0;
    if (!edtTimerValue.getText().toString().equals("")) {
        time = Integer.parseInt(edtTimerValue.getText().toString());
    } else
        Toast.makeText(MainActivity.this, "Please Enter Minutes...",
                Toast.LENGTH_LONG).show();

    totalTimeCountInMilliseconds = 60 * time * 1000;

    timeBlinkInMilliseconds = 30 * 1000;
}

private void startTimer() {
    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);
            buttonStartTime.setVisibility(View.VISIBLE);
            buttonStopTime.setVisibility(View.GONE);
            edtTimerValue.setVisibility(View.VISIBLE);
        }

    }.start();

}
}

【问题讨论】:

    标签: android timer countdowntimer milliseconds


    【解决方案1】:

    我正在使用以下代码将seconds 中的持续时间转换为HH:MM:SS 格式。

    String.format("%02d:%02d:%02d", durationSeconds / 3600,
                    (durationSeconds % 3600) / 60, (durationSeconds % 60));
    

    您可以轻松地调整它以接受minutes 中的输入,但是您的输出将采用HH:MM:00 的格式,因为您没有秒数,例如:

    String.format("%02d:%02d:%02d", durationMinutes / 60, durationMinutes % 60, 0);
    

    【讨论】:

      【解决方案2】:
      String.format("%02d:%02d:%02d",
                  TimeUnit.MILLISECONDS.toHours(duration)%60,
                  TimeUnit.MILLISECONDS.toMinutes(duration)%60,
                  TimeUnit.MILLISECONDS.toSeconds(duration) % 60)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-24
        • 2022-01-05
        • 2017-07-10
        • 2015-06-16
        相关资源
        最近更新 更多