首页 > 代码库 > 使用CounterDownTimer实现时分秒倒计时

使用CounterDownTimer实现时分秒倒计时

   使用android提供的android.os包中的 counterDownTimer可以很方便的实现倒计时功能,具体如下:

   

import android.os.CountDownTimer;public class Couterdown extends CountDownTimer {    public Couterdown(long millisInFuture, long countDownInterval) {        super(millisInFuture, countDownInterval);        // TODO Auto-generated constructor stub    }    @Override    public void onFinish() {        // TODO Auto-generated method stub            }    @Override    public void onTick(long arg0) {                    }    public String  toClock(long millisUntilFinished)        {        long hour = millisUntilFinished/(60*60*1000);        long minute = (millisUntilFinished - hour*60*60*1000)/(60*1000);        long second = (millisUntilFinished - hour*60*60*1000   - minute*60*1000)/1000;        if(second >= 60 )        {          second = second % 60;          minute+=second/60;        }        if(minute >= 60)        {          minute = minute %60;          hour += minute/60;        }        String sh = "";        String sm ="";        String ss = "";        if(hour <10)        {           sh = "0" + String.valueOf(hour);        }else        {           sh = String.valueOf(hour);        }        if(minute <10)        {           sm = "0" + String.valueOf(minute);        }else        {           sm = String.valueOf(minute);        }        if(second <10)        {           ss = "0" + String.valueOf(second);        }else        {           ss = String.valueOf(second);        }        return sh +":"+ sm+":" + ss;    }}

 

首先新建一个CounterDown类继承counterDownTimer,然后在activity中使用,并将结果显示在textView上

public class MainActivity extends Activity {    TextView tv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        tv=(TextView)findViewById(R.id.textView1);        new Couterdown(30000,1000){            @Override            public void onTick(long millisUntilFinished) {                tv.setText(toClock(millisUntilFinished));                            }            @Override            public String toClock(long millis) {                return super.toClock(millis);            }                    }.start();    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }}

最终结果如图:

之前因为没解决8小时误差的问题,所以用的上面的方法,其实可以像下面这样做

ublic class MainActivity extends Activity {    TextView tv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        tv=(TextView)findViewById(R.id.textView1);        new CountDownTimer(70000,1000){            @Override            public void onTick(long millisUntilFinished) {                SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");//初始化Formatter的转换格式。                  formatter.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));                String hms = formatter.format(millisUntilFinished);                  tv.setText(hms);                            }            @Override            public void onFinish() {                // TODO Auto-generated method stub                            }                }.start();    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }}

 

不过最后都会有个小问题,就是最后一秒不会再减少,应该是counter类的参数问题,哪位大虾看到了给小弟说说解决的办法吧~~~

 

使用CounterDownTimer实现时分秒倒计时