gpt4 book ai didi

java - TextView不显示计时器

转载 作者:行者123 更新时间:2023-12-01 13:42:46 26 4
gpt4 key购买 nike

出于某种原因,每当我尝试更新我的 TextView 以显示一个人还剩多少秒时。但由于某种原因现在它不起作用。

public class MainActivity extends Activity {
private int index=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
final int[] Times= {6000,3000,7000,3000,4000,6000,3000};
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button1);
final TextView textview =(TextView) findViewById(com.example.countdowntest.R.id.textView);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v){
if(index<Times.length){
CountDownTimer timer = new CountDownTimer(
Times[index], 0) {
public void onTick(long millisUntilFinished) {
textview.setText("seconds remaining: "+ millisUntilFinished / 1000);
}

public void onFinish() {
textview.setText("done!");
index++;
}

}.start();
}
}
});
}
}

它应该显示更新的秒数,直到完成,因为它全部在 onClick 方法中,直到计时器达到零时才会结束。

最佳答案

您的倒计时间隔为 0

CountDownTimer(long millisInFuture, long countDownInterval)

你拥有什么

 new CountDownTimer(Times[index], 0) { // second param to the constructor is 0.

应该是

new CountDownTimer(30000, 1000)  // whatever value you like 

http://developer.android.com/reference/android/os/CountDownTimer.html

关于java - TextView不显示计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20580836/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com