2017-06-18 93 views
-1

我有一個問題,其中我的代碼將不會顯示onTick方法,每次計數器運行時,它只是顯示文本「完成」,因爲它應該在它完成時。看來endTime變量沒有被傳遞給定時器類?任何幫助與我失蹤將不勝感激。倒數計時器不會顯示onTick

public class MainActivity extends AppCompatActivity { 

private long endTime; 
private MyTimer mainTimer; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    //Set up the two number pickers 
    NumberPicker minutes = (NumberPicker) findViewById(R.id.minutes); 
    pickerSetup(minutes); 
    NumberPicker seconds = (NumberPicker) findViewById(R.id.seconds); 
    pickerSetup(seconds); 

    mainTimer = new MyTimer(this, endTime, 1000); 
} 

public void pickerSetup(NumberPicker pickerId){ 
    pickerId.setMaxValue(60); 
    pickerId.setMinValue(0); 
    pickerId.setWrapSelectorWheel(true); 
} 

public void calcTime(View view){ 
    //Get value of Number pickers and convert to milliseconds from minutes and seconds. 
    NumberPicker minutes = (NumberPicker) findViewById(R.id.minutes); 
    int selectedMinutes = minutes.getValue() * 60000; 

    NumberPicker seconds = (NumberPicker) findViewById(R.id.seconds); 
    int selectedSeconds = seconds.getValue() * 1000; 

    //Add selected Minutes and seconds together 
    endTime = selectedMinutes + selectedSeconds; 
    Log.v("End time = ", String.valueOf(endTime)); 
    mainTimer.start(); 
} 
} 

定時器類:

class MyTimer extends CountDownTimer { 

private Context mContext; 
private TextView mToUpdate; 


public MyTimer(Context context, long startTime, long interval) { 
    super(startTime, interval); 
    mContext = context; 
    TextView toUpdate = (TextView) ((Activity)mContext).findViewById(R.id.intervalTimer); 
    mToUpdate = toUpdate; 
} 

@Override 
public void onTick(long millisUntilFinished) { 
    int seconds = (int) (millisUntilFinished/1000) % 60; 
    int minutes = (int) ((millisUntilFinished/(1000 * 60)) % 60); 
    mToUpdate.setText(String.valueOf(minutes) + ":" + String.valueOf(seconds)); 
} 

@Override 
public void onFinish() { 
    mToUpdate.setText("Done"); 
} 

} 

回答

0

它發生,因爲你不會被初始化結束時間時調用mainTimer = new MyTimer(this, endTime, 1000);的時間。它被設置爲0,這就是爲什麼它停止作爲開始。你可以把這兩種方法改成這樣的東西。

public long calcTime(View view){ 
    //Get value of Number pickers and convert to milliseconds from minutes and seconds. 
    NumberPicker minutes = (NumberPicker) findViewById(R.id.minutes); 
    int selectedMinutes = minutes.getValue() * 60000; 

    NumberPicker seconds = (NumberPicker) findViewById(R.id.seconds); 
    int selectedSeconds = seconds.getValue() * 1000; 

    //Add selected Minutes and seconds together 
    endTime = selectedMinutes + selectedSeconds; 
    Log.v("End time = ", String.valueOf(endTime)); 
    return mainTimer.start(); 
} 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    //Set up the two number pickers 
    NumberPicker minutes = (NumberPicker) findViewById(R.id.minutes); 
    pickerSetup(minutes); 
    NumberPicker seconds = (NumberPicker) findViewById(R.id.seconds); 
    pickerSetup(seconds); 

    endTime=calcTime(); 
    mainTimer = new MyTimer(this, endTime, 1000); 
    mainTimer.start(); 
}