Android Studio 中的计时器使用 CountDownTimer

Timers in Android Studio using CountDownTimer

看到我正在尝试获取用户输入的时间并使用用户输入尝试 运行 Android studio 4.2.2

中的 CountDownTimer 函数

问题是:-

案例 1

当我传递变量名称代替 countDown 计时器函数的 millisInFuture 属性并尝试相应地设置 textview 时,TextView 没有设置任何东西.

public void timer() {
        CountDownTimer countDownTimer = new CountDownTimer(**timeValueIntent**, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                //  mediaPlayer.setLooping(true);
                //mediaPlayer.start();
                timerValueTextView.setText(String.valueOf(millisUntilFinished / 1000) + "s");
            }

            @Override
            public void onFinish() {
                Toast.makeText(getApplicationContext(), "Timed out", Toast.LENGTH_SHORT).show();
                restartButton.setVisibility(View.VISIBLE);
                restartButton.setEnabled(true);
                quitButton.setVisibility(View.VISIBLE);
                setBtnCond(true);
                //mediaPlayer.stop();
            }
        }.start();
    }`

案例 2

但是当我在 CountDownTimer 的 millisInFuture 属性中传递 1000 等 int 值并设置 textView 时,它设置成功。

public void timer() {
        CountDownTimer countDownTimer = new CountDownTimer(**300000**, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                //  mediaPlayer.setLooping(true);
                //mediaPlayer.start();
                timerValueTextView.setText(String.valueOf(millisUntilFinished / 1000) + "s");
            }

            @Override
            public void onFinish() {
                Toast.makeText(getApplicationContext(), "Timed out", Toast.LENGTH_SHORT).show();
                restartButton.setVisibility(View.VISIBLE);
                restartButton.setEnabled(true);
                quitButton.setVisibility(View.VISIBLE);
                setBtnCond(true);
                //mediaPlayer.stop();
            }
        }.start();
    }`

任何人都可以 请帮助我在 CountDownTimer 函数的 MillisInFuture 属性中传递变量名称而不是硬编码整数值。!! ? `

我有一些与您的问题类似的源代码,只是实现了您的项目。

初始化变量

private static final long START_TIME_IN_MILLIS = 600000;
private TextView mTextViewCountDown;
private Button mButtonStartPause;
private Button mButtonReset;
private CountDownTimer mCountDownTimer;
private boolean mTimerRunning;
private long mTimeLeftInMillis = START_TIME_IN_MILLIS;

onCreate

//Init
    mTextViewCountDown = findViewById(R.id.text_view_countdown);
    mButtonStartPause = findViewById(R.id.button_start_pause);
    mButtonReset = findViewById(R.id.button_reset);
    mButtonStartPause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mTimerRunning) {
                pauseTimer();       //pauseFunction
            } else {
                startTimer();       //StartFunction
            }
        }
    });



mButtonReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resetTimer();       // resetFunction
            }
        });
        updateCountDownText();      //updateEverySecond

startTimer()

mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
        @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);

pauseTimer()

    mCountDownTimer.cancel();
    mTimerRunning = false;
    mButtonStartPause.setText("Start");
    mButtonReset.setVisibility(View.VISIBLE);

resetTimer()

    mTimeLeftInMillis = START_TIME_IN_MILLIS;
    updateCountDownText();
    mButtonReset.setVisibility(View.INVISIBLE);
    mButtonStartPause.setVisibility(View.VISIBLE);

updateCountDownText()

    int minutes = (int) (mTimeLeftInMillis / 1000) / 60;
    int seconds = (int) (mTimeLeftInMillis / 1000) % 60;
    String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
    mTextViewCountDown.setText(timeLeftFormatted);

最后 XML

<TextView
    android:id="@+id/text_view_countdown"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="00:00"
    android:textColor="@android:color/black"
    android:textSize="60sp" />
<Button
    android:id="@+id/button_start_pause"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/text_view_countdown"
    android:layout_centerHorizontal="true"
    android:text="start" />
<Button
    android:id="@+id/button_reset"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/text_view_countdown"
    android:layout_marginStart="11dp"
    android:layout_toEndOf="@+id/button_start_pause"
    android:text="reset"
    android:visibility="invisible"
    tools:visibility="visible" />