英文:
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);
}
}
英文:
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("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 );
}
}
答案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("START!");
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 + " : " + secondString );
with
runOnUiThread(new Runnable() {
@Override
public void run() {
timerTv.setText(minutes + " : " + secondString );
}
});
If you try to update the UI mid-thread, it will wait until the current thread has finished before updating the text.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论