我已經創建了一個TextClock小部件來顯示運行時間。如何在android中使用小部件TextClock獲取當前日期?
在佈局
<TextClock
android:id="@+id/timerr"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:textSize="50sp"
android:layout_height="wrap_content"
/>
在活動,創建的參考
textClock = (TextClock) findViewById(R.id.timerr);
兩個12小時,24小時
設置好的格式格式
對於24小時
textClock.setFormat12Hour(null);
textClock.setFormat24Hour("HH:mm:ss");
爲12小時,
textClock.setFormat12Hour("hh:mm:ss a");
textClock.setFormat24Hour(null);
現在,我需要獲得當前日期,而隨着時間而點擊的按鈕,但我需要只顯示時間不是在UI的日期。
有可能得到日期隨着時間,有人幫助PLZ .. 在此先感謝。
這是我的UI看起來如何?
http://i.stack.imgur.com/oQ5VR.png
如果你想要當前時間,你不能只是得到一個日曆立場,然後確定TextClock的參數? – JoxTraex
無法從TextClock小部件獲取當前日期,因爲有方法可以獲取。相反,你可以使用日曆對象。 https://developer.android。com/reference/android/widget/TextClock.html –
Date now = new Date(); Date alsoNow = Calendar.getInstance()。getTime(); String nowAsString = new SimpleDateFormat(「yyyy-MM-dd」)。format(now); –