android倒计时 用到CountDownTimer

Android中文API(143) —— CountDownTimer

前言

  本章内容android.os.CountDownTime章节,版本为Android 4.0 r1,翻译来自:"liliang1222",再次感谢他 !期待你一起参与翻译Android的相关资料,联系我over140@gmail.com。

声明

  欢迎转载,但请保留文章原始出处:)

    博客园:http://www.cnblogs.com/

    Android中文翻译组:http://androidbox.sinaapp.com/

 

CountDownTimer

译者署名: liliang1222

版本:Android 4.0 r1

结构

继承关系

public abstract class CountDownTimer extends Object

java.lang.Object

android.os.CountDownTimer

类概述

定时执行在一段时候后停止的倒计时,在倒计时执行过程中会在固定间隔时间得到通知(译者:触发onTick方法),下面的例子显示在一个文本框中显示一个30s倒计时:

new CountdownTimer(30000, 1000) {

public void onTick(long millisUntilFinished) {

mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);

}

public void onFinish() {

mTextField.setText("done!");

}

}.start();

onTick的调用是同步的,保证这次调用不会在之前调用完成前发生。这里的同步机制主要是用来:onTick的实现需要很多时间执行比倒计时间隔更重要的事情。

构造函数

public CountDownTimer (long millisInFuture, long countDownInterval)

参数

millisInFuture  从开始调用start()到倒计时完成并onFinish()方法被调用的毫秒数。(译者注:倒计时时间,单位毫秒)

countDownInterval  接收onTick(long)回调的间隔时间。(译者注:单位毫秒)

公共方法

public final void cancel ()

取消倒计时(译者:取消后,再次启动会重新开始倒计时)

public abstract void onFinish ()

倒计时完成时被调用

public abstract void onTick (long millisUntilFinished)

固定间隔被调用

参数

millisUntilFinished   倒计时剩余时间。

public synchronized final CountDownTimer start ()

启动倒计时

 

补充

文章精选

Android 定时器

android倒计时功能的实现(CountDownTimer)

示例代码

Java:

package com.test.countdowntimer;

import android.app.Activity;

import android.os.Bundle;

import android.os.CountDownTimer;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

import com.test.R;

public class CountDownTimeActivity extends Activity implements OnClickListener {

TextView mTextView;

Button mButton1;

Button mButton2;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.countdown);

mTextView = (TextView)findViewById(R.id.textView1);

mButton1 = (Button)findViewById(R.id.button1);

mButton2 = (Button)findViewById(R.id.button2);

mButton1.setOnClickListener(this);

mButton2.setOnClickListener(this);

}

CountDownTimer timer = new CountDownTimer(40000,1000) {

@Override

public void onTick(long millisUntilFinished) {

mTextView.setText("seconds remaining: " + millisUntilFinished / 1000);

try {

Thread.sleep(1200);

catch (InterruptedException e) {

e.printStackTrace();

}

Log.e("CountDown",millisUntilFinished+"");

}

@Override

public void onFinish() {

mTextView.setText("done!");

}

};

@Override

public void onClick(View v) {

switch(v.getId()){

case R.id.button1:

timer.start();

break;

case R.id.button2:

timer.cancel();

break;

}

}

}

XML:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

<TextView android:text="TextView" android:id="@+id/textView1"android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>

<Button android:text="开始" android:id="@+id/button1"android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

<Button android:text="取消" android:id="@+id/button2"android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

</LinearLayout>

说明:

CountDownTimer的间隔为1s,我们在onTIck方法里面休眠了1.2s,所以log出来发现打印间隔变成了2s,即中间一次onTick方法没有被执行(不会在之前一次调用完成前被调用)。

源代码下载

参考:http://www.cnblogs.com/over140/archive/2011/12/20/2294220.html

