Android实现倒计时效果
作者:nixs_0702 发布时间:2021-10-13 03:25:29
标签:Android,倒计时
本文实例为大家分享了Android实现倒计时效果的具体代码,供大家参考,具体内容如下
一个倒计时的效果
先看效果图:
直接上代码:
这里是关于倒计时 …天时分秒…的逻辑判断
/**
* 倒计时计算
*/
private void computeTime() {
mSecond--;
if (mSecond < 0) {
mMin--;
mSecond = 59;
if (mMin < 0) {
mMin = 59;
mHour--;
if (mHour < 0) {
// 倒计时结束
mHour = 23;
mDay--;
if(mDay < 0){
// 倒计时结束
mDay = 0;
mHour= 0;
mMin = 0;
mSecond = 0;
}
}
}
}
}
定时器主要代码如下…当然也可以开线程或者开后台服务来处理…只是没那种必要…定时器就可以搞定容易控制…毕竟倒计时时间起点…你总得后台获取吧,不是做时钟闹钟…如果是做时钟闹钟…拿你也不用考虑后台服务或者自己开线程…而是使用AlarmManager来实现
/**
* 开启倒计时
* //time为Date类型:在指定时间执行一次。
* timer.schedule(task, time);
* //firstTime为Date类型,period为long,表示从firstTime时刻开始,每隔period毫秒执行一次。
* timer.schedule(task, firstTime,period);
* //delay 为long类型:从现在起过delay毫秒执行一次。
* timer.schedule(task, delay);
* //delay为long,period为long:从现在起过delay毫秒以后,每隔period毫秒执行一次。
* timer.schedule(task, delay,period);
*/
private void startRun() {
TimerTask mTimerTask = new TimerTask() {
@Override
public void run() {
Message message = Message.obtain();
message.what = 1;
timeHandler.sendMessage(message);
}
};
mTimer.schedule(mTimerTask,0,1000);
}
修改界面,利用handler来提醒更新界面
private Handler timeHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 1) {
computeTime();
mDays_Tv.setText(mDay+"");//天数不用补位
mHours_Tv.setText(getTv(mHour));
mMinutes_Tv.setText(getTv(mMin));
mSeconds_Tv.setText(getTv(mSecond));
if (mSecond == 0 && mDay == 0 && mHour == 0 && mMin == 0 ) {
mTimer.cancel();
}
}
}
};
private String getTv(long l){
if(l>=10){
return l+"";
}else{
return "0"+l;//小于10,,前面补位一个"0"
}
}
附带主activity的代码…
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
private RelativeLayout countDown;
// 倒计时
private TextView mDays_Tv, mHours_Tv, mMinutes_Tv, mSeconds_Tv;
private long mDay = 23;// 天
private long mHour = 11;//小时,
private long mMin = 56;//分钟,
private long mSecond = 32;//秒
private Timer mTimer;
private Handler timeHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 1) {
computeTime();
mDays_Tv.setText(mDay+"");//天数不用补位
mHours_Tv.setText(getTv(mHour));
mMinutes_Tv.setText(getTv(mMin));
mSeconds_Tv.setText(getTv(mSecond));
if (mSecond == 0 && mDay == 0 && mHour == 0 && mMin == 0 ) {
mTimer.cancel();
}
}
}
};
private String getTv(long l){
if(l>=10){
return l+"";
}else{
return "0"+l;//小于10,,前面补位一个"0"
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTimer = new Timer();
countDown = (RelativeLayout) findViewById(R.id.countdown_layout);
mDays_Tv = (TextView) findViewById(R.id.days_tv);
mHours_Tv = (TextView) findViewById(R.id.hours_tv);
mMinutes_Tv = (TextView) findViewById(R.id.minutes_tv);
mSeconds_Tv = (TextView) findViewById(R.id.seconds_tv);
startRun();
}
/**
* 开启倒计时
* //time为Date类型:在指定时间执行一次。
* timer.schedule(task, time);
* //firstTime为Date类型,period为long,表示从firstTime时刻开始,每隔period毫秒执行一次。
* timer.schedule(task, firstTime,period);
* //delay 为long类型:从现在起过delay毫秒执行一次。
* timer.schedule(task, delay);
* //delay为long,period为long:从现在起过delay毫秒以后,每隔period毫秒执行一次。
* timer.schedule(task, delay,period);
*/
private void startRun() {
TimerTask mTimerTask = new TimerTask() {
@Override
public void run() {
Message message = Message.obtain();
message.what = 1;
timeHandler.sendMessage(message);
}
};
mTimer.schedule(mTimerTask,0,1000);
}
/**
* 倒计时计算
*/
private void computeTime() {
mSecond--;
if (mSecond < 0) {
mMin--;
mSecond = 59;
if (mMin < 0) {
mMin = 59;
mHour--;
if (mHour < 0) {
// 倒计时结束
mHour = 23;
mDay--;
if(mDay < 0){
// 倒计时结束
mDay = 0;
mHour= 0;
mMin = 0;
mSecond = 0;
}
}
}
}
}
}
附带xml的代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/countdown_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:gravity="center" >
<RelativeLayout
android:id="@+id/daojishi_rl"
android:layout_width="match_parent"
android:layout_height="40.0dip"
android:layout_marginLeft="10.0dip"
android:layout_marginRight="10.0dip"
android:gravity="center" >
<ImageView
android:id="@+id/describe_iv"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@mipmap/img"
android:scaleType="fitXY"
android:gravity="center_vertical" />
<TextView
android:id="@+id/describe_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="5.0dip"
android:layout_toRightOf="@+id/describe_iv"
android:text="距离开团还有"
android:textSize="25sp" />
<TextView
android:id="@+id/days_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:padding="4dp"
android:layout_toRightOf="@+id/describe_tv"
android:background="#c2c2c2"
android:gravity="center"
android:text=""
android:textSize="20sp" />
<TextView
android:id="@+id/colon0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5.0dip"
android:layout_marginRight="3.0dip"
android:layout_toRightOf="@+id/days_tv"
android:text="天"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/daojishi_rl"
android:gravity="center_horizontal" >
<TextView
android:id="@+id/hours_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/colon1"
android:background="#c2c2c2"
android:gravity="center"
android:text="23"
android:padding="3dp"
android:textSize="20sp" />
<TextView
android:id="@+id/colon1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="3.0dip"
android:layout_marginRight="3.0dip"
android:layout_toLeftOf="@+id/minutes_tv"
android:text=":"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/minutes_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/colon2"
android:background="#c2c2c2"
android:gravity="center"
android:text="59"
android:padding="3dp"
android:textSize="20sp" />
<TextView
android:id="@+id/colon2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="3.0dip"
android:layout_marginRight="3.0dip"
android:layout_toLeftOf="@+id/seconds_tv"
android:text=":"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/seconds_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#c2c2c2"
android:gravity="center"
android:text="59"
android:padding="3dp"
android:textSize="20sp" />
</RelativeLayout>
</RelativeLayout>
完美实现,直接用就可以了。
来源:https://blog.csdn.net/wvqusrtg/article/details/109032130


