我试图让它每次按下按钮时我的计时器增加5秒。我已经知道我需要取消之前的计时器并创建一个新计时器,以便按照我想要的方式工作。当我按下按钮一次,计时器增加5秒,一切正常,因为它应该。当我多次按下按钮时出现问题。计时器将在许多不同的计时器之间闪烁,而不是停留在最新的计时器上。每按一次按钮,另一个计时器就会在显示屏上闪烁。它几乎就像程序没有取消以前的计时器,只是每次创建一个新的。我真的很感激这方面的一些帮助。谢谢你们!
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int scoreTeamA = 0;
String countDownTimer;
long millisUntilFinishedInt = 5000;
long milliseconds;
long seconds;
long totalAddedTime = 0;
TextView text1;
MyCount counter = new MyCount(millisUntilFinishedInt + totalAddedTime,17);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text1=(TextView)findViewById(R.id.team_a_score);
counter.start();
}
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
millisUntilFinishedInt = millisUntilFinished;
seconds = millisUntilFinishedInt/1000;
milliseconds = millisUntilFinishedInt-(millisUntilFinishedInt/1000)*1000;
countDownTimer = "TIME: " + seconds + "." + milliseconds ;
text1.setText(countDownTimer);
}
@Override
public void onFinish() {
countDownTimer = "TIME'S UP!";
text1.setText(countDownTimer);
}
}
public void timerCreation (){
counter.cancel();
MyCount counter = new MyCount(millisUntilFinishedInt + 5000,1);
counter.start();
}
//method that is called when button is pressed
public void threePoints (View v) {
timerCreation();
}
}
答案 0 :(得分:1)
更改此
public void timerCreation (){
counter.cancel();
MyCount counter = new MyCount(millisUntilFinishedInt + 5000,1);
counter.start();
}
进入这个
public void timerCreation (){
counter.cancel();
counter = new MyCount(millisUntilFinishedInt + 5000,1);
counter.start();
}
使用当前的实现,您将取消成员变量counter
。然后创建一个具有相同名称的局部变量并启动该变量。下次按下按钮时,成员变量counter
将再次被取消(已取消),以便创建新的MyCount
对象并启动该对象。这就是为什么你最终会有多个计时器
答案 1 :(得分:0)
更改:将内部增加到 50 ,但必须增加。
MyCount counter = new MyCount(millisUntilFinishedInt + totalAddedTime, 50);
将文本视图重力与左android:gravity="left"
对齐,以便用户无法感受到它的新音色。
经过测试的工作演示
<强> MainActivity 强>
public class MainActivity extends AppCompatActivity {
Context context;
int scoreTeamA = 0;
String countDownTimer;
long millisUntilFinishedInt = 5000;
long milliseconds;
long seconds;
long totalAddedTime = 0;
TextView text1;
MyCount counter = new MyCount(millisUntilFinishedInt + totalAddedTime, 50);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
text1 = (TextView) findViewById(R.id.foodName);
text1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timerCreation();
}
});
}
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
millisUntilFinishedInt = millisUntilFinished;
seconds = millisUntilFinishedInt / 1000;
milliseconds = millisUntilFinishedInt - (millisUntilFinishedInt / 1000) * 1000;
countDownTimer = "TIME: " + seconds + "." + milliseconds;
text1.setText(countDownTimer);
}
@Override
public void onFinish() {
countDownTimer = "TIME'S UP!";
text1.setText(countDownTimer);
}
}
public void timerCreation() {
counter.cancel();
MyCount counter = new MyCount(millisUntilFinishedInt + 5000, 1);
counter.start();
}
//method that is called when button is pressed
public void threePoints(View v) {
timerCreation();
}
}
<强> activity_main.xml中强>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/base"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:background="@color/white"
android:orientation="horizontal"
android:weightSum="10">
<TextView
android:id="@+id/foodName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:hint="Food name"
android:gravity="left"
android:inputType="textCapWords"
android:textColor="@color/colorPrimaryDark"
android:textColorHint="@color/colorPrimaryDark"
android:textSize="32sp"
android:layout_marginLeft="20dp" />
</RelativeLayout>