前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android开发实现的计时器功能示例

Android开发实现的计时器功能示例

作者头像
砸漏
发布2020-11-05 10:33:42
9230
发布2020-11-05 10:33:42
举报
文章被收录于专栏:恩蓝脚本恩蓝脚本

本文实例讲述了Android开发实现的计时器功能。分享给大家供大家参考,具体如下:

效果图:

布局:

三个按钮 加上一个Chronometer

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"? 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity"
  android:orientation="vertical"
  android:gravity="center_horizontal" 
  <Chronometer
    android:id="@+id/test"
    android:textSize="25pt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" / 
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    <Button
      android:id="@+id/start"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="开始"
      android:layout_weight="1"/ 
    <Button
      android:id="@+id/pause"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="暂停"
      android:layout_weight="1"/ 
    <Button
      android:id="@+id/go_on"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="继续"
      android:layout_weight="1"/ 
  </LinearLayout 
</LinearLayout 

实现:

四个监听事件 三个按钮 一个计时器

代码语言:javascript
复制
package com.example.a30797.androidtest;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
public class MainActivity extends AppCompatActivity {
  Chronometer ch ;
  Button start ;
  Button pause ;
  Button restart ;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //获取计时器组件
    ch = (Chronometer) findViewById(R.id.test);
    //获取开始按钮
    start = (Button) findViewById(R.id.start) ;
    //暂停计时按钮
    pause = (Button) findViewById(R.id.pause);
    //继续计时按钮
    restart = (Button) findViewById(R.id.go_on);
    start.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        //设置开始计时时间
        ch.setBase(SystemClock.elapsedRealtime() );
        //启动计时器
        ch.start();
        pause.setEnabled(true);
        restart.setEnabled(false);
        start.setEnabled(false);
      }
    });
    //暂停按钮监听器
    pause.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        start.setText("重新开始");
        ch.stop();
        start.setEnabled(true);
        restart.setEnabled(true);
        pause.setEnabled(false);
      }
    });
    //暂停按钮监听器
    restart.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        start.setText("重新开始");
        ch.start();
        start.setEnabled(true);
        pause.setEnabled(true);
        restart.setEnabled(false);
      }
    });
    //为Chronomter绑定事件监听器
    ch.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
      @Override
      public void onChronometerTick(Chronometer chronometer) {
        //如果计时到现在超过了一小时秒
        if ( SystemClock.elapsedRealtime() - ch.getBase()   3600 * 1000) {
          ch.stop();
          start.setEnabled(true);
          restart.setEnabled(false);
          pause.setEnabled(false);
        }
      }
    });
  }
}

PS:这里再为大家推荐几款相关的在线工具供大家参考:

在线秒表工具: http://tools.zalou.cn/bianmin/miaobiao

Unix时间戳(timestamp)转换工具: http://tools.zalou.cn/code/unixtime

希望本文所述对大家Android程序设计有所帮助。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档