android倒计时(整理)
android倒计时 用到CountDownTimer
Android中文API(143) —— CountDownTimer
前言
本章内容android.os.CountDownTime章节,版本为Android 4.0 r1,翻译来自:"liliang1222",再次感谢他 !期待你一起参与翻译Android的相关资料,联系我over140@gmail.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倒计时功能的实现(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倒计时(整理)的更多相关文章
- Android进程整理
一.概括 系统启动架构图: 上图在Android系统-开篇中有讲解,是从Android系统启动的角度来分析,本文是从进程/线程的视角来分析该问题. 1.1 父进程 在所有进程中,以父进程的姿态存在的进 ...
- Android倒计时:计算两个时间将得到的时间差转化为倒计时(xx时xx分xx秒格式)
首先是一个自定义控件: public class RushBuyCountDownTimerView extends LinearLayout { // 小时,十位 private TextView ...
- android框架整理
http://blog.csdn.net/ma969070578/article/details/27808043 闲来无事.想搭个框架试试 分析一般应用 将资料整理整理 粗略统计 需要以下资料 1. ...
- Android -- 倒计时的实现
CountDownTimer CountDownTimer这个 ...
- Android基础整理之四大组件Activity
最近准备系统的重新整理复习一下Android的各方面的知识,本着知识分享的原则,我就把梳理过程中一些东西给记录下来,权当一个学习笔记吧. 下面步入正题..... 什么是Activity Activit ...
- 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/ ...
随机推荐
- REACT 学习
1.React/React Native 的ES5 ES6写法对照表 http://bbs.reactnative.cn/topic/15/react-react-native-%E7%9A%84es ...
- Java for LeetCode 225 Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- xdebug安装
sudo apt-get install php-pearsudo apt-get install php5-devsudo pecl install xdebug 下载安装编译完后,在php.ini ...
- Match:Cyclic Nacklace(KMP的next数组的高级应用)(HDU 3746)
串珠子 题目大意:给定一个字串,要你找到如果要使之成为循环串,在末尾需要的最小的字数(只能添加字符,不能删减字符) 首先联动一下之前做过的动态规划问题POJ 3280,当然了3280这一题是用的LD, ...
- 【leetcode】3Sum (medium)
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- 如何把一个excel工作薄中N个工作表复制到另一个工作薄中
一般遇到标题这样的情况,许多人可能会一个一个的复制粘贴,其实完全不必那么麻烦. 你可以按以下步骤来操作: 第一步:打开所有要操作的excel工作薄\n 第二步:按住Shift键,选择所有要复制的工作表 ...
- windows server 2008 配置安装AD 域控制器
工作需要,搞起AD域来,具体配置如下: 配置环境 Windows版本:Windows Server 2008 R2 Enterprise Service Pack 1 系统类型: 64 位操作系统 配 ...
- js 如何在浏览器中获取当前位置的经纬度
这个有一定的误差哈,具体的误差是多少,有兴趣的朋友可以去测试下 直接上代码 index.html页面代码: <html> <head lang="en"> ...
- php生成对象的研究
<?php abstract class E{ protected $name; function __construct($name){ $this->name = $name; } a ...
- [Android Pro] Java进阶学习:jar打包详解
jar文件听说过吗,没有?或者陌生!好,没关系,这就是我们的第一站:打包发布. 为什么会有这个玩意呢,首先,这是jar的全称:JavaTM Archive (JAR) file,是的,就是java存档 ...