Chronometer控件实现的Android计时器
本文为大家演示了如何使用Chronometer控件实现Android计时器的实例。
先贴上最终的实现效果图:
Android计时器实现思路
使用Chronometer控件实现计器的操作。通过设置setBase(long base)来设置初始时间,然后为其添加一个 setOnChronometerTickListener(Chronometer.OnChronometerTickListener l)事件来判断时间是否到了,然后再调用其stop()方法实现停止计时。
Android计时器实现代码
main.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/back"
- android:gravity="center"
- android:orientation="vertical" >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_margin="10dip"
- android:orientation="horizontal" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="4"
- android:gravity="center"
- android:text="设置时间:" />
- <EditText
- android:id="@+id/edt_settime"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:inputType="number" />
- </LinearLayout>
- <Chronometer
- android:id="@+id/chronometer"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:textColor="#ff0000"
- android:textSize="60dip" />
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_margin="10dip"
- android:orientation="horizontal" >
- <Button
- android:id="@+id/btnStart"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="开始记时" />
- <Button
- android:id="@+id/btnStop"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="停止记时" />
- <Button
- android:id="@+id/btnReset"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="重置" />
- </LinearLayout>
- </LinearLayout>
Activity代码:
- package com.jiahui.chronometer;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.os.SystemClock;
- import android.text.format.Time;
- import android.view.View;
- import android.widget.Button;
- import android.widget.Chronometer;
- import android.widget.EditText;
- public class ChronometerDemoActivity extends Activity {
- private int startTime = 0;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- final Chronometer chronometer = (Chronometer) findViewById(R.id.chronometer);
- Button btnStart = (Button) findViewById(R.id.btnStart);
- Button btnStop = (Button) findViewById(R.id.btnStop);
- Button btnRest = (Button) findViewById(R.id.btnReset);
- final EditText edtSetTime = (EditText) findViewById(R.id.edt_settime);
- btnStart.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- System.out.println("--开始记时---");
- String ss = edtSetTime.getText().toString();
- if (!(ss.equals("") && ss != null)) {
- startTime = Integer.parseInt(edtSetTime.getText()
- .toString());
- }
- // 设置开始讲时时间
- chronometer.setBase(SystemClock.elapsedRealtime());
- // 开始记时
- chronometer.start();
- }
- });
- btnStop.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // 停止
- chronometer.stop();
- }
- });
- // 重置
- btnRest.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- chronometer.setBase(SystemClock.elapsedRealtime());
- }
- });
- chronometer
- .setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
- @Override
- public void onChronometerTick(Chronometer chronometer) {
- // 如果开始计时到现在超过了startime秒
- if (SystemClock.elapsedRealtime()
- - chronometer.getBase() > startTime * 1000) {
- chronometer.stop();
- // 给用户提示
- showDialog();
- }
- }
- });
- }
- protected void showDialog() {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setIcon(R.drawable.eb28d25);
- builder.setTitle("警告").setMessage("时间到")
- .setPositiveButton("确定", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- }
- });
- AlertDialog dialog = builder.create();
- dialog.show();
- }
- }
转载于:https://www.cnblogs.com/xiaochao1234/p/3937649.html
Chronometer控件实现的Android计时器相关推荐
- Android Chronometer控件
1. Chronometer类 Chronometer是一个简单的计时器. 主要配置 format,显示格式,默认是"MM:SS"或"H:MM:SS",以%s来 ...
- android studio控件使用方法,android studio使用教程
android studio使用教程 Android Studio是谷歌推出的一个Android集成开发工具,基于IntelliJ IDEA. 类似 Eclipse ADT,Android Studi ...
- android 代码控件框高,Android控件_TextView(显示文本框控件)
一.TextView控件的常用属性 1.android:id--控件的id 2.android:layout_width--设置控件的宽度 wrap_content(包裹实际文本内容) fill_pa ...
- App控件定位:Android 控件介绍及元素定位方法
本文将分享Android相关基础知识和Android APP控件定位工具的使用方法. 目录 Android基础知识 Android布局 Android四大组件 1.activity 2.Service ...
- android控件触摸缩放,Android控件之ZoomControls缩放使用
先看一下效果 正常 缩小 放大 一.简介 ZoomControls是一组可缩放的控件.它包含俩个按钮(放大按钮.缩小按钮) 二.重要方法 hasFocus():判断焦点 hide():隐藏 onTou ...
- android设置控件的坐标,Android中使PopupWindow显示在指定控件的上下左右!
**1.View中的方法getLocationOnScreen(int[] location):获取该控件在屏幕中的绝对坐标并将坐标保存在数组中:如下图所示,A为屏幕的原点,R,C点即为view的绝对 ...
- java使用gridview,网格控件GridView在Android中的使用
我们在上面之前发表过一篇"使用Gallery实现缩略图浏览器",今天介绍另一种类似的控件,但是该控件是以网格的方式显示的,也就是说,当我们使用它来显示一组图片,结果图片将会以类似九 ...
- android开发控件水波纹,Android实现水波纹控件的方法
有很多app使用过水波纹的这样的效果,看着很酷酷的样子,所以自己就撸码写了一个. 实现思路: 利用贝塞尔曲线绘制圆弧(也就是水波的波纹) 通过动画改变绘制的起始点使水波纹平移 首先,定义我们需要的自定 ...
- android控件使用大全,Android常见控件使用详解
本文实例为大家分享了六种Android常见控件的使用方法,供大家参考,具体内容如下 1.TextView 主要用于界面上显示一段文本信息 2.Button 用于和用户交互的一个按钮控件 //为Butt ...
最新文章
- Matlab学习笔记——图形绘制
- Relation(NOIP模拟赛)(二分图染色)
- 让人难以置信的HTML5和JavaScript实验
- 以下各节已定义,但尚未为布局页“~/Views/_LayoutHome.cshtml”呈现:“mainContent; jsSrc”。...
- 苹果自研 M1 芯片性能强大,却不支持 Docker ?
- WSUS全攻略之二 :安装WSUS
- 常见花材的固定的方法有哪些_固定无梁拱形屋顶的方法都有哪些呢?
- mysql 三种循环语句_MySQL循环语句
- 十八掌教育_徐培成_Hadoop3.0-01.简介
- 将系统临时文件夹恢复到原来默认的目录
- oracle创建用户密码和权限--ORA-65096 invalid common user or role name
- 个人计算机更新主要基于,计算机基础知练识习题.doc
- STM32实战(1):搭建模板工程
- RPA or 爬虫?模拟人工访问某服务平台—增加空间的访问量
- 【GYM101409】2010-2011 ACM-ICPC, NEERC, Western Subregional Contest
- Android账号同步系统的建立——AccountManager及其他相关类的运用
- Golang 从入门到放弃
- 《 大型多人在线游戏开发》笔记
- fanuc系统md和mf区别_发那科 FANUC-MF 系统参数设定步骤(通过 MDI 进行设定)
- 讨论无穷积分的敛散性