实现简易秒表功能

2024-03-14 01:48
文章标签 实现 功能 简易 秒表

本文主要是介绍实现简易秒表功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


今天为了给师弟们讲安卓,花了10分钟写了一个简易的秒表app,现贴出代码,供各位刚入门以及还未入门的同胞们参考

第一步:布局activity_main.xml:

[html]  view plain copy
在CODE上查看代码片 派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity" >  
  10.   
  11.     <RelativeLayout  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_centerInParent="true" >  
  15.   
  16.         <LinearLayout  
  17.             android:id="@+id/top"  
  18.             android:layout_width="wrap_content"  
  19.             android:layout_height="wrap_content"  
  20.             android:layout_centerHorizontal="true"  
  21.             android:orientation="horizontal" >  
  22.   
  23.             <TextView  
  24.                 android:id="@+id/mint"  
  25.                 android:layout_width="wrap_content"  
  26.                 android:layout_height="wrap_content"  
  27.                 android:text="00"  
  28.                 android:textSize="30dp" />  
  29.   
  30.             <TextView  
  31.                 android:layout_width="wrap_content"  
  32.                 android:layout_height="wrap_content"  
  33.                 android:text=":"  
  34.                 android:textSize="30dp" />  
  35.   
  36.             <TextView  
  37.                 android:id="@+id/sec"  
  38.                 android:layout_width="wrap_content"  
  39.                 android:layout_height="wrap_content"  
  40.                 android:text="00"  
  41.                 android:textSize="30dp" />  
  42.         </LinearLayout>  
  43.   
  44.         <LinearLayout  
  45.             android:layout_width="wrap_content"  
  46.             android:layout_height="wrap_content"  
  47.             android:layout_below="@+id/top"  
  48.             android:layout_centerHorizontal="true"  
  49.             android:orientation="horizontal" >  
  50.   
  51.             <Button  
  52.                 android:id="@+id/start"  
  53.                 android:layout_width="wrap_content"  
  54.                 android:layout_height="wrap_content"  
  55.                 android:text="start" />  
  56.   
  57.             <Button  
  58.                 android:id="@+id/reset"  
  59.                 android:layout_width="wrap_content"  
  60.                 android:layout_height="wrap_content"  
  61.                 android:text="reset" />  
  62.         </LinearLayout>  
  63.     </RelativeLayout>  
  64.   
  65. </RelativeLayout>  



第二步,实现秒表功能