猜你喜欢
- 如今RxJava和Retrofit的结合使用估计已经相当普遍了,自己工作中也是一直都在使用。在使用的过程中我们都会对其进行封装使用,GitH
- 本文实例讲述了java实现MD5加密的方法。分享给大家供大家参考,具体如下:private String getMD5Str(String
- checkbox控件时导致Activity启动默认不显示输入法。网上很多资料说要放一个空的Linearlayout,完全是在误导大众,正确的
- 这是我们用得比较多的一种设计模式,也是23种标准设计模式之一,使用前面讲的简单工厂设计模式,遇到具体产品经常变换时就不太适合了,违反了开闭设
- 在Android开发中,View是我们必须要接触的用来展示的技术.通常情况下随着View视图的越来越复杂,整体布局的性能也会随之下降.这里介
- 很多情况下sql不好解决的多表查询,临时表分组,排序,尽量用java8新特性stream进行处理使用java8新特性,下面先来点基础的Lis
- 前言作为Java开发者,我们每天都会创建大量的对象,但是,我们总是使用管理依赖系统(如Spring框架)来创建这些对象。其实还有其他方法可以
- spring boot ${}占位符不起作用问题:在 pom.xml 文件里定义好属性标签,然后在 properties或者xml 中使用$
- Stripe支付首页需要引用Stripe.net框架,我引用的是22.8.0版本,注意.NETFramework的版本为4.5,同时需要引用
- 本文实例讲述了Android实现的秒表计时器。分享给大家供大家参考,具体如下:package com.liu.time;import jav
- 目录1、前提知识2、实现思路:1、前提知识需要知道简单的IO流操作,以及简单的UDP发送数据包的原理。需要用到的类:DatagramSock
- 在进行一个表达式的计算时,先将表达式分割成数字和字符串然后利用出入栈将分割后的表达式进行中缀转后缀,再将后缀表达式进行计算得到结果(思想在上
- 环境信息名称版本号Spring Boot2.4.5Idea2021.3.2服务端实现导入依赖<dependency>  
- 本文我们将讲解一下对于“大对象”的优化。这里的“大对象”,是
- 如果一个内存中的对象没有任何引用的话,就说明这个对象已经不再被使用了,从而可以成为被垃圾回收的候选。不过由于垃圾回收器的运行时间不确定,可被
- 在前面的一篇文章中,简单的介绍了一下如何实现软键盘不自动弹出,使用的方法是设置android:wind
- 使用过Mybatis的同学,应该都知道,我们只需要编写mybatis对应的接口和mapper XML文件即可,并不需要手动编写mapper接
- 背景:在Android中按照数据保存的方式,可以分为如下几种Content Provider (用的SQLite实现),SQLite,Sha
- 一、什么是单例模式?单例设计模式(Singleton Design Pattern)理解起来非常简单。一个类只允许创建一个对象(或者实例),
- 在8 里面Lambda是最火的主题,不仅仅是因为语法的改变,更重要的是带来了函数式编程的思想,我觉得优秀的程序员,有必要学习一下函数式编程的