最近,在做一个Android项目,需要用到一个计时器。特此,拿来和大家分享一下。
首先,看一下实现效果图。
实现思路
主要利用Handler
中的postDelayed (Runnable r, long delayMillis)
函数每隔1秒钟更新一次UI。
具体实现如下。
布局文件
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.caltimer.MainActivity" >
<TextView
android:id="@+id/tv_timer"
android:layout_width="match_parent"