我有一个CountDown计时器,用于获取numberPicker的值。我的倒数计时器的文本将更改为数字选择器的值,但是当我点击开始按钮时,倒数计时器文本视图的值将更改为其他数字。所以我的问题是我如何编辑我的代码,以便当我点击开始时它开始倒数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() {
}
}}
感谢:)
答案 0 :(得分:1)
在你的onValueChange()中你用倒数计时器设置(newVal * 60 * 60)
timer = new CounterClass((newVal * 60 * 60), 1000);
然后你用长milis设置textview(newVal * 60 * 60 * 1000)
long millis = (newVal * 60 * 60 * 1000);
所以与您初始化倒数计时器相比,差异为* 1000