Android实现倒计时方法汇总
程序员文章站
2024-03-31 11:04:52
android开发中经常会有倒计时的功能,下面将总结出常见的集中实现方式。
1.直接使用handler的消息机制来实现
xml布局中文件如下:
<...
android开发中经常会有倒计时的功能,下面将总结出常见的集中实现方式。
1.直接使用handler的消息机制来实现
xml布局中文件如下:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onclick="clickbutton" android:text="开始计时" /> </linearlayout>
java代码如下:
import android.app.activity; import android.os.bundle; import android.os.handler; import android.os.message; import android.view.view; import android.widget.button; public class firstactivity extends activity{ private button button; private int count = 60; private int count_time = 0; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.first_activity); button = (button) findviewbyid(r.id.button); } private handler handler = new handler(){ @override public void handlemessage(message msg) { if(count <= 0){ count = 60; button.settext("重新计时"); button.setclickable(true); return; } count--; button.settext(""+count); sendemptymessagedelayed(count_time,1000); } }; public void clickbutton(view view){ handler.sendemptymessage(count_time); button.setclickable(false); } }
2.使用timer和timertask,结合handler一起实现倒计时
import java.util.timer; import java.util.timertask; import android.app.activity; import android.os.bundle; import android.os.handler; import android.os.message; import android.view.view; import android.widget.button; public class firstactivity extends activity{ private button button; private int count = 30; private int count_time = 0; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.first_activity); button = (button) findviewbyid(r.id.button); } private handler handler = new handler(){ @override public void handlemessage(message msg) { button.settext(""+count); if(count <= 0){ count = 30; button.setclickable(true); button.settext("重新计时"); timertask.cancel(); //取消该任务 } } }; private timer timer = new timer(); private timertask timertask; public void clickbutton(view view){ button.setclickable(false); timertask = new timertask() { @override public void run() { count--; handler.sendemptymessage(count_time); } }; timer.schedule(timertask,0,1000); //0秒后,每过一秒钟执行一次该任务 } @override protected void ondestroy() { super.ondestroy(); //释放资源 if(timertask != null){ timertask.cancel(); timertask = null; } if(timer != null){ timer.cancel(); timer = null; } } }
3.使用android自带的原生倒计时类 countdowntimer
import android.app.activity; import android.os.bundle; import android.os.countdowntimer; import android.view.view; import android.widget.button; public class firstactivity extends activity{ private button button; private countdowntimer timer; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.first_activity); button = (button) findviewbyid(r.id.button); } public void clickbutton(view view){ button.setclickable(false); //第一个参数:倒计时的毫秒数;第二个参数:接收ontick回调的时间间隔 timer = new countdowntimer(30000, 10) { public void ontick(long millisuntilfinished) { button.settext(millisuntilfinished / 1000 + "秒"); } public void onfinish() { button.settext("重新计时"); button.setclickable(true); } }; timer.start(); } @override protected void ondestroy() { super.ondestroy(); if(timer != null){ timer.cancel(); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 逐步讲解MySQL中定时事件计划的创建
下一篇: python 开发的三种运行模式详细介绍