很长时间没有写博客了,发现自己慢慢的便懒了,不想动,一个呢,是天气比较热,昨天还是39.5°呢,二来最近比较忙,今天有点闲暇时间,就把以前忘记的知识都整理出来,以便以后查阅,好了,说正题吧,我们知道,用System.currentTimeMillis()可以获取系统当前的时间,我们可以开启一个线程,然后通过handler发消息,来实时的更新TextView上显示的系统时间。
我们开启一个线程,线程每隔一秒发送一次消息,我们在消息中更新TextView上显示的时间就ok了。
首先我们在布局文件中放一个TextView用来显示时间,如下所示:
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout
3 xmlns:android="http://schemas.android.com/apk/res/android"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 android:background="@android:color/white">
7 <TextView
8 android:id="@+id/mytime"
9 android:layout_width="match_parent"
10 android:layout_height="match_parent"
11 android:gravity="center"
12 android:textColor="@android:color/black"
13 android:textSize="36sp"/>
14 </LinearLayout>