2016-01-20 126 views
1

我想做一個倒計時應用程序,最大時間將在10分鐘以下,我想它顯示爲毫米:ss:毫秒(不太確定如果毫秒是爲毫秒)。這就是我在這裏安卓倒計時到毫秒

 text = (TextView) this.findViewById(R.id.counterView); 
    new CountDownTimer(16069000, 10) { // adjust the milli seconds here 
     public void onTick(long millisUntilFinished) { 
      text.setText(""+String.format("%02d:%02d:%02d", 
        TimeUnit.MILLISECONDS.toHours(millisUntilFinished), 
        TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)), 
        TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)) 
        )); 
     } 

     public void onFinish() { 
      text.setText("done!"); 
     } 
    }.start(); 

它是目前顯示hh:mm:ss,我試圖玩弄它,但無法得到它的工作。我試圖把格式

「%02D:%02D:%02D:%03D」

但是當我試圖添加的setText內的毫秒部分我迷路了。

回答

2

你明確地顯示小時:分鐘:秒在這裏:

 text.setText(""+String.format("%02d:%02d:%02d", 
       TimeUnit.MILLISECONDS.toHours(millisUntilFinished), 
       TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)), 
       TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)) 
       )); 

改變它是這樣的:

 long m = TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished); 
     long s = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished - m*60*1000); 
     long ms = millisUntilFinished - m*60*1000 - s*1000; 
     text.setText(""+String.format("%02d:%02d:%02d",m,s,ms)); 
0

你是什麼迷路呢?

如果你問放什麼價值,爲毫秒部分,只需使用

millisUntilFinished % 1000 
1

這裏是代碼

CounterClass timer = new CounterClass(150000, 1000); // set time here 
    timer.start(); 

define class here 

public class CounterClass extends CountDownTimer { 
     public CounterClass(long millisInFuture, long countDownInterval) { 
      super(millisInFuture, countDownInterval); 
     } 

     @Override 
     public void onFinish() { 
      // timeTextView.setText("Completed."); 

      this.cancel(); 

      finish(); 
     } 

     public void onTick(long millisUntilFinished) { 
      long millis = millisUntilFinished; 
      caaryLong = millis; 
      String hms = String.format(
        "%02d:%02d", 
        TimeUnit.MILLISECONDS.toMinutes(millis) 
          - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS 
            .toHours(millis)), 
        TimeUnit.MILLISECONDS.toSeconds(millis) 
          - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS 
            .toMinutes(millis))); 
      System.out.println(hms); 

      timeTextView.setText(hms); 
     } 
    } 
1

如果你正在使用Android的工作室之外還有一個外部庫

第1步:在build.gradle(Module App)

編譯 '喬達時間:喬達時間:2.9.1'

步驟2:在您的活動.xml文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/ll_main" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/darker_gray" 
    android:gravity="center_horizontal" 
    android:orientation="vertical" 
    tools:context="com.countdowntimer.MainActivity"> 

<TextView 
     android:id="@+id/tv_milliseconds" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     android:gravity="center" 
     android:text="MilliSeconds" 
     android:textColor="@android:color/white" 
     android:textSize="80sp" /> 

    <Chronometer 
     android:id="@+id/chronometer" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:visibility="gone" /> 
</LinearLayout> 

第3步:初始化的TextView和天文臺

CountDownTimer countDownTimer; 
    private long startTime = 1000; 
    private final long interval = 100; 

步驟4:在OnChronometerTickListener

chronometer.start(); 
chronometer.setBase(1); 
chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() { 
      @Override 
      public void onChronometerTick(Chronometer chronometer) { 
       setDateTime(); 
      } 
     }); 

第5步:創建功能

public void setDateTime() { 
     countDownTimer = new MalibuCountDownTimer(startTime, interval); 
     countDownTimer.start(); 

} 

public class MalibuCountDownTimer extends CountDownTimer { 

     public MalibuCountDownTimer(long startTime, long interval) { 
      super(startTime, interval); 
     } 

     @Override 
     public void onFinish() { 
     } 

     @Override 
     public void onTick(long millisUntilFinished) { 
      tvMilliseconds.setText("" + millisUntilFinished); 
     } 
    } 
1

其實我設法得到它與下面的代碼text.setText(""+String.format("%02d:%02d:%03d", TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)), TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)), TimeUnit.MILLISECONDS.toMillis(millisUntilFinished) - TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished)) ));

完成