2011-05-07 23 views
0

我正在學習Android和Java,同時爲自己構建計時器應用程序。
引用舊的線程Android - Controlling a task with Timer and TimerTask? 我想創建Runnable方法來倒計時我的計時器。
我堅持的基本的java問題是類怎麼附加postDelayed()調用?新的開發人員 - 涉及計時器實現的基本Java問題

我的活動被稱爲TimerButtons現在,我想這會工作:

package com.TimerButtons; 

    import android.app.Activity; 
    import android.graphics.Color; 
    import android.graphics.PorterDuff; 
    import android.graphics.PorterDuffColorFilter; 
    import android.graphics.drawable.Drawable; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.TextView; 

    public class TimerButtons extends Activity { 

     private TextView mDisplayTime; 

     private Button mButtonStart; 
     private Button mButtonStop; 

     private int timeTenths = 0; 

     private Drawable d; 
     private PorterDuffColorFilter filter; 

     // capture our View elements 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 

      filter = new PorterDuffColorFilter(Color.DKGRAY, PorterDuff.Mode.SRC_ATOP); 
      d = findViewById(R.id.buttonStart).getBackground(); d.setColorFilter(filter); 
      d = findViewById(R.id.buttonStop).getBackground(); d.setColorFilter(filter); 

      mDisplayTime = (TextView) findViewById(R.id.displayTime); 

      mButtonStart = (Button) findViewById(R.id.buttonStart); 
      mButtonStop = (Button) findViewById(R.id.buttonStop); 


      // add click listeners to the buttons 
      mButtonStart.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
           new Thread(r).start(); 
       } 
      }); 

      mButtonStop.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        timeTenths = 0; 
        updateDisplay(); 
        updateSetting(); 
       } 
      }); 

      // display the current time 
      updateDisplay(); 

     } 

     // runtime methods below here 
     // updates the time we display in the TextView 
     private void updateDisplay() { 
      mDisplayTime.setText(
        String.valueOf(((float)timeTenths)/10) 
      ); 
     } 

     private void updateSetting() { 
      mTensDigit.setText(String.valueOf(timeTenths/100)); 
      mOnesDigit.setText(String.valueOf((timeTenths%100)/10)); 
      mTenthsDigit.setText(String.valueOf(timeTenths%10)); 
     } 

     Runnable r = new Runnable() 
     { 
      public void run() 
      { 
       if (timeTenths >= 1) 
       { 
        timeTenths -= 1; 
        if (timeTenths != 0) 
         mDisplayTime.postDelayed(this, 100); 
        updateDisplay(); 
       } 
      } 
     }; 

    } 

我得到的錯誤:方法postDelayed(新的Runnable(){},INT)是未定義的類型TimerButtons在註釋行中。

感謝您的任何noob指導!
Dave

+0

什麼是TimerButtons? – Varun 2011-05-07 16:14:09

+0

TimerButtons是我的應用程序的活動 - 頂級... – DBell 2011-05-07 16:29:25

回答

0

如果TimerButtons是你的活動,那應該工作。試試這個:

TimerButtons.this.postDelayed(this, 1000); 

EDIT(爲後人):我最初是錯誤的:postDelayed是在View,而不是一個Context定義。使用

mDisplayTime.postDelayed(this, 1000); 
+0

剛剛嘗試過,但同樣的錯誤。舊的線程談到創建一個處理程序,但我也在那裏輸了。 – DBell 2011-05-07 16:22:54

+0

建立一個顯示周圍方法的代碼示例:如果這不起作用,很難分辨究竟發生了什麼。 – Femi 2011-05-07 16:28:32

+0

討厭發表整個代碼;也許這會有所幫助:我最初發布的代碼是一個完整的方法,是onCreate塊結束後(最後一個)創建的最後一個。戴夫 – DBell 2011-05-07 16:31:02

0

我認爲你有handler類來完成你的任務。

你有方法不同的看法...

我不知道你是真正想這個類的事,但 這是一個實現。

import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.Locale; 
import java.util.TimeZone; 
import java.util.concurrent.TimeUnit; 

import com.skens.sms.R; 

