从倒计时中获取变量并将其设置为 TextView

Get variable from countdown and set it to TextView

我需要你的帮助来从倒计时中获取一个变量并将该值设置为文本视图。假设如果倒计时停止在 0:40 秒,我想将该数字放入文本视图。

所以我正在使用 Seekbar 来更新带有进度和文本视图的时间。假设我停在某个数字,下次我重新开始时,让数字从停止时开始。我已经上传了输出图像。谢谢

I learned to create this app from udemy online tutorial. Its called Complete android developer course- Build 23 Apps!!. Its lesson 38- App Egg timer.

这是我的 MainActivity.java 文件

public class MainActivity extends AppCompatActivity {
    TextView timerTv;
    SeekBar timerSeekBar;
    Button startBtn;

    CountDownTimer countDownTimer;

    boolean counterisActive = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        timerTv = (TextView) findViewById(R.id.countdowntimertextview);
        timerSeekBar = (SeekBar) findViewById(R.id.timerSeekBar);

        startBtn = (Button) findViewById(R.id.startBtn);

        timerSeekBar.setMax(300);
        timerSeekBar.setProgress(20);

        timerSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                updateTimer(progress);

                Log.i("Seekbar changes", String.valueOf(progress), null);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }

    public void resetTimer(){


        //This is where I want to set the text.
        timerTv.setText("Trying to get text from countdown!!");

        startBtn.setText("START!");
        timerSeekBar.setEnabled(true);
        countDownTimer.cancel();
        timerSeekBar.setProgress(20);
        counterisActive = false;

    }

    public void buttonClicked(View view){

        if(counterisActive){
            resetTimer();

        }else {
            counterisActive =  true;
            timerSeekBar.setEnabled(false);
            startBtn.setText("STOP!");

            Log.i("Button Clicked", "Clicked");
             countDownTimer = new CountDownTimer(timerSeekBar.getProgress() * 1000 + 100, 1000) {


                @Override
                public void onTick(long millisUntilFinished) {
                    updateTimer((int) millisUntilFinished / 1000);
                }

                @Override
                public void onFinish() {
                    MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.templebell);
                    mediaPlayer.start();
                    Log.i("Timer Finished", "Done!!");
                    resetTimer();
                }
            }.start();
        }
    }

    public void updateTimer(int secondLefts){
        int minutes = secondLefts / 60;
        int seconds = secondLefts - (minutes * 60);

        String secondString = Integer.toString(seconds);

        if(seconds <= 9) {
            secondString = "0" + seconds;
        }

        timerTv.setText(minutes + " : " + secondString );

    }

}

尝试用

替换 timerTv.setText(minutes + " : " + secondString );
runOnUiThread(new Runnable() {
    @Override
    public void run() {
        timerTv.setText(minutes + " : " + secondString );
    }
});

如果您尝试更新 UI 线程中,它会等到当前线程完成后再更新文本。

我认为你需要暂停计时器。 首先在 activity;

中创建一个全局 long 变量
long timerProgress;

如下更改您的 restProgress() 方法,或者您可以添加新方法 pauseTimer()。

public void restTimer(){
        //This is where I want to set the text.

        updateTimer((int) timerProgress/1000);
        startBtn.setText("START!");
        timerSeekBar.setEnabled(true);
        countDownTimer.cancel();
        timerSeekBar.setProgress((int) timerProgress/1000);
        counterisActive = false;
    }

知道将此行添加到您的覆盖方法 onTick。

@Override
public void onTick(long millisUntilFinished) {
         updateTimer((int) millisUntilFinished / 1000);
         // add this line for store progress timer.
         timerProgress = millisUntilFinished;
     }
  • 您可以添加另一个按钮,一个用于暂停,另一个用于休息计时器。