Android实现简单计时器功能

 更新时间:2020年10月21日 17:19:57   作者:Red&&Black  
这篇文章主要为大家详细介绍了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);
  }
  }
 );

 }
}

测试

完成

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

相关文章

  • Android RecycleView 实现左滑上下分层示例代码(自定义功能)

    Android RecycleView 实现左滑上下分层示例代码(自定义功能)

    这篇文章主要介绍了Android RecycleView 实现左滑上下分层示例代码(自定义功能),具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-03-03
  • Android自定义ViewGroup实现标签流效果

    Android自定义ViewGroup实现标签流效果

    这篇文章主要为大家详细介绍了Android自定义ViewGroup实现标签流效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06
  • Android Handler机制详解原理

    Android Handler机制详解原理

    Handler主要用于异步消息的处理:当发出一个消息之后,首先进入一个消息队列,发送消息的函数即刻返回,而另外一个部分在消息队列中逐一将消息取出,然后对消息进行处理,也就是发送消息和接收消息不是同步的处理。 这种机制通常用来处理相对耗时比较长的操作
    2021-11-11
  • android读取assets中Excel表格并显示

    android读取assets中Excel表格并显示

    这篇文章主要为大家详细介绍了android读取assets中Excel表格并显示的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-02-02
  • Android实现计算器(计算表达式/计算小数点以及括号)

    Android实现计算器(计算表达式/计算小数点以及括号)

    这篇文章主要为大家详细介绍了Android实现计算器功能,计算表达式,能计算小数点以及括号,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-09-09
  • Android实现气泡布局/弹窗效果 气泡尖角方向及偏移量可控

    Android实现气泡布局/弹窗效果 气泡尖角方向及偏移量可控

    这篇文章主要为大家详细介绍了Android实现气泡布局/弹窗效果,可控制气泡尖角方向及偏移量,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-08-08
  • Android自定义viewgroup快速滑动(4)

    Android自定义viewgroup快速滑动(4)

    这篇文章主要为大家详细介绍了Android自定义viewgroup快速滑动的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-12-12
  • Android 蓝牙开发实例解析

    Android 蓝牙开发实例解析

    本文主要介绍Android 蓝牙开发,这里提供实例代码和详细解析实现方法,对开发Android蓝牙开发的朋友提供简单示例,有需要的朋友可以参考下
    2016-08-08
  • Android页面中引导蒙层的使用方法详解

    Android页面中引导蒙层的使用方法详解

    这篇文章主要为大家详细介绍了Android页面中的引导蒙层使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-03-03
  • Android 屏幕实现上下翻转

    Android 屏幕实现上下翻转

    这篇文章主要介绍了Android 屏幕实现上下翻转的相关资料,需要的朋友可以参考下
    2017-07-07

最新评论