我正在尝试一个简单的计数器用于测验应用程序。在这里我想从按下按钮的计时器中减去一秒钟。我使用了一个包含真实值的标志..当按下按钮时它变为假..然后我检查标志的值,并尝试从计时器减去几秒钟... bt,它不起作用。
这是我尝试过的代码...
Java代码:
package com.okj.hjhu;
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class OkghActivity extends Activity implements OnClickListener {
TextView tv;
Button a;
Boolean flag = true;
CountDownTimer counter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Initializer();
setlisteners();
MyCount counter = new MyCount(120000, 1000);
counter.start();
}
private void setlisteners() {
// TODO Auto-generated method stub
a.setOnClickListener(this);
}
private void Initializer() {
tv = (TextView) findViewById(R.id.textView1);
a = (Button) findViewById(R.id.button1);
}
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
tv.setText("done");
}
@Override
public void onTick(long millisUntilFinished) {
tv.setText("Lef Time ... 00:00:" + millisUntilFinished / 1000);
if (flag = false) {
long ty = millisUntilFinished - 9000;
tv.setText("Lef Time ... 00:00:" + ty / 1000);
}
}
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
//when the button will be pressed the counter will minus some second from the timer
flag = false;
break;
case R.id.button2:
break;
case R.id.button3:
break;
case R.id.button4:
break;
}
}
}
请帮忙 ...........
最佳答案
我认为方法是,您必须使用Counterdowntimer.cancel()取消当前的countdowntimer,然后在新的特定时间/
关于java - 如何从倒数计时器中减去几秒钟?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10773492/