android显示msg的方法,Android 显示实时时间的方法。

2966591fe80e1a10e2cc1e74d9babd7e.png0e3ea6bd97d7c2dd9d5683a911f062c5.png

一、利用

模拟时钟AnalogClock和数字时钟DigitalClock,用法功能很简单,就是显示一个模拟时钟或是数字时钟

两个时钟都不需要Java代码,只要在layout的xml里插入以下代码即可自动显示时间:

二、利用多线程自己用TextView来显示时间。

先实现Runnable接口,在run方法中获取系统时间,然后用生成Message,在用Handler对象发送消息,在hanler对象中处理消息,即获得系统时间,再显示出来。

实现runnable方法:

public void run()

{

try{

do

{

curTime = dateformat.format(new Date());

sysTime = System.currentTimeMillis();

Thread.sleep(1000);

Message msg = new Message();

msg.what=msg_Key;

mHandler.sendMessage(msg);

}while(tr.interrupted()==false);

}catch(InterruptedException e)

{

e.printStackTrace();

}

}

下面是定义Hanler对象:

mHandler = new Handler()

{

public void handleMessage(Message msg)

{

super.handleMessage(msg);

switch(msg.what)

{

case msg_Key:

tv.setText(“当前时间:  “+curTime+ “\n系统时间:”+sysTime+” ms”);

break;

default:

break;

}

}

};

最后,可以用不同的格式来显示时间:

dateformat = new SimpleDateFormat(“yyyy-mm-dd hh:mm:ss”); //设置显示格式

curTime = dateformat.format(new Date());   //获得该格式的时间

sysTime = System.currentTimeMillis();    //获得系统时间,ms

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值