我正在努力让倒数计时器从10分钟开始,同样到篮球记分牌:10:00到00:00。我该怎么办?这是我的代码:
private TextView Timer;
Handler handler = new Handler();
private int length = 120000;
private int decision = 0;
MyCount counter;
public String formatTime(long millis) {
String output = "00:00";
long seconds = millis / 1000;
long minutes = seconds / 60;
seconds = seconds % 60;
minutes = minutes % 60;
String sec = String.valueOf(seconds);
String min = String.valueOf(minutes);
if (seconds < 10)
sec = "0" + seconds;
if (minutes < 10)
min= "0" + minutes;
output = min + " : " + sec;
return output;
}//formatTime
@Override
public void onCreate(Bundle cute) {
super.onCreate(cute);
counter = new MyCount(length, 1000);
updateTime();
handler.removeCallbacks(updateTimeTask);
handler.postDelayed(updateTimeTask, 1000);
}//end of cuteness
private Runnable updateTimeTask = new Runnable() {
public void run() {
updateTime();
handler.postDelayed(this, 1000);
}
};
private void updateTime() {
switch (decision) {
case 0:
startTime = 0L;
counter.start();
decision=1;
break;
case 1:
counter.onPause();
decision=0;
break;
}
}//updateTime
class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}//MyCount
public void onPause() {
//do stuff later
onPause();
}//finish
public void onTick(long millisUntilFinished) {
Timer.setText("" + formatTime(millisUntilFinished));
}//on tick
@Override
public void onFinish() {
onStop();
}//finish
}//class MyCount
任何帮助将不胜感激。谢谢!
答案 0 :(得分:6)
这不一定太难。您已经创建了用字母写下时间的功能,现在需要倒数。启动计时器很简单,只需在您的开始按钮事件处理程序(或您选择使用的任何内容)中执行此操作(来自android developer reference的修改示例:
// New timer for 10 minutes, starts after initialization
new MyCount(600000, 1000)
{
// Updates the text on your "scoreboard" every second
public void onTick(long millisUntilFinished)
{
Timer.setText("Time remaining: " + formatTime(millisUntilFinished));
}
public void onFinish()
{
mTextField.setText("done!");
}
}.start();
这就是你所需要的!您可以跳过UpdateTime
功能和updateTimeTask
。只需在onCreate
方法
counter = new MyCount(length, 1000);
updateTime();
handler.removeCallbacks(updateTimeTask);
handler.postDelayed(updateTimeTask, 1000);
用我的代码。或者随意修改它!
答案 1 :(得分:1)
为什么不创建3个文本视图,以便您更容易编码?
一分钟。
一个用于冒号。
一秒钟。
答案 2 :(得分:0)
我想倒计时的方式只是在开头保存(当前时间+10分钟),然后在处理程序中每秒从当前时间减去它,并以所需的格式显示结果。