我的倒数计时器的 millisUntilFinished 不会根据用户输入而改变
the millisUntilFinished of my countdown timer would not change according to user input
这是我的问题:
我从两个 numberPickers 获得用户输入。然后我使用收集到的数字来设置我的倒数计时器的 millisInFuture
。但问题是:无论我选择什么数字,倒数计时器的 millisUntilFinished
都不会根据用户输入而改变。
我的代码简化如下:
int firstMin = 1;
int firstSec = 30;//initial value in the field
int i = 1; //to determine whether the remained time should be shown
private void initFirstMinPicker() {
minPicker.setMaxValue(60);
minPicker.setValue(firstMin);//set the current value of number picker
minPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
firstMin = newVal;//pass the selected int to firstMin
}
});
//My countdown timer is supposed to start on clicking a button
firstTimer = new CountDownTimer(firstMin * 60000, 500) {//use 500 to
avoid skipping the last onTick()
@Override
public void onTick(long millisUntilFinished) {
Log.d("TAG", "onTick:firstMin " + firstMin);
if (i % 2 == 0) {
workLeft.setText(String.valueOf(millisUntilFinished / 1000));
}
i++;
}
@Override
public void onFinish() {
}
};
我的标签:
D/TAG: onTick:firstMin 6
D/TAG: onTick:firstMin 6
D/TAG: onTick:firstMin 6
很明显 firstMin
已经改变了,但是当我使用 firstMin * 60000
作为参数时, millisUntilFinished
总是从 60000 开始,不管 firstMin * 60000
多少评估为。也就是说,当我的倒数计时器开始倒数时,它总是从60(firstMin * 60的初始值)开始倒数。我不知道为什么会这样。大家遇到过这样的问题吗?非常感谢任何帮助。
您只声明了 firstTimer
一次,这意味着它只会在 firstmin = 1
时创建。每当调用 onValueChanged
时,您都需要创建一个 firstTimer
的新实例,例如:
minPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
firstMin = newVal;//pass the selected int to firstMin
firstTimer = new CountDownTimer(firstMin * 60000, 500) { ... }
}
});
可能还值得创建一个 returns 新 CountDownTimer
的函数,仅采用 firstMin
的值并自动填充其余默认值,以节省不必要的代码重复。
这是我的问题:
我从两个 numberPickers 获得用户输入。然后我使用收集到的数字来设置我的倒数计时器的 millisInFuture
。但问题是:无论我选择什么数字,倒数计时器的 millisUntilFinished
都不会根据用户输入而改变。
我的代码简化如下:
int firstMin = 1;
int firstSec = 30;//initial value in the field
int i = 1; //to determine whether the remained time should be shown
private void initFirstMinPicker() {
minPicker.setMaxValue(60);
minPicker.setValue(firstMin);//set the current value of number picker
minPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
firstMin = newVal;//pass the selected int to firstMin
}
});
//My countdown timer is supposed to start on clicking a button
firstTimer = new CountDownTimer(firstMin * 60000, 500) {//use 500 to
avoid skipping the last onTick()
@Override
public void onTick(long millisUntilFinished) {
Log.d("TAG", "onTick:firstMin " + firstMin);
if (i % 2 == 0) {
workLeft.setText(String.valueOf(millisUntilFinished / 1000));
}
i++;
}
@Override
public void onFinish() {
}
};
我的标签:
D/TAG: onTick:firstMin 6
D/TAG: onTick:firstMin 6
D/TAG: onTick:firstMin 6
很明显 firstMin
已经改变了,但是当我使用 firstMin * 60000
作为参数时, millisUntilFinished
总是从 60000 开始,不管 firstMin * 60000
多少评估为。也就是说,当我的倒数计时器开始倒数时,它总是从60(firstMin * 60的初始值)开始倒数。我不知道为什么会这样。大家遇到过这样的问题吗?非常感谢任何帮助。
您只声明了 firstTimer
一次,这意味着它只会在 firstmin = 1
时创建。每当调用 onValueChanged
时,您都需要创建一个 firstTimer
的新实例,例如:
minPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
firstMin = newVal;//pass the selected int to firstMin
firstTimer = new CountDownTimer(firstMin * 60000, 500) { ... }
}
});
可能还值得创建一个 returns 新 CountDownTimer
的函数,仅采用 firstMin
的值并自动填充其余默认值,以节省不必要的代码重复。