我最近开始研究我的第一个Android应用程序,我有点卡住了。我的应用程序使用CountDownTimer类,似乎故障 - CountDownTimer.cancel()不起作用。我知道如何通过修改CountDownTimer.java文件来解决这个问题,但我没有这样做的权限。我在Android Studio工作。我在网上找不到解释,即使它看起来是一个基本问题,不是吗?也许我没有使用正确的条款?
非常感谢你的帮助,如果我的问题有点无知,我道歉。哈希。
答案 0 :(得分:0)
尝试这种方式:
public class Main extends Activity implements OnClickListener
{
private static final String tag = "Main";
private MalibuCountDownTimer countDownTimer;
private long timeElapsed;
private boolean timerHasStarted = false;
private Button startB;
private TextView text;
private TextView timeElapsedView;
private final long startTime = 50000;
private final long interval = 1000;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startB = (Button) this.findViewById(R.id.button);
startB.setOnClickListener(this);
text = (TextView) this.findViewById(R.id.timer);
timeElapsedView = (TextView) this.findViewById(R.id.timeElapsed);
countDownTimer = new MalibuCountDownTimer(startTime, interval);
text.setText(text.getText() + String.valueOf(startTime));
}
@Override
public void onClick(View v)
{
if (!timerHasStarted)
{
countDownTimer.start();
timerHasStarted = true;
startB.setText("Start");
}
else
{
countDownTimer.cancel();
timerHasStarted = false;
startB.setText("RESET");
}
}
// CountDownTimer class
public class MalibuCountDownTimer extends CountDownTimer
{
public MalibuCountDownTimer(long startTime, long interval)
{
super(startTime, interval);
}
@Override
public void onFinish()
{
text.setText("Time's up!");
timeElapsedView.setText("Time Elapsed: " + String.valueOf(startTime));
}
@Override
public void onTick(long millisUntilFinished)
{
text.setText("Time remain:" + millisUntilFinished);
timeElapsed = startTime - millisUntilFinished;
timeElapsedView.setText("Time Elapsed: " + String.valueOf(timeElapsed));
}
}
}