首页 > 系统 > Android > 正文

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

2019-12-12 00:14:21
字体:
来源:转载
供稿:网友

本文实例讲述了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.VeVB.COm/bianmin/miaobiao

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

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

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表