Android -- 倒计时的实现
CountDownTimer
CountDownTimer这个类,实现了倒计时的功能。将后台线程的创建和Handler队列封装成一个方便的类调用。
这个类比较简单,只有四个方法:onTick,onFinsh、cancel和start。其中前面两个是抽象方法,所以要重写一下。
下面是官方给的一个小例子:
new CountdownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
Code
package com.yydcdut.daojishi; import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity {
private MyCount mc;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);tv = (TextView)findViewById(R.id.show);
mc = new MyCount(30000, 1000);
mc.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;
} /*定义一个倒计时的内部类*/
class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
tv.setText("finish");
}
@Override
public void onTick(long millisUntilFinished) {
tv.setText("请等待30秒(" + millisUntilFinished / 1000 + ")...");
Toast.makeText(MainActivity.this, millisUntilFinished / 1000 + "", Toast.LENGTH_LONG).show();//toast有显示时间延迟
}
} }
主要是重写onTick和onFinsh这两个方法,onFinish()中的代码是计时器结束的时候要做的事情;onTick(Long m)中的代码是你倒计时开始时要做的事情,参数m是直到完成的时间,构造方法MyCount()中的两个参数中,前者是倒计的时间数,后者是倒计每秒中间的间隔时间,都是以毫秒为单位。例如要倒计时30秒,每秒中间间隔时间是1秒,两个参数可以这样写MyCount(30000,1000)。 将后台线程的创建和Handler队列封装成为了一个方便的类调用。
当你想取消的时候使用mc.cancel()方法就行了。
我是天王盖地虎的分割线

源代码:http://pan.baidu.com/s/1dD1Qx01
倒计时.zip
Android -- 倒计时的实现的更多相关文章
- android倒计时(整理)
android倒计时 用到CountDownTimer Android中文API(143) —— CountDownTimer 前言 本章内容android.os.CountDownTime章节,版本 ...
- Android倒计时:计算两个时间将得到的时间差转化为倒计时(xx时xx分xx秒格式)
首先是一个自定义控件: public class RushBuyCountDownTimerView extends LinearLayout { // 小时,十位 private TextView ...
- Android倒计时Button
最近做用户绑定,需要用到倒计时的一个Button,就花点时间封装了一个,非常简单,效果图如下: 1.TimeButton 自定义倒计时Button package com.example.timebu ...
- Android倒计时实现
Android为我们封装好了一个抽象类CountDownTimer,可以实现计时器功能: /** * 倒数计时器 */ private CountDownTimer timer = new Count ...
- Android 倒计时按钮,倒计时发送短信验证码…
Android基础之——CountDownTimer类,轻松实现倒计时功能https://www.cnblogs.com/yfceshi/p/6853746.html android中获取验证码后出现 ...
- Android倒计时功能的实现
Android中的倒计时的功能(也能够直接使用CountDownTimer这个类直接实现,相关此Demo可查看我的博客).參考了网上写的非常好的一个倒计时Demo: watermark/2/text/ ...
- Java/Android倒计时(开始,暂停,恢复,停止)
由于要做暂停和恢复,这里我就没有使用Android的CountDownTimer,而是用了Java的Timer.所以,这个方法在java肯定是通用.我也外加了Android独有的Service,有些计 ...
- Android倒计时CountDownTimer小记
Android 超简便的倒计时实现: CountDownTimer CountDownTimer由系统提供 查资料的时候 发现了CountDownTimer这个类之后 果断抛弃了曾经的倒计时做法 功 ...
- Android倒计时案例展示
1. Handler 与Message方法实现倒计时功能 关于Handler与Message消息机制的原理可查看:Android--Handler使用应运及消息机制处理原理分析 这个设计思路也是最经常 ...
随机推荐
- 获取Spring的上下文环境ApplicationContext的方式
摘自: http://blog.csdn.net/yang123111/article/details/32099329 获取Spring的上下文环境ApplicationContext的方式 Web ...
- Can Live View boot up images acquired from 64bit OS evidence?
Some said Live View could only boot up images acquired from 32bit OS evidence. I have to say that it ...
- Android WebRTC 音视频开发总结(三)-- 信令服务和媒体服务
前面介绍了WebRTCDemo的基本结构,本节主要介绍WebRTC音视频服务端的处理,,转载请说明出处(博客园RTC.Blacker). 通过前面的例子我们知道运行WebRTCDemo即可看到P2P的 ...
- .NET中使用log4net
一,加载log4net引用 下载log4net.dll,我们这里使用的是.NET2.0 下载地址:http://files.cnblogs.com/gosky/log4net-1.2.13-bin-n ...
- s3c6410_时钟初始化
参考: 1)<USER'S MANUAL-S3C6410X>第三章 SYSTEM CONTROLLER 2)u-boot/board/samsumg/smdk6410/lowlevel_i ...
- SQL SERVER连接字符串学习
在使用connection string时遇到一些问题 字符串如下"Data Source= ******;Initial Catalog=******;Persist Security I ...
- CSS计数器与动态计数呈现
代码: CSS代码: body { counter-reset: icecream; } input:checked { counter-increment: icecream; } .total:: ...
- FaceBook微光闪烁---第三方开源--shimmer-android
Android上的微光闪烁shimmer效果,实现的手段不少,其中比较好的是facebook做的开源库:shimmer-android,其在github上的项目主页是:https://github.c ...
- 【推介】GitHub
隆重推介:GitHub(https://github.com/) 作为开源代码库以及版本控制系统,Github拥有140多万开发者用户. 随着越来越多的应用程序转移到了云上,Github已经成为了管理 ...
- List GetEnumerator
static void Main() { List<int> list = new List<int>(); list.Add(); list.Add(); list.Add( ...