我有一个CountDownTimer类来设置一个倒计时,当用户点击一些Button时,它将启动 这是包含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));
但它不起作用,文字显示" Time' Up"而不是倒计时 有谁知道如何解决这个问题?
答案 0 :(得分:0)
您的startTime
和interval
被错误地设置为秒。
只需将两者乘以1000即可,因为您需要毫秒。
另请注意,您不需要长,以保持这么低的值 整数会做。
private final int startTime = 10000; // 10 secs
private final int interval = 1000; // 1s = 1000 ms
这也没有意义:millisUntilFinished/1
(将任何数字除以1会得到原始数字)
必须是millisUntilFinished / 1000