我的倒数计时器出了问题。 我的应用程序有一个textview说"点击开始"然后倒计时从3开始。我的问题是当我设置textview visibility(View.GONE)倒计时从2开始如果我删除了我设置可见性的行,倒计时工作正常(从3开始)但当然textview仍然可见 我在倒计时使用750而不是1000,因为1000倒计时总是从2开始 为什么会这样?我该如何解决这个问题?
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="60sp"
android:text="Tap to start"
android:id="@+id/starttext"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
public void countdowntostart(){
TextView timetostart = (TextView) findViewById(R.id.starttext);
timetostart.setVisibility(View.GONE);
CountDownTimer countDownTimer = new CountDownTimer(3000, 750) {
TextView timestart = (TextView) findViewById(R.id.playtimetext);
public void onTick(long millisUntilFinished) {
timestart.setText("" + millisUntilFinished/1000);
}
public void onFinish() {
abcuu();
}
}.start();
}
答案 0 :(得分:0)
TextView
的文本显示2是因为系统在运行onTick()
方法之前已经计算了一秒,因此即使文本视图是3秒倒计时仍然是准确的倒计时开始时显示2。如果您希望在TextView
上显示倒计时值,可以在倒计时前将初始倒计时值设置为TextView
。
答案 1 :(得分:0)
可能是因为您没有给它特定的舍入。试试Math.ceil((double) millisecondsUntilFinished / 1000);
如果这不起作用,您也可以尝试将timerstart
文字设置为&#34; 3&#34;当你启动计时器。否则,我创建了一个CustomCountdownTimer类,它也有onStarted,onPaused等的抽象方法。
public abstract class CustomCountdownTimer {
private long millisInFuture;
private long countDownInterval;
private int secondsRemaining;
private boolean started;
private boolean paused;
public enum State {
RUNNING,
PAUSED,
STOPPED,
RESUMED,
STARTED,
FINISHED,
STATUS
}
public CustomCountdownTimer(long millisInFuture, long countDownInterval) {
this.millisInFuture = millisInFuture;
this.countDownInterval = countDownInterval;
started = false;
paused = false;
initialize();
}
public boolean isPaused(){
return paused && isStarted();
}
public boolean isStarted(){
return started;
}
public void pause() {
paused = true;
timerPaused(secondsRemaining);
}
public void resume(){
paused = false;
timerResumed(secondsRemaining);
}
public CustomCountdownTimer start() {
started = true;
return this;
}
public void stop() {
millisInFuture = 0;
}
public void initialize() {
final Handler handler = new Handler();
Log.v("status", "starting");
final Runnable counter = new Runnable(){
public void run(){
long sec = millisInFuture / 1000;
if(started && !paused) {
if(millisInFuture <= 0) {
Log.v("status", "done");
started = false;
onFinish();
} else {
Log.v("status", Long.toString(sec) + " seconds remain");
millisInFuture -= countDownInterval;
secondsRemaining = (int) Math.ceil(((double) millisInFuture / 1000));
handler.postDelayed(this, countDownInterval);
onTick(secondsRemaining);
}
} else {
Log.v("status", Long.toString(sec) + " seconds remain and timer has stopped!");
handler.postDelayed(this, countDownInterval);
}
}
};
handler.postDelayed(counter, countDownInterval);
}
public void updateTimer(long millisInFuture){
this.millisInFuture = millisInFuture;
}
public abstract void onTick(int secondsRemaining);
public abstract void onFinish();
public abstract void timerPaused(int secondsRemaining);
public abstract void timerResumed(int secondsRemaining);
}
像这样使用:
CustomCountdownTimer customCountdownTimer = new CustomCountdownTimer(totalSeconds * 1000, 1000) {
@Override
public void onTick(int remainingSeconds) {
// Code on each tick
}
@Override
public void onFinish() {
// Code to run when finished
}
@Override
public void timerPaused(int remainingSeconds) {
// Code to run if paused
}
@Override
public void timerResumed(int remainingSeconds) {
// Code to run when resumed from paused
}
};
customCountdownTimer.start();
请注意,这是使用完整秒而不是毫秒,并且它会在您启动时显示3而不是2,因为它会像我之前建议的那样向上舍入。