#java #android #textview #countdowntimer #numberpicker
Вопрос:
Обратный отсчет и просмотр текста — неверное отображение времени. Привет! Когда вы нажмете кнопку «Пуск», таймер вернется. Но когда вы нажимаете ту же кнопку для паузы, это указатель на скриншоте, который неправильно отображает секунды. Скачки с 59 на 09, с 58 на 08 и так далее. Помогите исправить.
public class MainActivity extends AppCompatActivity implements NumberPicker.OnValueChangeListener {
private TextView textView1;
private Button mButtonStartPause;
private Button mButtonReset;
private CountDownTimer mCountDownTimer;
private boolean mTimerRunning;
private long mStarTimeInMillis;
private long mTimeLeftInMillis;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// NumberPicker
Button buttonSetTime = findViewById(R.id.buttonSetTime);// on click of button display the dialog
buttonSetTime.setOnClickListener(v -> show());
textView1 = findViewById(R.id.textView1);
mButtonStartPause = findViewById(R.id.buttonStart);
mButtonReset = findViewById(R.id.buttonStop);
mButtonStartPause.setOnClickListener(view -> {
setTime();
if(mTimerRunning)
{
pauseTimer();
}
else {
startTimer();
}
});
mButtonReset.setOnClickListener(view -> {
resetTimer();
});
updateCountDownText();
}
@SuppressLint("SetTextI18n")
private void setTime()
{
String input = textView1.getText().toString();
String str1 = input.substring(0, 2);
String str2 = input.substring(4, 5);
long number1 = Long.parseLong(str1);
long number2 = Long.parseLong(str2);
long sumMillisMin = number1 *60000;
long sumMillisSec = number2 *1000;
long millisInput = sumMillisMin sumMillisSec;
mStarTimeInMillis = millisInput;
mTimeLeftInMillis = mStarTimeInMillis;
textView.setText(Long.toString(mTimeLeftInMillis));
}
@SuppressLint("SetTextI18n")
private void updateCountDownText()
{
long minutes = (mTimeLeftInMillis / 1000) / 60;
long seconds = (mTimeLeftInMillis / 1000) % 60;
String timeLeftFormatted = String.format(Locale.getDefault(),"d:d", minutes, seconds);
textView1.setText(timeLeftFormatted);
}
private void startTimer()
{
mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 500) {
@Override
public void onTick(long millisUntilFinished)
{
mTimeLeftInMillis = millisUntilFinished;
updateCountDownText();
}
@Override
public void onFinish()
{
mTimerRunning = false;
mButtonStartPause.setText("Start");
mButtonStartPause.setVisibility(View.INVISIBLE);
mButtonReset.setVisibility(View.VISIBLE);
}
}.start();
mTimerRunning = true;
mButtonStartPause.setText("Pause");
mButtonReset.setVisibility(View.INVISIBLE);
}
private void pauseTimer()
{
mCountDownTimer.cancel();
mTimerRunning = false;
mButtonStartPause.setText("Start");
mButtonReset.setVisibility(View.VISIBLE);
}
private void resetTimer()
{
mTimeLeftInMillis = mStarTimeInMillis;
updateCountDownText();
mButtonReset.setVisibility(View.INVISIBLE);
mButtonStartPause.setVisibility(View.VISIBLE);
}
Ответ №1:
Это работает на меня
переменная и onCreate:
private static final long START_TIME_IN_MILLIS = 60000;
private CountDownTimer mCountDownTimer;
private boolean mTimerRunning = false;
private long mTimeLeftInMillis = START_TIME_IN_MILLIS;
MaterialButton timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timer = findViewById(R.id.counter);
timer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ResetTimer();
StartTimer();
}
});
StartTimer();
}
Время начала():
private void StartTimer() {
mCountDownTimer = null;
mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
@Override
public void onTick(long millisUntilFinished) {
mTimeLeftInMillis = millisUntilFinished;
UpdateCountDownText();
}
@Override
public void onFinish() {
timer.setEnabled(true);
timer.setText("start");
mTimerRunning = true;
}
}.start();
}
UpdateCountDownText():
private void UpdateCountDownText() {
int min = (int) ((mTimeLeftInMillis / 1000) / 60);
int sec = (int) ((mTimeLeftInMillis / 1000) % 60);
String timeleftformated = String.format(Locale.getDefault(), "d:d", min, sec);
timer.setText(timeleftformated);
}
Таймер сброса:
private void ResetTimer() {
mCountDownTimer.cancel();
mTimeLeftInMillis = START_TIME_IN_MILLIS;
UpdateCountDownText();
}