android倒计时(整理)的更多相关文章

  1. Android进程整理

    一.概括 系统启动架构图: 上图在Android系统-开篇中有讲解,是从Android系统启动的角度来分析,本文是从进程/线程的视角来分析该问题. 1.1 父进程 在所有进程中,以父进程的姿态存在的进 ...

  2. Android倒计时:计算两个时间将得到的时间差转化为倒计时(xx时xx分xx秒格式)

    首先是一个自定义控件: public class RushBuyCountDownTimerView extends LinearLayout { // 小时,十位 private TextView ...

  3. android框架整理

    http://blog.csdn.net/ma969070578/article/details/27808043 闲来无事.想搭个框架试试 分析一般应用 将资料整理整理 粗略统计 需要以下资料 1. ...

  4. Android -- 倒计时的实现

    CountDownTimer                                                                      CountDownTimer这个 ...

  5. Android基础整理之四大组件Activity

    最近准备系统的重新整理复习一下Android的各方面的知识,本着知识分享的原则,我就把梳理过程中一些东西给记录下来,权当一个学习笔记吧. 下面步入正题..... 什么是Activity Activit ...

  6. Android倒计时Button

    最近做用户绑定,需要用到倒计时的一个Button,就花点时间封装了一个,非常简单,效果图如下: 1.TimeButton 自定义倒计时Button package com.example.timebu ...

  7. Android倒计时实现

    Android为我们封装好了一个抽象类CountDownTimer,可以实现计时器功能: /** * 倒数计时器 */ private CountDownTimer timer = new Count ...

  8. Android 倒计时按钮,倒计时发送短信验证码…

    Android基础之——CountDownTimer类,轻松实现倒计时功能https://www.cnblogs.com/yfceshi/p/6853746.html android中获取验证码后出现 ...

  9. Android倒计时功能的实现

    Android中的倒计时的功能(也能够直接使用CountDownTimer这个类直接实现,相关此Demo可查看我的博客).參考了网上写的非常好的一个倒计时Demo: watermark/2/text/ ...

随机推荐

  1. leetcode Add and Search Word - Data structure design

    我要在这里装个逼啦 class WordDictionary(object): def __init__(self): """ initialize your data ...

  2. java web 学习 --第七天(Java三级考试)

    第六天的学习内容如下:http://www.cnblogs.com/tobecrazy/p/3462244.html application application对象的方法与应用: ①   setA ...

  3. java sleep() 、yield()

    1.sleep() 使当前线程(即调用该方法的线程)暂停执行一段时间,让其他线程有机会继续执行,但它并不释放对象锁.也就是说如果有synchronized同步快,其他线程仍然不能访问共享数据.注意该方 ...

  4. code vs 1026 逃跑的拉尔夫

    1026 逃跑的拉尔夫  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 年轻的拉尔夫开玩笑地从一个小镇上偷走 ...

  5. 【leetcode】Spiral Matrix(middle)

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  6. python getopt.getopt 不能精确匹配的问题

    代码:opts,argv = getopt.getopt(sys.argv[1:],('u:'),['ad','join','passwd=','domain=','dip=','test','ip= ...

  7. IOS - Passbook

    1. 什么是Passbook Passbook是苹果公司于北京时间2012年6月12日上午,在全球开发者大会(WWDC)上宣布了iOS 6系统将提供操作一个全新的应用——Passbook 这是一款可以 ...

  8. YAML-初识

    YAML简介 YAML-what? YAML Ain't Markup Language 和GNU一样,YAML是一个递归着说"不"的名字.不同的是,GNU对UNIX说不,YAML ...

  9. mongoose学习笔记1--基础知识1

    今天我们将学习Mongoose,什么是Mongoose呢,它于MongoDB又是什么关系呢,它可以用来做什么呢? MongoDB是一个开源的NoSQL数据库,相比MySQL那样的关系型数据库,它更显得 ...

  10. RedHat下安装MySQL

    下载mysql 解压tar -xvf mysql-5.7.16-1.el6.x86_64.rpm-bundle.tar 安装MySQL-server包 rpm -ivh mysql-community ...