安卓:倒计时例如10:00至00:00?使用OnclickListener到TextView的? [英] Android: Count Down Timer e.g. 10:00 to 00:00? using OnclickListener to TextView?

查看:154
本文介绍了安卓:倒计时例如10:00至00:00?使用OnclickListener到TextView的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个倒数计时器开始10分钟,similair到篮球记分牌:10:00至00:00。我将如何做呢?这是我的code:

I'm trying to make a countdown timer starting at 10 minutes, similair to a basketball scoreboard: 10:00 to 00:00. How would I do that? This is my code:

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  

任何帮助将是AP preciated。谢谢!

Any help would be appreciated. thanks!

推荐答案

这不必太用力。你已经创建了功能写你在信的时间,现在你需要倒计时。启动一个定时器很容易,只要做到这一点在你的启动按钮的事件处理程序(或者任何你选择使用)(变形例从的 Android开发者参考

This does not have to be too hard. You've already created your functionality for writing your time in letters, now you need to count down. Starting a timer is easy, just do this in your start button event handler (or whatever you choose to use) (modified example from the 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();

这就是你所需要的!您可以跳过你的录入功能,你的 updateTimeTask 。只需更换这一切都在你的的onCreate 方法

And that's all you need! You can skip your UpdateTime function and your updateTimeTask. Just replace all this on your onCreate method

counter = new MyCount(length, 1000);  
updateTime();  
handler.removeCallbacks(updateTimeTask);  
handler.postDelayed(updateTimeTask, 1000); 

使用我的code。或修改它,你请!

With my code. Or modify it as you please!

这篇关于安卓:倒计时例如10:00至00:00?使用OnclickListener到TextView的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