温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-10-23 21:30:32 来源:脚本之家 阅读:228 作者:水中鱼之1999 栏目:移动开发

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

效果图:

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

布局:

三个按钮 加上一个Chronometer

<?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>

实现:

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

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.jb51.net/bianmin/miaobiao

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

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android日期与时间操作技巧总结》、《Android开发入门与进阶教程》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI