我已经创建了一个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); –