這就是我做到的。在我自己的計數器類中使用TextView擴展CountDownTimer。
public class myCounter extends CountDownTimer
{
TextView counter;
public myCounter(final long millisInFuture, final long countDownInterval,
final TextView newCounter)
{
super(millisInFuture, countDownInterval);
counter = newCounter;
counter.setText("Left: " + millisInFuture);
}
@Override
public void onFinish()
{
counter.setText("GO!");
}
@Override
public void onTick(final long millisUntilFinished)
{
counter.setText("Left: " + millisUntilFinished/1000);
}
}
然後在我的活動:
public class CountdownViewActivity extends Activity
{
@Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.countdownview);
final Button startBtn = (Button) findViewById(R.id.startButton);
//Send total time in milliseconds, the interval to display at, and the TextView from your xml it should display in
final myCounter countdown = new myCounter(timeInSeconds * 1000, 1000, (TextView) findViewById(R.id.textView4));
startBtn.setOnClickListener(startClick(countdown));
//MoreStuff
}
private OnClickListener startClick(final myCounter countdown)
{
return new OnClickListener()
{
@Override
public void onClick(final View v)
{
countdown.start();
}
};
}
}
顯示你的代碼中,我們是不是讀心在這裏非常好。 – Squonk