2015-09-05 51 views
0

我有一個CountDownTimer類設置倒計時,當用戶點擊某個按鈕時將開始倒計時。
這是包括CountDownTimer文字在CountDownTimer中不顯示字符串

public class play extends Activity implements View.OnClickListener{ 

     private TapCountDownTimer countDownTimer; 
     private final long startTime = 10; 
     private final long interval = 1; 
     private TextView countdowntext; 


     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 
      setContentView(R.layout.play); 

     countDownTimer = new TapCountDownTimer(startTime, interval); 
     countdowntext = (TextView)findViewById(R.id.countdowntext); 
     countdowntext.setText(String.valueOf(startTime)); 

      Button buttonstart = (Button)findViewById(R.id.stopbutton); 
        buttonstart.setOnClickListener(new View.OnClickListener() { 

        public void onClick(View v){ 
         countDownTimer.start(); 
         }     
       }); 

      Button buttonstop = (Button)findViewById(R.id.stopbutton); 
        buttonstop.setOnClickListener(new View.OnClickListener() { 

        public void onClick(View v){ 
         countDownTimer.cancel(); 
         }     
       }); 

    public class TapCountDownTimer extends CountDownTimer { 

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

     @Override 
     public void onTick(long millisUntilFinished) 
      { 
      countdowntext.setText(Long.toString(millisUntilFinished/1)); 

      } 
     @Override 
     public void onFinish() 
      { 
      countdowntext.setText("Time's up!"); 
      } 
     } 
    } 

我設置文本在此行

countdowntext.setText(Long.toString(millisUntilFinished/1)); 

的代碼,但它不工作,文本顯示,而不是倒計時「時間到」。
有誰知道如何解決這個問題?

+0

也許這是因爲'interval'設置爲'1' – Titus

+0

@ FrankN.Stein好吧我會改變 – RichFounders

回答

0

您的startTimeinterval錯誤設置在
只需乘以1000,因爲您需要毫秒

另請注意,您不需要 s,用於保持這樣的低值。
整數會做。

private final int startTime = 10000; // 10 secs 
    private final int interval = 1000; // 1s = 1000 ms 

而且這是沒有意義的:millisUntilFinished/1(將任意數量由1給你原來的號碼)。
它必須是millisUntilFinished/1000