我试图建立一个非常简单的游戏,你必须在一分钟内尽可能快地点击弹出的按钮(1是弹出,0是不弹出)(以便在该时间范围内获得最大数量的打击)。我可以通过点击“开始”按钮启动我的应用程序并启动计数器,
但如果由于某种原因我在几秒钟后再次错误地点击“开始”,计数器就会出错,并在第一次倒计时和第二次倒计时之间快速交替。我的代码出了什么问题?此外,当countdowntimer自动变为0(或“完成!”)时,我不知道如何使whacks计数器变为0。你能帮我吗?
非常感谢您的宝贵时间!
这是我的代码:
main activity.Java
package com.example.android.whack_a_lock_022;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
int cl = 0;
int nl = 0;
int sl = 0;
int whacks = 0;
int maxwhacks = 0;
float random0to1;
private CountDownTimer countDownTimer;
private Button start;
private Button cancel;
private TextView time;
Random r;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
r = new Random();
Button clButton = findViewById(R.id.cl);
Button nlButton = findViewById(R.id.nl);
Button slButton = findViewById(R.id.sl);
View.OnClickListener btnClickListener = new View.OnClickListener () {
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.startButton :
start();
break;
case R.id.cancelButton :
cancel();
break;
}
}
};
clButton.setOnClickListener (new View.OnClickListener() {
@Override
public void onClick(View v) {
if (cl == 1) {
cl = 0;
TextView CapsLock = findViewById(R.id.cl);
CapsLock.setText("0");
whacks++;
TextView NumberOfWhacks = findViewById(R.id.numberOfWhacksView);
NumberOfWhacks.setText(String.valueOf(whacks));
if (whacks > maxwhacks) {
maxwhacks = whacks;
TextView NumberOfMaxWhacks = findViewById(R.id.numberOfMaxWhacksView);
NumberOfMaxWhacks.setText(String.valueOf(maxwhacks));
}
pop();
}
}
});
nlButton.setOnClickListener (new View.OnClickListener() {
@Override
public void onClick(View v) {
if (nl == 1) {
nl = 0;
TextView NumLock = findViewById(R.id.nl);
NumLock.setText("0");
whacks++;
TextView NumberOfWhacks = findViewById(R.id.numberOfWhacksView);
NumberOfWhacks.setText(String.valueOf(whacks));
if (whacks > maxwhacks) {
maxwhacks = whacks;
TextView NumberOfMaxWhacks = findViewById(R.id.numberOfMaxWhacksView);
NumberOfMaxWhacks.setText(String.valueOf(maxwhacks));
}
pop();
}
}
});
slButton.setOnClickListener (new View.OnClickListener() {
@Override
public void onClick(View v) {
if (sl == 1) {
sl = 0;
TextView ScrollLock = findViewById(R.id.sl);
ScrollLock.setText("0");
whacks++;
TextView NumberOfWhacks = findViewById(R.id.numberOfWhacksView);
NumberOfWhacks.setText(String.valueOf(whacks));
if (whacks > maxwhacks) {
maxwhacks = whacks;
TextView NumberOfMaxWhacks = findViewById(R.id.numberOfMaxWhacksView);
NumberOfMaxWhacks.setText(String.valueOf(maxwhacks));
}
pop();
}
}
});
// Capture our button from layout
start = (Button) findViewById(R.id.startButton);
start.setOnClickListener(btnClickListener);
cancel = (Button) findViewById(R.id.cancelButton);
cancel.setOnClickListener(btnClickListener);
time = (TextView) findViewById(R.id.time);
// Register the onClick listener with the implementation above
}
public float numAlea() {
return r.nextFloat();
}
private void start () {
time.setText("60");
countDownTimer = new CountDownTimer(60 * 1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
time.setText("" + millisUntilFinished / 1000);
}
@Override
public void onFinish(){
time.setText("Done !");
}
};
countDownTimer.start();
pop();
}
private void pop(){
// Escolhe e "POPa" o botão
// Gera número aleatório
random0to1 = numAlea();
if (random0to1 <= (1F / 3)) {
cl = 1;
Button clButton = (Button) findViewById(R.id.cl);
clButton.setText("1");
} else if (random0to1 <= 2F / 3) {
nl = 1;
Button nlButton = (Button) findViewById(R.id.nl);
nlButton.setText("1");
} else if (random0to1 <= 1F) {
sl = 1;
Button slButton = (Button) findViewById(R.id.sl);
slButton.setText("1");
}
}
private void cancel() {
if (countDownTimer != null) {
countDownTimer.cancel();
countDownTimer = null;
}
//Põe botões a zeros
cl = 0;
Button clButton = (Button) findViewById(R.id.cl);
clButton.setText("0");
nl = 0;
Button nlButton = (Button) findViewById(R.id.nl);
nlButton.setText("0");
sl = 0;
Button slButton = (Button) findViewById(R.id.sl);
slButton.setText("0");
whacks = 0;
TextView NumberOfWhacks = findViewById(R.id.numberOfWhacksView);
NumberOfWhacks.setText("0");
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/whacksView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="WHACKS"
android:textSize="24sp"
android:textAlignment="center"/>
<TextView
android:id="@+id/maxWhacksView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="MAX WHACKS"
android:layout_weight="1"
android:textSize="24sp"/>
</LinearLayout>
<TextView
android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="60"
android:textSize="24sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/numberOfWhacksView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0"
android:textAlignment="center"
android:textSize="24sp"/>
<TextView
android:id="@+id/numberOfMaxWhacksView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="0"
android:layout_weight="1"
android:textSize="24sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/cl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_weight="1"/>
<Button
android:id="@+id/nl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_weight="1"/>
<Button
android:id="@+id/sl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_weight="1"/>
</LinearLayout>
<Button
android:id="@+id/startButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Start" />
<Button
android:id="@+id/cancelButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel" />
</LinearLayout>
如果您只是继续重新构建相同的CountDownTimer并且同时运行多个实例,那么它就是这样做的。在再次启动计时器之前,您需要检查计时器当前是否正在运行。有两种方法可以做到这一点。
public CountDownTimer countDownTimer;
public boolean isTimerRunning = false;
private void start () {
if (!isTimerRunning) {
time.setText("60");
countDownTimer = new CountDownTimer(60 * 1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
time.setText("" + millisUntilFinished / 1000);
}
@Override
public void onFinish() {
isTimerRunning = false;
time.setText("Done !");
}
};
countDownTimer.start();
isTimerRunning = true;
pop();
}
}
public CountDownTimer countDownTimer = null;
private void start () {
if (countDownTimer == null) {
time.setText("60");
countDownTimer = new CountDownTimer(60 * 1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
time.setText("" + millisUntilFinished / 1000);
}
@Override
public void onFinish() {
countDownTimer = null;
time.setText("Done !");
}
};
countDownTimer.start();
pop();
}
}