gpt4 book ai didi

java - 倒数计时器 onTick 方法不起作用

转载 作者:行者123 更新时间:2023-11-30 01:42:35 25 4
gpt4 key购买 nike

我有一个 CountDown 计时器,它获取 numberPicker 的值。我的倒计时计时器的文本将更改为数字选择器的值,但是当我点击开始按钮时,倒计时计时器 TextView 的值更改为不同的数字。所以我的问题是如何编辑我的代码,以便当我点击开始时它开始倒计时 textView 的值。这是我的代码...

public class MainActivity extends AppCompatActivity {
NumberPicker noPickerHours;
NumberPicker noPickerMinutes;
TextView textViewTime;
Button button;
CounterClass timer;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

textViewTime = (TextView) findViewById(R.id.textView);

noPickerMinutes = (NumberPicker) findViewById(R.id.numberPicker2);
noPickerMinutes.setMaxValue(59);
noPickerMinutes.setMinValue(0);
noPickerHours = (NumberPicker) findViewById(R.id.numberPicker);
noPickerHours.setMaxValue(24);
noPickerHours.setMinValue(0);
button = (Button)findViewById(R.id.button);



button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
timer.start();
}
});

noPickerHours.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {


@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
timer = new CounterClass((newVal * 60 * 60), 1000);
long millis = (newVal * 60 * 60 * 1000);
String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
textViewTime.setText(hms);
}
});



FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}




public class CounterClass extends CountDownTimer {


/**
* @param millisInFuture The number of millis in the future from the call
* to {@link #start()} until the countdown is done and {@link #onFinish()}
* is called.
* @param countDownInterval The interval along the way to receive
* {@link #onTick(long)} callbacks.
*/

public CounterClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}



@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override

public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis) * 60));
System.out.println(hms);
textViewTime.setText(hms);

}

@Override
public void onFinish() {

}


}}

谢谢:)

最佳答案

在您的 onValueChange() 中,您正在使用 (newVal * 60 * 60) 设置倒数计时器

timer = new CounterClass((newVal * 60 * 60), 1000);

然后你用长 milis (newVal * 60 * 60 * 1000) 设置 textview

long millis = (newVal * 60 * 60 * 1000);

因此与您初始化倒数计时器的内容相比,差异是 * 1000

关于java - 倒数计时器 onTick 方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34246478/

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