[java]  view plain copy
在CODE上查看代码片 派生到我的代码片
  1. package com.example.second;  
  2. import android.os.Bundle;  
  3. import android.os.Handler;  
  4. import android.os.Message;  
  5. import android.app.Activity;  
  6. import android.view.Menu;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.TextView;  
  11. public class MainActivity extends Activity {  
  12.     private TextView mint;  
  13.     private TextView sec;  
  14.     private Button start;  
  15.     private Button reset;  
  16.     private long timeusedinsec;  
  17.     private boolean isstop = false;  
  18.     private Handler mHandler = new Handler() {  
  19.         /* 
  20.          * edit by yuanjingchao 2014-08-04 19:10 
  21.          */  
  22.         @Override  
  23.         public void handleMessage(Message msg) {  
  24.             // TODO Auto-generated method stub  
  25.             super.handleMessage(msg);  
  26.             switch (msg.what) {  
  27.             case 1:  
  28.                 // 添加更新ui的代码  
  29.                 if (!isstop) {  
  30.                     updateView();  
  31.                     mHandler.sendEmptyMessageDelayed(11000);  
  32.                 }  
  33.                 break;  
  34.             case 0:  
  35.                 break;  
  36.             }  
  37.         }  
  38.   
  39.     };  
  40.   
  41.     @Override  
  42.     protected void onCreate(Bundle savedInstanceState) {  
  43.         super.onCreate(savedInstanceState);  
  44.         setContentView(R.layout.activity_main);  
  45.         initViews();  
  46.     }  
  47.     private void initViews() {  
  48.         mint = (TextView) findViewById(R.id.mint);  
  49.         sec = (TextView) findViewById(R.id.sec);  
  50.         reset = (Button) findViewById(R.id.reset);  
  51.         start = (Button) findViewById(R.id.start);  
  52.         reset.setOnClickListener(new OnClickListener() {  
  53.             @Override  
  54.             public void onClick(View arg0) {  
  55.                 // TODO Auto-generated method stub  
  56.               
  57.                 mint.setText("00");  
  58.                 sec.setText("00");  
  59.                 start.setText("start");  
  60.                 timeusedinsec=0;  
  61.                 isstop=true;  
  62.             }  
  63.         });  
  64.         start.setOnClickListener(new OnClickListener() {  
  65.             @Override  
  66.             public void onClick(View arg0) {  
  67.                 // TODO Auto-generated method stub  
  68.                 mHandler.removeMessages(1);  
  69.                 String aaa=start.getText().toString();  
  70.                 if(aaa.equals("start")){  
  71.                     mHandler.sendEmptyMessage(1);  
  72.                     isstop = false;  
  73.                     start.setText("pause");  
  74.                 }else {  
  75.                     mHandler.sendEmptyMessage(0);  
  76.                     isstop = true;  
  77.                     start.setText("start");  
  78.                 }  
  79.                   
  80.             }  
  81.         });  
  82.     }  
  83.     private void updateView() {  
  84.         timeusedinsec += 1;  
  85.         int minute = (int) (timeusedinsec / 60)%60;  
  86.         int second = (int) (timeusedinsec % 60);  
  87.         if (minute < 10)  
  88.             mint.setText("0" + minute);  
  89.         else  
  90.             mint.setText("" + minute);  
  91.         if (second < 10)  
  92.             sec.setText("0" + second);  
  93.         else  
  94.             sec.setText("" + second);  
  95.     }  
  96. }  

转自:http://blog.csdn.net/huanongjingchao/article/details/38374233

这篇关于实现简易秒表功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/806857

相关文章

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

golang版本升级如何实现

《golang版本升级如何实现》:本文主要介绍golang版本升级如何实现问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录golanwww.chinasem.cng版本升级linux上golang版本升级删除golang旧版本安装golang最新版本总结gola

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Mysql实现范围分区表(新增、删除、重组、查看)

《Mysql实现范围分区表(新增、删除、重组、查看)》MySQL分区表的四种类型(范围、哈希、列表、键值),主要介绍了范围分区的创建、查询、添加、删除及重组织操作,具有一定的参考价值,感兴趣的可以了解... 目录一、mysql分区表分类二、范围分区(Range Partitioning1、新建分区表:2、分

MySQL 定时新增分区的实现示例

《MySQL定时新增分区的实现示例》本文主要介绍了通过存储过程和定时任务实现MySQL分区的自动创建,解决大数据量下手动维护的繁琐问题,具有一定的参考价值,感兴趣的可以了解一下... mysql创建好分区之后,有时候会需要自动创建分区。比如,一些表数据量非常大,有些数据是热点数据,按照日期分区MululbU

MySQL中查找重复值的实现

《MySQL中查找重复值的实现》查找重复值是一项常见需求,比如在数据清理、数据分析、数据质量检查等场景下,我们常常需要找出表中某列或多列的重复值,具有一定的参考价值,感兴趣的可以了解一下... 目录技术背景实现步骤方法一:使用GROUP BY和HAVING子句方法二:仅返回重复值方法三:返回完整记录方法四:

IDEA中新建/切换Git分支的实现步骤

《IDEA中新建/切换Git分支的实现步骤》本文主要介绍了IDEA中新建/切换Git分支的实现步骤,通过菜单创建新分支并选择是否切换,创建后在Git详情或右键Checkout中切换分支,感兴趣的可以了... 前提:项目已被Git托管1、点击上方栏Git->NewBrancjsh...2、输入新的分支的

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方