从倒计时获取变量,并将其设置到TextView。

huangapple go评论89阅读模式
英文:

Get variable from countdown and set it to TextView

问题

这是我的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));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }

    public void resetTimer() {

        // 这是我想设置文本的地方。
        timerTv.setText("尝试从倒计时获取文本!!");

        startBtn.setText("开始!");
        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("停止!");

            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);

    }
}

从倒计时获取变量,并将其设置到TextView。

英文:

I need your help to get a variable from the countdown and set that value to text view. Suppose if countdown stopped at 0:40 sec and I want to put that number to text view.

So I'm using Seekbar to update the time with progress and a textview. And suppose I stopped at a certain number, next time I start again, let the number start from when it stopped. I have uploaded the image of output. Thanks

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.

This is my MainActivity.java file

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(&quot;Seekbar changes&quot;, 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(&quot;Trying to get text from countdown!!&quot;);
startBtn.setText(&quot;START!&quot;);
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(&quot;STOP!&quot;);
Log.i(&quot;Button Clicked&quot;, &quot;Clicked&quot;);
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(&quot;Timer Finished&quot;, &quot;Done!!&quot;);
resetTimer();
}
}.start();
}
}
public void updateTimer(int secondLefts){
int minutes = secondLefts / 60;
int seconds = secondLefts - (minutes * 60);
String secondString = Integer.toString(seconds);
if(seconds &lt;= 9) {
secondString = &quot;0&quot; + seconds;
}
timerTv.setText(minutes + &quot; : &quot; + secondString );
}
}

从倒计时获取变量,并将其设置到TextView。

答案1

得分: 1

我认为你需要暂停计时器。
首先在你的活动中创建一个全局的 long 变量;

long timerProgress;

将你的 restProgress() 方法修改如下,或者你可以添加一个新的方法 pauseTimer()

public void restTimer(){
        // 这里是我想要设置文本的地方。
        
        updateTimer((int) timerProgress/1000);
        startBtn.setText("开始!");
        timerSeekBar.setEnabled(true);
        countDownTimer.cancel();
        timerSeekBar.setProgress((int) timerProgress/1000);
        counterisActive = false;
}

现在在你的 onTick 重写方法中添加这一行。

@Override
public void onTick(long millisUntilFinished) {
         updateTimer((int) millisUntilFinished / 1000);
         // 添加这一行来存储进度计时器。
         timerProgress = millisUntilFinished;
}
  • 你可以添加另一个按钮,一个用于暂停,另一个用于重置计时器。
英文:

I think you need to pause the timer.
First create a global long variable in your activity;

long timerProgress;

Change your restProgress() method like below, or you can add new method pauseTimer().

public void restTimer(){
//This is where I want to set the text.
updateTimer((int) timerProgress/1000);
startBtn.setText(&quot;START!&quot;);
timerSeekBar.setEnabled(true);
countDownTimer.cancel();
timerSeekBar.setProgress((int) timerProgress/1000);
counterisActive = false;
}

Know add this line to your override method onTick.

@Override
public void onTick(long millisUntilFinished) {
updateTimer((int) millisUntilFinished / 1000);
// add this line for store progress timer.
timerProgress = millisUntilFinished;
}
  • You can add another button one for pause and other for rest Timer.

答案2

得分: 0

尝试将 timerTv.setText(minutes + " : " + secondString ); 替换为

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

如果你在线程中间尝试更新UI,它会等待当前线程完成后再更新文本。

英文:

Try replacing timerTv.setText(minutes + &quot; : &quot; + secondString ); with

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

If you try to update the UI mid-thread, it will wait until the current thread has finished before updating the text.

huangapple
  • 本文由 发表于 2020年4月3日 21:08:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/61012640.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定