在布局文件中放一個TextView用來顯示時間,如下所示:
創(chuàng)新互聯(lián)專注于睢縣企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城開發(fā)。睢縣網(wǎng)站建設(shè)公司,為睢縣等地區(qū)提供建站服務(wù)。全流程定制網(wǎng)站開發(fā),專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
<?xml version="1.0" encoding="utf-8"?>
開啟一個線程,然后通過handler發(fā)消息,來實時的更新TextView上顯示的系統(tǒng)時間:
import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.text.format.DateFormat; import android.widget.TextView; public class TestActivity extends Activity { private static final int msgKey1 = 1; private TextView mTime; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.time); mTime = (TextView) findViewById(R.id.mytime); new TimeThread().start(); } public class TimeThread extends Thread { @Override public void run () { do { try { Thread.sleep(1000); Message msg = new Message(); msg.what = msgKey1; 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 msgKey1: mTime.setText(getTime()); break; default: break; } } }; //獲得當(dāng)前年月日時分秒星期 public String getTime(){ final Calendar c = Calendar.getInstance(); c.setTimeZone(TimeZone.getTimeZone("GMT+8:00")); String mYear = String.valueOf(c.get(Calendar.YEAR)); // 獲取當(dāng)前年份 String mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 獲取當(dāng)前月份 String mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 獲取當(dāng)前月份的日期號碼 String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK)); String mHour = String.valueOf(c.get(Calendar.HOUR_OF_DAY));//時 String mMinute = String.valueOf(c.get(Calendar.MINUTE));//分 String mSecond = String.valueOf(c.get(Calendar.SECOND));//秒 if("1".equals(mWay)){ mWay ="天"; }else if("2".equals(mWay)){ mWay ="一"; }else if("3".equals(mWay)){ mWay ="二"; }else if("4".equals(mWay)){ mWay ="三"; }else if("5".equals(mWay)){ mWay ="四"; }else if("6".equals(mWay)){ mWay ="五"; }else if("7".equals(mWay)){ mWay ="六"; } return mYear + "年" + mMonth + "月" + mDay+"日"+" "+"星期"+mWay+" "+mHour+":"+mMinute+":"+mSecond; } }
以上所述是小編給大家介紹的Android動態(tài)顯示當(dāng)前年月日時分秒系統(tǒng)時間,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!