如何使用秒和毫秒进行倒计时

时间:2013-09-27 21:37:59

标签: android countdowntimer

我正在使用android studio到一个倒计时从10秒开始的应用程序。我写了代码,它工作正常,但它只显示剩余的秒数,现在我想要倒计时显示毫秒。你能帮我吗? 这是代码

public class MainActivity extends Activity {
TextView txtCount;
Button btnCount;
int count = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txtCount = (TextView)findViewById(R.id.textView1);
    txtCount.setText(String.valueOf(count));
    btnCount = (Button)findViewById(R.id.button1);

    btnCount.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            count++;
            txtCount.setText(String.valueOf(count));
        }
    });
    final TextView textic = (TextView) findViewById(R.id.textView2);

    CountDownTimer Count = new CountDownTimer(10000, 1000) {
        public void onTick(long millisUntilFinished) {
            int seconds = (int) ((millisUntilFinished / 1000));

            textic.setText(seconds + "seconds " + millisUntilFinished / 1000);

        }

        public void onFinish() {
            textic.setText("TEMPO SCADUTO");
        }
    };

    Count.start();



}

4 个答案:

答案 0 :(得分:2)

//start button click
     CountDown timer = new CountDown(180000, 1000);
     timer.start();

//stop button click
timer.stop();   

    //countdown class
    public class CountDown extends CountDownTimer {

          public CountDown(long millisInFuture, long countDownInterval) {
             super(millisInFuture, countDownInterval);
          }

          @Override
          public void onTick(long millisUntilFinished) {
             long ms = millisUntilFinished;
             String text = String.format("%02d\' %02d\"",
                                         TimeUnit.MILLISECONDS.toMinutes(ms) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(ms)),
                                         TimeUnit.MILLISECONDS.toSeconds(ms) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(ms)));
             textViewTimer.setText(text);
          }

          @Override
          public void onFinish() {
             textViewTimer.setText("ffinish");
          }
       }

答案 1 :(得分:2)

考虑到CountDownTimer跳过一秒钟(https://stackoverflow.com/a/6811744/1225669)的问题,我已经实现了秒和毫秒的倒计时。它对我有用。

private static final long NUMBER_MILLIS = 20000;
private static final String MILLISECONDS_FORMAT = "%03d";
private int secondsLeft = 0;

//
new CountDownTimer(NUMBER_MILLIS, 1) {

        public void onTick(long millisUntilFinished) {                    

                if (Math.round((float)millisUntilFinished / 1000.0f) != secondsLeft)
                {
                    secondsLeft = Math.round((float)millisUntilFinished / 1000.0f);
                }
                long roundMillis = secondsLeft * 1000;
                if(roundMillis==NUMBER_MILLIS){
                    tvTimer.setText(secondsLeft
                            + "." + String.format(MILLISECONDS_FORMAT, 0));
                }else {
                    tvTimer.setText(secondsLeft
                            + "." + String.format(MILLISECONDS_FORMAT, millisUntilFinished % 1000));
                }
        }

        public void onFinish() {

            tvTimer.setText("done!");
        }
}.start();

答案 2 :(得分:1)

使用余数运算符

millisUntilFinished % 1000

写一些

的内容
textic.setText(seconds + "seconds " + millisUntilFinished / 1000 + " ms" + 
millisUntilFinished / 1000);

虽然在你的情况下,它总是返回0,因为你的计时器每1000毫秒滴答一次。 为了获得更好的图片,请尝试使用较小的自动收报机时间值,例如100毫秒或250毫秒。

CountDownTimer Count = new CountDownTimer(10000, 100){...

答案 3 :(得分:0)

以下是倒计时类的片段,格式为 display = "00:0" (seconds:milliseconds)

  mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1)  

  private void updateCountDownText() {
          int seconds = (int) (mTimeLeftInMillis / 1000) % 60;
          int miliseconds = (int) (mTimeLeftInMillis / 100) % 10;
          String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%01d", seconds, miliseconds);
          TextView_Countdown.setText(timeLeftFormatted);