我该如何修复此代码,以便我的 android textview 使用倒数计时器更新并显示剩余的秒数?
How can I fix this code so my android textview updates with the countdowntimer and shows the seconds that are left?
我正在尝试制作电影倒计时应用程序中的倒计时应用程序。我对编码很陌生。
这里的目标是更新显示剩余秒数的文本视图。
这是我为实现该目标而编写的 java 代码。我知道其他方面如年、日、时、分还没有说完
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
private TextView mTextViewCountDownYears;
private TextView mTextViewCountDownDays;
private TextView mTextViewCountDownHours;
private TextView mTextViewCountDownMinutes;
private TextView mTextViewCountDownSeconds;
private Button mButtonAccept;
private CountDownTimer mCountDownTimer;
private Boolean mTimerRunning;
long timeLeftToLive = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Calendar deathDay = Calendar.getInstance();
deathDay.set(2021,7,24,10,14,45);
Calendar today = Calendar.getInstance();
long diff = deathDay.getTimeInMillis() - today.getTimeInMillis();
timeLeftToLive = diff;
mTextViewCountDownYears = findViewById(R.id.text_view_countdown_years);
mTextViewCountDownDays = findViewById(R.id.text_view_countdown_days);
mTextViewCountDownHours = findViewById(R.id.text_view_countdown_hours);
mTextViewCountDownMinutes = findViewById(R.id.text_view_countdown_minutes);
mTextViewCountDownSeconds = findViewById(R.id.text_view_countdown_seconds);
mButtonAccept = findViewById(R.id.button_accept);
mButtonAccept.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startTimer();
}
});
}
private void startTimer(){
mCountDownTimer = new CountDownTimer(timeLeftToLive, 1000) {
@Override
public void onTick(long millisUntilFinished) {
timeLeftToLive = millisUntilFinished;
updateTextView();
}
@Override
public void onFinish() {
}
};
}
private void updateTextView() {
long secondsLeft = timeLeftToLive / 1000;
String secondsLeftFormatted = String.format("%02d",secondsLeft);
mTextViewCountDownSeconds.setText(secondsLeftFormatted);
}
}
您的代码看起来不错,而且您离它正常工作已经很近了!您只需在 startTimer()
函数内的 CountDownTimer
定义的末尾添加一个 .start()
(在分号之前)。
像这样:
private void startTimer(){
mCountDownTimer = new CountDownTimer(timeLeftToLive, 1000) {
@Override
public void onTick(long millisUntilFinished) {
timeLeftToLive = millisUntilFinished;
updateTextView();
}
@Override
public void onFinish() {
}
}.start();
}
有一个很好的例子供你参考here如果你需要它
我正在尝试制作电影倒计时应用程序中的倒计时应用程序。我对编码很陌生。 这里的目标是更新显示剩余秒数的文本视图。 这是我为实现该目标而编写的 java 代码。我知道其他方面如年、日、时、分还没有说完
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
private TextView mTextViewCountDownYears;
private TextView mTextViewCountDownDays;
private TextView mTextViewCountDownHours;
private TextView mTextViewCountDownMinutes;
private TextView mTextViewCountDownSeconds;
private Button mButtonAccept;
private CountDownTimer mCountDownTimer;
private Boolean mTimerRunning;
long timeLeftToLive = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Calendar deathDay = Calendar.getInstance();
deathDay.set(2021,7,24,10,14,45);
Calendar today = Calendar.getInstance();
long diff = deathDay.getTimeInMillis() - today.getTimeInMillis();
timeLeftToLive = diff;
mTextViewCountDownYears = findViewById(R.id.text_view_countdown_years);
mTextViewCountDownDays = findViewById(R.id.text_view_countdown_days);
mTextViewCountDownHours = findViewById(R.id.text_view_countdown_hours);
mTextViewCountDownMinutes = findViewById(R.id.text_view_countdown_minutes);
mTextViewCountDownSeconds = findViewById(R.id.text_view_countdown_seconds);
mButtonAccept = findViewById(R.id.button_accept);
mButtonAccept.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startTimer();
}
});
}
private void startTimer(){
mCountDownTimer = new CountDownTimer(timeLeftToLive, 1000) {
@Override
public void onTick(long millisUntilFinished) {
timeLeftToLive = millisUntilFinished;
updateTextView();
}
@Override
public void onFinish() {
}
};
}
private void updateTextView() {
long secondsLeft = timeLeftToLive / 1000;
String secondsLeftFormatted = String.format("%02d",secondsLeft);
mTextViewCountDownSeconds.setText(secondsLeftFormatted);
}
}
您的代码看起来不错,而且您离它正常工作已经很近了!您只需在 startTimer()
函数内的 CountDownTimer
定义的末尾添加一个 .start()
(在分号之前)。
像这样:
private void startTimer(){
mCountDownTimer = new CountDownTimer(timeLeftToLive, 1000) {
@Override
public void onTick(long millisUntilFinished) {
timeLeftToLive = millisUntilFinished;
updateTextView();
}
@Override
public void onFinish() {
}
}.start();
}
有一个很好的例子供你参考here如果你需要它