本文实例为大家分享了Android实现计时器功能的具体代码,供大家参考,具体内容如下

计时器工具类

import android.annotation.SuppressLint;

import android.os.Handler;

import android.os.Message;

import androidx.annotation.NonNull;

import java.util.Timer;

import java.util.TimerTask;

/**

* 用于计时,在主线程中使用此方法

*/

public class ChjTimer {

private int time;//设置倒计时 X 秒

private int interval = 1000;//设置间隔时间

private ChjTimerInter chjTimerInter; //回调

private Timer timer; // 定时器

private static final int WHAT_REFREH = 0;//刷新

/**

* 创建对象则开始计时

*

* @param chjTimerInter 接口回调

*/

public ChjTimer(ChjTimerInter chjTimerInter) {

this.chjTimerInter = chjTimerInter;

}

/**

* 创建对象开始计时

* @param interval 间隔时间通知(使用第一个方法,默认1秒钟刷新一次)

* @param chjTimerInter 接口回调

*/

public ChjTimer(int interval, ChjTimerInter chjTimerInter) {

this.chjTimerInter = chjTimerInter;

this.interval = interval;

}

/**

* 开始计时

*/

public void start(int time) {

this.time = time;

if (timer == null){

timer = new Timer();

} else {

stop();

return;

}

timer.schedule(new TimerTask() {

@Override

public void run() {

timesss.sendMessage(new Message());

}

}, interval);

}

/**

* 终止计时

*/

public void stop() {

if (timer != null) {

timer.cancel();

timer = null;

}

if (timesss != null) timesss.removeMessages(WHAT_REFREH);

if (chjTimerInter != null)chjTimerInter.stop(time);

}

@SuppressLint("HandlerLeak")

private Handler timesss = new Handler() {

@Override

public void handleMessage(@NonNull Message msg) {

super.handleMessage(msg);

if (msg.what != WHAT_REFREH) return;

time -= 1;

if (chjTimerInter != null) chjTimerInter.second(time);

if (time == 0) {

if (timer == null) return;

timer.cancel();

timer = null;

if (chjTimerInter != null) chjTimerInter.expire();

} else if (time > 0) {

timer.schedule(new TimerTask() {

@Override

public void run() {

timesss.sendMessage(new Message());

}

}, interval);

}

}

};

/**

* 接口

*/

public interface ChjTimerInter {

/**

* 间隔时间内回调

*/

void second(int time);

/**

* 完成回调

*/

void expire();

/**

* 终止计时

*/

void stop(int time);

}

}

使用演示

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.TextView;

public class MainActivity extends Activity implements View.OnClickListener, ChjTimer.ChjTimerInter {

private TextView tiems,timnew;

private ChjTimer chjTimer;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

tiems = findViewById(R.id.time);

timnew = findViewById(R.id.timnew);

findViewById(R.id.but).setOnClickListener(this);

findViewById(R.id.buts).setOnClickListener(this);

chjTimer = new ChjTimer(this);

}

@Override

public void onClick(View view) {

switch (view.getId()){

case R.id.but:

tiems.setText("10");

timnew.setText("正在计时");

chjTimer.start(10);

break;

case R.id.buts:

chjTimer.stop();

break;

}

}

@Override

public void second(int time) {

tiems.setText(time + "");

}

@Override

public void expire() {

timnew.setText("计时完成");

}

@Override

public void stop(int time) {

timnew.setText("计时终止" + time);

}

}

页面布局

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

xmlns:tools="http://schemas.android.com/tools"

android:orientation="vertical"

android:layout_gravity="center"

android:gravity="center"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

android:id="@+id/timnew"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="计时完成"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintLeft_toLeftOf="parent"

app:layout_constraintRight_toRightOf="parent"

app:layout_constraintTop_toTopOf="parent" />

android:id="@+id/time"

android:text="0"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

android:id="@+id/but"

android:text="开始"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

android:id="@+id/buts"

android:text="终止"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

演示效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

