如何在android中创建倒计时?单击按钮时保持重新启动
我想给我的游戏添加一个计时器。我不完全确定如何做到这一点。我对android编码比较新。游戏需要在一定的时间内点击一个按钮。我有我喜欢的计数器工作方式,但在添加倒计时计时器时遇到问题 计时器应该运行30秒,我希望它显示秒:毫秒,我也不确定 我曾经尝试过创建这个,并添加了计数器和计时器,但我遇到的另一个问题是,我在游戏屏幕上只有一个按钮需要启动计时器和计数,但上次我这么做时,每次我都会单击该按钮,计时器将重新启动,并从上一次单击开始同时计数 有人能帮我吗?如果可能的话,我也希望得到一个简短的解释 以下是我目前的代码:如何在android中创建倒计时?单击按钮时保持重新启动,android,button,timer,countdowntimer,Android,Button,Timer,Countdowntimer,我想给我的游戏添加一个计时器。我不完全确定如何做到这一点。我对android编码比较新。游戏需要在一定的时间内点击一个按钮。我有我喜欢的计数器工作方式,但在添加倒计时计时器时遇到问题 计时器应该运行30秒,我希望它显示秒:毫秒,我也不确定 我曾经尝试过创建这个,并添加了计数器和计时器,但我遇到的另一个问题是,我在游戏屏幕上只有一个按钮需要启动计时器和计数,但上次我这么做时,每次我都会单击该按钮,计时器将重新启动,并从上一次单击开始同时计数 有人能帮我吗?如果可能的话,我也希望得到一个简短的解释
public class GameActivity extends Activity
{
int score = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
/** This is the score calculator */
final ImageView startgametitleImage = (ImageView)findViewById(R.id.startgameImage);
final TextView currentScore = (TextView)findViewById(R.id.gameScore);
currentScore.setText("");
final Button startButton = (Button)findViewById(R.id.startButton);
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
score++;
currentScore.setText("Score: " + score);
currentScore.setTextSize(40);
currentScore.setTextColor(Color.WHITE);
startgametitleImage.setImageDrawable(null);
}
});
}
}
请尝试以下代码:
private TextView txtCount, textViewTimer;
private Button btnCount, btnRestart;
int count = 0;
boolean[] timerProcessing = { false };
boolean[] timerStarts = { false };
private MyCount timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtCount = (TextView) findViewById(R.id.textView1);
txtCount.setText(String.valueOf(count));
btnCount = (Button) findViewById(R.id.button1);
btnRestart = (Button) findViewById(R.id.button2);
textViewTimer = (TextView) findViewById(R.id.textView2);
timer = new MyCount(10000, 1);
btnCount.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// start timer once when button first click
if (!timerStarts[0]) {
timer.start();
timerStarts[0] = true;
timerProcessing[0] = true;
}
if (timerProcessing[0]) {
count++;
txtCount.setText(String.valueOf(count));
}
}
});
btnRestart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
timerProcessing[0] = true;
count = 0;
txtCount.setText(String.valueOf(count));
timer.cancel();
timer.start();
}
});
}
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
textViewTimer.setText("0:000");
timerProcessing[0] = false;
}
@Override
public void onTick(long millisUntilFinished) {
textViewTimer.setText("" + millisUntilFinished / 1000 + ":"
+ millisUntilFinished % 1000);
}
}
这里,您的计数器变量被一个内部类替换,这样您就不必每次都创建计数器变量。如果你想重新启动计数器,只需创建一个计数器变量并调用它的start方法。你知道谷歌搜索引擎吗
谢谢!它明确地显示了我想要的计时器,但是计数器不再上升了。当我再次点击按钮时,计时器也会继续重新启动。有什么想法吗?问题解决了,非常感谢@AndroidCode新手欢迎你。。如果这有助于你接受我的回答。:)很抱歉。这是新的。再次感谢!