首页 > 代码库 > Android 动态显示时间
Android 动态显示时间
package code.example.newcodeview;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.text.format.DateFormat;import android.widget.TextView;import android.app.Activity;public class MainActivity extends Activity { private static final int msgKey = 1; private TextView txtTime; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); txtTime = (TextView) findViewById(R.id.txtSystemtime); new TimeThread().start(); } public class TimeThread extends Thread { @Override public void run() { do { try { Thread.sleep(1000); Message msg = new Message(); msg.what = msgKey; mHandler.sendMessage(msg); } catch (InterruptedException e) { e.printStackTrace(); } } while (true); } } private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case msgKey: long sysTime = System.currentTimeMillis(); CharSequence sysTimeStr = DateFormat .format("hh:mm:ss", sysTime); txtTime.setText("北京时间:" + sysTimeStr); break; default: break; } } };}
附图:
Android 动态显示时间
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。