如何从countdowntimer获得价值,并显示到吐司 [英] how to get value from countdowntimer and display into toast
问题描述
我有倒数计时器从1到9999,如果我点击开始按钮计数将启动,但如果点击停止按钮,我需要从倒数获取当前值,并显示在烤面包的价值,但倒数无法阻止如果我点击停止按钮请帮我
i have countdown timer from 1 to 9999 if i click start button the count will start, but if click stop button i need to get current value from countdown and display that value in toast but the countdown could not stop if i click stop button please help me
private CountDownTimer countDownTimer;
private boolean timerHasStarted = false;
private Button startB;
public TextView ;
private final long startTime = 9999 * 1;
private final long interval = 1 *1 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startB = (Button) this.findViewById(R.id.button);
startB.setOnClickListener(this);
text = (TextView) this.findViewById(R.id.timer);
countDownTimer = new MyCountDownTimer(startTime, interval);
text.setText(text.getText() + String.valueOf(startTime / 1));
}
public void onClick(View v) {
if (!timerHasStarted) {
countDownTimer.start();
timerHasStarted = true;
startB.setText("STOP");
} else {
/*countDownTimer.cancel();
timerHasStarted = false;
startB.setText("RESTART");*/
}
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
@Override
public void onFinish() {
//text.setText("Time's up!");
countDownTimer.start();
}
@Override
public void onTick(long millisUntilFinished) {
text.setText("" + millisUntilFinished / 1);
}
}
感谢您
推荐答案
下面是我的倒计时器:
QuestionCountdownTimer 的
QuestionCountdownTimer
public class QuestionCountdownTimer extends CountDownTimer {
private TextView remainingTimeDisplay;
private Context context;
public QuestionCountdownTimer(Context context,long millisInFuture, long countDownInterval,TextView remainingTimeDisplay) {
super(millisInFuture, countDownInterval);
this.context = context;
this.remainingTimeDisplay = remainingTimeDisplay;
}
@Override
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
String hms = String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
remainingTimeDisplay.setText(hms);
}
@Override
public void onFinish() {
Toast.makeText(context,"COUNTDOWN FINISH :)",Toast.LENGTH_SHORT).show();
}
}
注意:
TextView的remainingTimeDisplay
Note: TextView remainingTimeDisplay
remainingTimeDisplay.setText(HMS);
我用它使用一个TextView来显示剩余时间
remainingTimeDisplay.setText(hms); I use it to display the remaining time using a TextView
下面我所说的计时器:
//Start Quiz timer
QuestionCountdownTimer timer = new QuestionCountdownTimer(this,10000, 1000, remainingTimeDisplay);
timer.start();
- 首先参数:此 - 我使用它的上下文显示吐司消息
-first parameter: this - I use it for context to show Toast message
- 第二参数:10000 - 总时间(10秒)
-second parameter: 10000 - total time (10 sec)
- 第三参数:1000 - 倒计时间隔(1秒)
-third parameter: 1000 - countdown interval (1 sec)
-last参数:dispaly剩余时间实时
-last parameter: dispaly remaining time in real time
测试和工作的
Tested and working
这篇关于如何从countdowntimer获得价值,并显示到吐司的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!