Android实现简单计时器功能
作者:Red&&Black 发布时间:2021-11-22 03:30:00
标签:Android,计时器
本文实例为大家分享了Android实现简单计时器的具体代码,供大家参考,具体内容如下
布局
在res/layout 下进行布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="0dp">
<TextView
android:text="00:00:00"
android:textSize="60sp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/timeView"/>
<Button
android:text="start"
android:onClick="onClickStart"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/start"/>
<Button
android:text="stop"
android:onClick="onClickStop"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/stop"/>
<Button
android:text="reset"
android:onClick="onClickReset"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/reset"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
MainActivity
package com.test;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private int seconds = 0;
private boolean running = false; //计时状态
private boolean wasRunning = false; //保存running的状态
//app进入后台,暂停计时
@Override
protected void onStop() {
super.onStop();
wasRunning = running;
running = false;
}
//重新进入app,开始计时
@Override
protected void onStart() {
super.onStart();
if(wasRunning) running = true;
}
//失去焦点(如分屏),暂停计时
@Override
protected void onPause() {
super.onPause();
wasRunning = running;
running = false;
}
//获得焦点,重新开始计时
@Override
protected void onResume() {
super.onResume();
if(wasRunning) running = true;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取保存的状态
if(savedInstanceState!=null){
seconds = savedInstanceState.getInt("seconds");
running = savedInstanceState.getBoolean("running");
wasRunning = savedInstanceState.getBoolean("wasRunning");
}
runTime();
}
/**
*保存状态
*/
@Override
public void onSaveInstanceState(Bundle saveInstanceState) {
super.onSaveInstanceState(saveInstanceState);
saveInstanceState.putInt("seconds",seconds);
saveInstanceState.putBoolean("running",running);
saveInstanceState.putBoolean("wasRunning",wasRunning);
}
/**
* 响应button的onClick事件
* 方法名和onClick的值一致
*/
public void onClickStart(View button){
running = true;
}
public void onClickStop(View button){
running = false;
}
public void onClickReset(View button){
running = false;
seconds = 0;
}
/**
* 注意 ui线程不能被堵塞,因此不能在ui线程中调用sleep方法
* 只允许ui线程更新界面,不能在后台线程更新界面
*
* ** 使用ui线程的Handler定时更新 **
* 将任务封装到 Runnable的run方法中 ,通过Handler的
* post(立即提交任务)或postDelayed(实现定时调度)方法提交到ui线程
*/
private void runTime(){
final Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
final TextView textView = findViewById(R.id.timeView);
int hour = seconds /3600%24;
int minute = seconds%3600/60;
String time = String.format("%02d:%02d:%02d",hour,minute,seconds%60);
textView.setText(time);
if(running) seconds++;
handler.postDelayed(this,1000);
}
}
);
}
}
测试
完成
来源:https://blog.csdn.net/m0_46267375/article/details/108777169


猜你喜欢
- 本文实例为大家分享了Android简单使用PopupWindow的的具体代码,供大家参考,具体内容如下思路1.在res下面创建一个menu文
- 一、ArrayList 了解过吗?它是啥?有啥用?众所周知,Java 集合框架拥有两大接口 Collection 和 Map,其中,Coll
- C#字符串提取数值(带小数点)string input = "树2草45210.2m2";if (GetInputUti
- •强引用(FinalReference),在java中,有点像C++的指针,通过引用,可以对堆中的对象进行操作。强引用具备以下特点: 1.强
- 下载Android SDK两种方式:(1)官网下载(需翻墙):https://developer.android.com/studio/in
- Java doGet, doPost方法和文件上传index.html<!DOCTYPE html><html lang=
- 一些公共的模板############################################### 对于一些基本指令的添加####
- 在我们的程序当中如果要实现类似《360软件管家》的功能,就要解决两个问题,首先是要判断该程序已有一个实例在运行,其次是要将已运行的应用程序实
- java控制台输入有如下几个方法1、JDK 1.4 及以下版本读取的方法JDK 1.4 及以下的版本中要想从控制台中输入数据只有一种办法,即
- Spring 使用Junit单元测试并配置数据源一、问题描述由于公司项目中的数据源是配置在Tomcat中的server.xml中的,所以在使
- 对称加密算法是应用较早的加密算法,技术成熟。在对称加密算法中,数据发信方将明文(原始数据)和加密密钥(mi yue)一起经过特殊加密算法处理
- 前言本篇文章主要讲述的是SpringBoot整合Mybatis、Druid和PageHelper 并实现多数据源和分页。其中SpringBo
- 这里介绍通过委托取消Button事件switch-case的方法。需要注意的是,事先要按顺序在各个Button的Tag属性中设置0、1、2、
- main方法调用spring的service将业务层类配置到Spring中:<bean id="customerServic
- 如果没有安装过maven,是用的idea自带的maven,那就是idea的安装目录下 /plugins/maven/lib/maven3这个
- 经常会遇到这样的情况,我们在响应客户端请求的数据的时候需要对数据进行处理,比如数据库中的数据是int型,它可能表示某个枚举,或者其它的逻辑意
- 引言float和double类型的主要设计目标是为了科学计算和工程计算。他们执行二进制浮点运算,这是为了在广域数值范围上提供较为精确的快速近
- 在字符集中,有一类字符具有这样的特性:当从键盘上输入这个字符时,显示器上就可以显示这个字符,即输入什么就显示什么。这类字符称为可显示字符,如
- 话不多说直接上代码,简单明了import java.io.File;import java.io.FileInputStream;impor
- 在远程调用中,需要把参数和返回值通过网络传输,这个使用