import android.app.Activity; 
import android.graphics.Color; 
import android.graphics.PorterDuff; 
import android.graphics.PorterDuffColorFilter; 
import android.graphics.drawable.Drawable; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.SystemClock; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class TimerButtons extends Activity { 

    private TextView mDisplayTime; 

    private Button mButtonStart; 
    private Button mButtonStop; 

    //private int timeTenths = 0; 

    private Drawable d; 
    private PorterDuffColorFilter filter; 

    private TinyTimer myTimer; 

    // capture our View elements 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     filter = new PorterDuffColorFilter(Color.DKGRAY, PorterDuff.Mode.SRC_ATOP); 

     d = findViewById(R.id.buttonStart).getBackground(); d.setColorFilter(filter); 
     d = findViewById(R.id.buttonStop).getBackground(); d.setColorFilter(filter); 

     mDisplayTime = (TextView) findViewById(R.id.displayTime); 

     mButtonStart = (Button) findViewById(R.id.buttonStart); 
     mButtonStop = (Button) findViewById(R.id.buttonStop); 



     // add click listeners to the buttons 
     mButtonStart.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       //new Thread(r).start(); 
       myTimer.startTimer(1000, mDisplayTime, mTensDigit, mOnesDigit, mTenthsDigit); 
      } 
     }); 

     mButtonStop.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       //timeTenths = 0; 
       //updateDisplay(); 
       updateSetting(); 
      } 
     }); 

     myTimer = new TinyTimer(); 

     // display the current time 
     //updateDisplay(); 

    } 

    // runtime methods below here 
    // updates the time we display in the TextView 
    private void updateDisplay() { 
     //mDisplayTime.setText(
     //  String.valueOf(((float)timeTenths)/10) 
     //); 
    } 

    private void updateSetting() { 
     //mTensDigit.setText(String.valueOf(timeTenths/100)); 
     //mOnesDigit.setText(String.valueOf((timeTenths%100)/10)); 
     //mTenthsDigit.setText(String.valueOf(timeTenths%10)); 


    } 

    //Runnable r = new Runnable() 
    //{ 
    // public void run() 
    // { 
    //  if (timeTenths >= 1) 
    //  { 
    //   timeTenths -= 1; 
    //   if (timeTenths != 0) 
    //    mDisplayTime.postDelayed(this, 100); 
    //   updateDisplay(); 
    //  } 
    // } 
    //}; 


    public class TinyTimer { 

     private Handler _handler; 

     private long _startTime = 0; 
     private long _millis; 

     private long _delayMillis; 

     private TextView mDisplayTime; 
     private TextView mTensDigit; 
     private TextView mOnesDigit; 
     private TextView mTenthsDigit; 

     private String _format = String.format("%%0%dd", 2); 

     private int timeTenths = 0; 

     public TinyTimer(){ 
      _handler = new Handler(); 
     } 

     public void startTimer 
     (long delayMillis,TextView DisplayTime, TextView TensDigit, TextView OnesDigit, TextView TenthsDigit){ 
      mDisplayTime = (TextView) DisplayTime; 
      mTensDigit = (TextView) TensDigit; 
      mOnesDigit = (TextView) OnesDigit; 
      mTenthsDigit = (TextView) TenthsDigit; 

      _delayMillis = delayMillis; 
      _startTime = SystemClock.elapsedRealtime(); 
      _handler.removeCallbacks(updateTimerTask); 

      updateTimerTask.run(); 
     } 

     public void stopTimer(){ 
      _handler.removeCallbacks(updateTimerTask); 
     } 

     public void pauseTimer(){ 
      _handler.removeCallbacks(updateTimerTask); 
     } 

     public void unpauseTimer(){ 
      if(_startTime != 0){ 
       updateTimerTask.run();   
      } 
     } 

     private Runnable updateTimerTask = new Runnable() { 

      @Override 
      public void run() { 
       final long start = _startTime; 
       _millis = SystemClock.elapsedRealtime() - start; 


       if (timeTenths >= 1) 
       { 
        timeTenths -= 1; 
        if (timeTenths != 0) 
         mDisplayTime.postDelayed(this, 100); 

        mDisplayTime.setText(String.valueOf(((float)timeTenths)/10)); 
        mTensDigit.setText(String.valueOf(timeTenths/100)); 
        mOnesDigit.setText(String.valueOf((timeTenths%100)/10)); 
        mTenthsDigit.setText(String.valueOf(timeTenths%10)); 
       } 
       _handler.postDelayed(updateTimerTask, _delayMillis); 
      } 
     }; 

     public long getElapsedtime(){ 
      return _millis; 
     } 

     public String getMillis(){ 
      return String.format(_format, _millis % 1000); 
     } 


} 

我實施的課只是一個概念。我沒有測試,但編譯類。