Android开发实现的计时器功能示例
作者:水中鱼之1999 时间:2023-09-04 07:33:33
本文实例讲述了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程序设计有所帮助。
来源:https://blog.csdn.net/qq_43377749/article/details/84068373
标签:Android,计时器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java-String类最全汇总(上篇)
2023-04-15 11:47:10
![](https://img.aspxhome.com/file/2023/1/83571_0s.jpg)
iOS WebView中使用webp格式图片的方法
2023-06-17 22:06:03
MyBatis-Plus QueryWrapper及LambdaQueryWrapper的使用详解
2021-07-02 03:06:16
![](https://img.aspxhome.com/file/2023/8/92558_0s.jpg)
Java 输入流中的read(byte[] b)方法详解
2022-08-13 18:58:12
![](https://img.aspxhome.com/file/2023/8/101948_0s.jpg)
Spring Boot 2.7.6整合redis与低版本的区别
2023-11-11 19:44:56
![](https://img.aspxhome.com/file/2023/7/59697_0s.png)
springBoot Junit测试用例出现@Autowired不生效的解决
2023-01-24 12:57:59
![](https://img.aspxhome.com/file/2023/0/73120_0s.png)
Winform 实现进度条弹窗和任务控制
2023-06-20 04:27:09
![](https://img.aspxhome.com/file/2023/4/66134_0s.png)
Kotlin协程开发之Flow的融合与Channel容量及溢出策略介绍
2023-07-02 23:52:01
spring拓展之如何定义自己的namespace
2022-01-12 05:13:33
![](https://img.aspxhome.com/file/2023/3/79873_0s.png)
C#根据年月日计算星期几的函数
2022-03-17 04:50:50
Android编程下拉菜单spinner用法小结(附2则示例)
2022-11-02 20:25:45
![](https://img.aspxhome.com/file/2023/5/111665_0s.jpg)
SpringBoot项目启动时如何读取配置以及初始化资源
2021-11-19 04:04:11
![](https://img.aspxhome.com/file/2023/4/79274_0s.png)
C++ 实现球迷 今日头条面试题
2022-07-08 11:03:24
Android 6.0动态权限申请教程
2023-09-26 16:43:56
Mybatis延迟加载和缓存深入讲解
2022-06-02 15:50:43
![](https://img.aspxhome.com/file/2023/6/69686_0s.png)
java代码实现C盘文件统计工具
2021-06-23 01:22:38
![](https://img.aspxhome.com/file/2023/4/67664_0s.png)
Kotlin方法与Lambda表达式实践使用介绍
2021-11-07 08:03:04
![](https://img.aspxhome.com/file/2023/9/113319_0s.png)
MyBatis的9种动态标签详解
2021-06-21 19:03:40
JAVA读取文件夹大小的几种方法实例
2021-05-24 21:01:53
关于java 图形验证码的解决方法
2023-08-09 15:21:19