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

问题描述 投票:0回答:2

我需要您的帮助才能从倒数计时中获取变量并将该值设置为文本视图。假设倒计时在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 );

    }

}

enter image description here

java android android-studio countdown countdowntimer
2个回答
0
投票

[尝试用[]替换timerTv.setText(minutes + " : " + secondString );

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

如果您尝试更新UI中线,它将等待直到当前线程完成才更新文本。


0
投票

我认为您需要暂停计时器。首先在您的活动中创建一个全局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;
     }
  • 您可以添加另一个按钮,一个用于暂停,另一个按钮用于休息定时器。
© www.soinside.com 2019 - 2024. All rights reserved.