出于某种原因,每当我尝试更新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 :(得分:1)
您的countDownInterval为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
答案 1 :(得分:0)
它不会更新,因为您正在通过0
尝试传递1000
,这将以1秒的间隔更新它