CountDownTimer完整具体演示样例
MainActivity例如以下:
package cc.cv; import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
/**
* Demo演示样例:
* CountDownTimer完整具体演示样例
* 代码非常easy,直接看凝视就可以
*
* CountDownTimer是Android4.0引入的倒计时
*/
public class MainActivity extends Activity {
private Button mStartButton;
private Button mCancelButton;
private CountDownTimerSubClass mCountDownTimerSubClass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
} private void init(){
mCountDownTimerSubClass=new CountDownTimerSubClass(15*1000, 1000);
mStartButton=(Button) findViewById(R.id.startButton);
//開始倒计时
mStartButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
mCountDownTimerSubClass.start();
}
});
//取消倒计时.
//再次调用CountDownTimer的start时会又一次開始倒计时.
mCancelButton=(Button) findViewById(R.id.cancelButton);
mCancelButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
mCountDownTimerSubClass.cancel();
}
});
} private class CountDownTimerSubClass extends CountDownTimer{
/**
* millisInFuture 倒计时间
* countDownInterval 每两次倒计时之间的间隔
*/
public CountDownTimerSubClass(long millisInFuture,long countDownInterval) {
super(millisInFuture, countDownInterval);
} /**
* 倒计时结束
*/
@Override
public void onFinish() {
System.out.println("结束");
} /**
* 每个倒计时间点到来时均会触发该方法
* millisUntilFinished表示整个倒计时剩余的时间
*/
@Override
public void onTick(long millisUntilFinished) {
long remainedSeconds=millisUntilFinished/1000;
System.out.println("剩余: "+remainedSeconds+" s");
} } }
main.xml例如以下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
> <Button
android:id="@+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:text="start" /> <Button
android:id="@+id/cancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/startButton"
android:layout_marginTop="100dp"
android:text="cancel" /> </RelativeLayout>
CountDownTimer完整具体演示样例的更多相关文章
- Android利用Volley异步载入数据完整具体演示样例(二)
MainActivity例如以下: package cc.y; import android.app.Activity; import android.content.Context; import ...
- Android清理设备内存具体完整演示样例(二)
版权声明: https://blog.csdn.net/lfdfhl/article/details/27672913 MainActivity例如以下: package cc.c; import j ...
- FutureTask使用完整演示样例
MainActivity例如以下: package cc.cv; import java.util.concurrent.FutureTask; import android.os.Bundle; i ...
- 通过Canvas及File API缩放并上传图片完整演示样例
创建一个只管的用户界面,并同意你控制图片的大小.上传到server端的数据,并不须要处理enctype为 multi-part/form-data 的情况.只一个简单的POST表单处理程序就能够了. ...
- 源代码方式向openssl中加入新算法完整具体步骤(演示样例:摘要算法SM3)【非engine方式】
openssl简单介绍 openssl是一个功能丰富且自包括的开源安全工具箱.它提供的主要功能有:SSL协议实现(包括SSLv2.SSLv3和TLSv1).大量软算法(对称/非对称/摘要).大数运算. ...
- 让你提前认识软件开发(19):C语言中的协议及单元測试演示样例
第1部分 又一次认识C语言 C语言中的协议及单元測试演示样例 [文章摘要] 在实际的软件开发项目中.常常要实现多个模块之间的通信.这就须要大家约定好相互之间的通信协议,各自依照协议来收发和解析消息. ...
- WebGL自学教程——WebGL演示样例:開始
最终開始WebGL的演示样例了,...... 開始 使用WebGL的步骤,非常easy: 1. 获得WebGL的渲染环境(也叫渲染上下文). 2. 发挥你的想象力,利用<WebGL參考手冊> ...
- Eureka 的 Application Client client的执行演示样例
上篇以一个 demo 演示样例介绍了 Eureka 的 Application Service 客户端角色.今天我们继续了解 Eureka 的 Application Client 客 ...
- 10分钟理解Android数据库的创建与使用(附具体解释和演示样例代码)
1.Android数据库简单介绍. Android系统的framework层集成了Sqlite3数据库.我们知道Sqlite3是一种轻量级的高效存储的数据库. Sqlite数据库具有以下长处: (1) ...
随机推荐
- Farseer.net轻量级开源框架 入门篇:查询数据详解
导航 目 录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 删除数据详解 下一篇:Farseer.net轻量级开源框架 中级篇: Where条 ...
- vscode常好用的插件以及几个快捷操作
使用方法,可以在官网中搜索需要的插件或者在VsCode的“”扩展“”中搜索需要的插件添加方法使用Ctrl+P, 输入 ext install xxxx ,搜索要安装的插件,点击安装按钮即可(各取所需插 ...
- Codeforces_779_D.String Game_(二分)
D. String Game time limit per test 2 seconds memory limit per test 512 megabytes input standard inpu ...
- Java学习4_一些基础4_输入输出_16.5.7
读取输入: 想从控制台进行输入,首先需要构造一个Scanner对象,并与“标准输入流”System.in关联. Scanner in=new Scanner(System.in); String na ...
- POJ_1611_The Suspect
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 25149 Accepted: 12329 De ...
- Call stack-函数调用栈
https://en.wikipedia.org/wiki/Call_stack#STACK-FRAME In computer science, a call stack is a stack da ...
- Spring 中无处不在的 Properties
转自:https://javadoop.com/post/spring-properties?hmsr=toutiao.io&utm_medium=toutiao.io&utm_sou ...
- 使用 MyBatis 对表执行 CRUD 操作
说明: 1.CRUD: C -- create R -- read U -- update D -- delete 2.Mybatis 的 SQL 核心配置文件中 SQL 语句的参数的传 ...
- Django-django-redis使用
自定义连接池 这种方式跟普通py文件操作redis一样,代码如下: views.py import redis from django.shortcuts import render,HttpResp ...
- Jboss 服务器SSL证书安装指南
1.获取服务器证书 将证书签发邮件中的从BEGIN到 END结束的服务器证书内容(包括“-----BEGIN CERTIFICATE-----”和“-----END CERTIFICATE-----” ...