我的问题是在底部,我有一个布尔值, 正在进行 ,这是假的。按下按钮应该成为真,然后启动倒数计时器,但不启动计时器。计时器工作在代码中的其他位置。对不起,如果它是一个完全混乱我不熟悉。
public int counter;
public boolean ongoing = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//0-59 number picker to represent seconds
final NumberPicker secPicker = (NumberPicker) findViewById(R.id.secPicker);
secPicker.setMaxValue(59);
Button btnStart = (Button) findViewById(R.id.btnStart);
Button btnSet = (Button) findViewById(R.id.btnSet);
Button btnPause = (Button) findViewById(R.id.btnPause);
final TextView label = (TextView) findViewById(R.id.txtCount);
//sets the time chosen to timer with button press btnSet
btnSet.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
counter = secPicker.getValue();
label.setText(String.valueOf(counter));
}
}
);
//starts timer from selected time, from number picker, with btnStart
btnStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ongoing = true;
}
});
//disables, not worried about this yet
btnPause.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ongoing = false;
}
});
//supposed to turn on a timer with button press btnStart, but it does nothing this is my problem.
if (ongoing == true) {
//any replacement for 9999999, as an infinite value?
new CountDownTimer(999999999, 1000) {
public void onTick(long millisUntilFinished) {
label.setText(String.valueOf(counter));
counter--;
if (counter == -1) {
counter = 0;
cancel();
}
}
public void onFinish() {
}
}.start();
}
}
}
答案 0 :(得分:0)
if (ongoing == true) // this will never work for you
在点击任何可以调用的内容之前,你已经将上面的代码放在了oncreate中。
要么在btnStart点击事件中写下面的代码,要么创建一个方法并从中调用。
//应该按下按钮按btnStart打开一个计时器,但它没有做任何事这是我的问题。
if (ongoing == true) {
//any replacement for 9999999, as an infinite value?
new CountDownTimer(999999999, 1000) {
public void onTick(long millisUntilFinished) {
label.setText(String.valueOf(counter));
counter--;
if (counter == -1) {
counter = 0;
cancel();
}
}
public void onFinish() {
}
}.start();
}
希望这些可以帮助你。