android 计时器工具类,Android实现计时器功能相关推荐

  1. android sharedpreferences 工具类,android sharedpreferences工具类

    释放双眼,带上耳机,听听看~! 今天,简单讲讲如何写一个sharedpreferences的工具类. 很简单,把一些重复的操作封装在工具类里,其他地方调用就可以.在网上搜索了比较多的资料,找到一个比较 ...

  2. android 计时器工具类,Android中通用定时器--好用的工具

    package com.utility.common; import android.os.Handler; import android.os.Message; public class BaseT ...

  3. android 开发工具类,Android中常用开发工具类—持续更新...

    一.自定义ActionBar public class ActionBarTool { public static void setActionBarLayout(Activity act,Conte ...

  4. Android加密工具类,Android AES加密工具类分享

    1.AES加密工具类 java不支持PKCS7Padding,只支持PKCS5Padding.我们知道加密算法由算法+模式+填充组成,下一篇介绍iOS和Android通用的AES加密,本篇文章使用PK ...

  5. android attributeset 工具类,Android使用AttributeSet自定义控件的方法

    释放双眼,带上耳机,听听看~! 所谓自定义控件(或称组件)也就是编写自己的控件类型,而非Android中提供的标准的控件,如TextView,CheckBox等等.不过自定义的控件一般也都是从标准控件 ...

  6. android attributeset 工具类,android – 如何将AttributeSet传递给自定义视图

    如何将当前的AttributeSet传递给自定义View类?如果我使用的参数中只有Context的构造函数,我将丢失所有主题,并在该自定义视图的xml中使用"style"标签的能力 ...

  7. android dp工具类,Android 单位px、dp、sp转换工具类

    import android.content.Context; import android.util.TypedValue; //常用单位转换的辅助类 public DensityUtils { p ...

  8. android attributeset 工具类,android attributeset总结

    一般是当项目中遇到这样的场景需要自定义控件的AttributeSet属性:一个自定义控件的有些属性内容是随着外部条件而动态改变的,for example:一个自定义的ListView控件,需要在底部添 ...

  9. Android开发工具类 Utils

    包括了各种工具类.辅助类.管理类等 Awesome_API: https://github.com/marktony/Awesome_API/blob/master/Chinese.md 收集中国国内 ...

  10. Android开发工具类

    包括了各种工具类.辅助类.管理类等 Awesome_API: https://github.com/marktony/Awesome_API/blob/master/Chinese.md 收集中国国内 ...

最新文章

  1. Food HDU - 4292 网络流
  2. java account函数的_用Java进行同时函数调用 - java
  3. [Tips on Ember 2] 如何尝试 angle-bracket component
  4. 大型企业网络配置系列课程详解(三)--OSPF高级配置与相关概念的理解
  5. RadioGroup和RadioButton(单选框)
  6. 新鲜零食=零食“新”市场+用户“鲜”生活
  7. Linux IPC实践(3) --具名FIFO
  8. Wireshark-003过滤器
  9. ubuntu mysql环境变量配置_MySQL在Win10与Ubuntu下的安装与配置
  10. 用.NET编程风格实现Ajax——Atlas快速入门
  11. Unity3D TextMeshPro
  12. WINDOWS XP数字总线补丁 KB888111
  13. qtdesigner设计表格_使用Qt Designer进行布局
  14. java求指数_Java Math类的常用方法,指数运算
  15. 通过jquery读cookie添加xsrf HTTP头来避免请求伪造
  16. 4星|《经济学通识课》:深入浅出的经济学思想发展简史
  17. python画误差棒_Python数据处理从零开始----第四章(可视化)(3)散点图和误差棒...
  18. 电商平台退货管理教程
  19. canvas学习(html5)画画
  20. 开启oracle的acfs,配置OracleACFS集群文件系统

热门文章

  1. 一个基于百度云和图灵的人工智能程序
  2. Dubbo初识-入门
  3. 96309245通讯异常工行_工商银行信息代码 96309245 是什么意思
  4. 苹果Appstore加急审核方法
  5. Win10指定用户访问共享文件及“无法访问。你可能没有权限使用网络资源。”问题解决
  6. MySQL修改数据表中的字段名_MySQL修改数据表中的字段名
  7. Android自动化-双击操作
  8. 关于区块链的一些特有技术
  9. zyf整合ssm环境
  10. AWK手册(ZYF译)