Android 秒表
2012年10月28日,手机没有秒表,自己想做一个秒表来给自己用,现在马上做出一个实例来,这只是开始,以后做个界面漂亮的应用出来。废话不说,先上图:
源码:
建立项目:Stopwatch
代码清单:org/wwj/Stopwatch/Stopwatch.java
package org.wwj.Stopwatch;import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;public class Stopwatch extends Activity {private TextView minText; //分private TextView secText; //秒private Button start; //开始按钮private Button stop; //停止按钮private boolean isPaused = false;private String timeUsed;private int timeUsedInsec;private Handler uiHandle = new Handler(){public void handleMessage(android.os.Message msg) {switch(msg.what){case 1:if(!isPaused) {addTimeUsed();updateClockUI();}uiHandle.sendEmptyMessageDelayed(1, 1000);break;default: break;}}};@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_stopwatch);//获取界面的控件minText = (TextView) findViewById(R.id.min);secText = (TextView) findViewById(R.id.sec);start = (Button) findViewById(R.id.start);stop = (Button) findViewById(R.id.stop);//为按钮Start注册监听器start.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubuiHandle.removeMessages(1);startTime();isPaused = false;}});//为按钮stop注册监听器stop.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubisPaused = true;timeUsedInsec = 0;}});} @Overrideprotected void onPause() {// TODO Auto-generated method stubsuper.onPause();isPaused = true;}@Overrideprotected void onResume() {// TODO Auto-generated method stubsuper.onResume();isPaused = false;}private void startTime(){uiHandle.sendEmptyMessageDelayed(1, 1000);}/*** 更新时间的显示*/private void updateClockUI(){minText.setText(getMin() + ":");secText.setText(getSec());}public void addTimeUsed(){timeUsedInsec = timeUsedInsec + 1;timeUsed = this.getMin() + ":" + this.getSec();}public CharSequence getMin(){return String.valueOf(timeUsedInsec / 60);}public CharSequence getSec(){int sec = timeUsedInsec % 60;return sec < 10? "0" + sec :String.valueOf(sec);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.activity_stopwatch, menu);return true;}}
界面布局:res/layout/layout_stopwatch.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/LinearLayout1"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><LinearLayout android:orientation="horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"><TextView android:id="@+id/min"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="40sp"/><TextView android:id="@+id/sec"android:layout_width="wrap_content" android:layout_height="wrap_content"android:textSize="40sp"android:textColor="#ff0000"/></LinearLayout><LinearLayout android:orientation="horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"><Button android:id="@+id/start"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/start"/><Button android:id="@+id/stop"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/stop"/></LinearLayout>
</LinearLayout>
Android 秒表相关推荐
- Android 秒表 Demo 介绍
1. Android 秒表 Demo 代码主要从系统-时钟的秒表模块截取. 主要原理:View事件的PostRunable进行时间和UI的更新,不同于我们常用的Thread+延时, Handle + ...
- 使用Handler创建一个Android秒表应用
本文所有代码都放在以下链接中:https://github.com/MADMAX110/Stopwatch 0.应用是一个有活动.布局和其他资源组成的集合.其中一个活动是应用的主活动.每个应用都有一个 ...
- android 秒表倒计时
获取手机验证码,需要一个秒表倒计时.非常简单,可直接拿来用,代码如下: TimerTask task = new TimerTask() {@Override public void run() {r ...
- android秒表课程设计,电子秒表电路课程设计.doc
数字电子技术课程设计 题目 电子秒表电路课程设计 姓名: 学号: 专业班级: 指导老师: 日期: 一.设计目的:3 二.设计任务及说明:3 三. 功能要求3 四.总体方案及原理:3 五.电子秒表原理仿 ...
- android秒表课程设计,1258【毕设课设】基于单片机8155秒表计时器系统设计
/***********************基于8155的8LED显示电子表设计************************************************/ #include ...
- Android实用应用程序源码
andriod闹钟源代码 http://www.apkbus.com/android-20974-1-1.html android源码分享之指南针程序 http://www.apkbus.com/an ...
- android 精选文章
2019独角兽企业重金招聘Python工程师标准>>> [xml解析]学习笔记之SAX解析 解析XML文件时,有三种方式,其中SAX解析由于占用内存较小,比较适合Android开发中 ...
- Android项目大全
注:打开请贴网址,有些直接通过链接打开的不正确. 1.相对布局实例 http://kukuqiu.iteye.com/blog/1018396 2.Log图文详解(Log.v,Log.d,Log.i, ...
- 分享45个Android实例源码
分享45个Android实例源码,很好很强大.收藏吧!!! 分享45个android实例源码,很好很强大 http://www.apkbus.com/android-20978-1-1.html an ...
- 分享45个android实例源码,很好很强大
链接地址:http://www.apkbus.com/android-20978-1-1.html 分享45个android实例源码,很好很强大 http://www.apkbus.com/andro ...
最新文章
- /* * 编程第一题(20分): 	1+(1+2)+(1+2+3)+……+(1+2+3+……+98+99+100) */
- 使用基本MVC2模式创建新闻网站
- 关闭Windows不必要服务,电脑更安全
- (~解题报告~)L1-017 到底有多二 (15分)(16行代码AC)
- 再次实践 MySQL chart - 每天5分钟玩转 Docker 容器技术(166)
- 【Python基础】11_Python中的字符串
- centos mysql.tar.gz_centos7.2 mysql tar.gz 搭建 (亲测成功)
- git stash后怎么恢复_苹果换卡后怎么恢复通讯录?两种方法帮你解决
- bzoj 1415: [Noi2005]聪聪和可可
- Springboot整合junit单元测试
- php环境安装教程,PHP运行环境配置与开发环境的配置(图文教程)
- ArcGISPro通视分析之视线分析
- 什么是软件测试工程师
- 【linux内核-源码编译之centos7】
- java创建exe程序快捷方式
- 实战项目!用PHP实现的编程论坛网站
- 【不一样的面经】阿里面试,我挂在了第四轮……
- 为何家会伤人:2020-10-15早上
- 第30课时_电力MOSFET开关概述及工作原理
- 流行偶像Belinda Carlisle将在9月17日的Unite For Safe Care虚拟活动中献唱