【Android 应用开发】Android - 时间 日期相关组件
源碼下載地址 :
-- CSDN : ?http://download.csdn.net/detail/han1202012/6856737?
-- GitHub :?https://github.com/han1202012/Timer_Date_Test.git
.
作者?:萬(wàn)境絕塵?
轉(zhuǎn)載請(qǐng)注明出處??:?http://blog.csdn.net/shulianghan/article/details/18314667
.
一. 時(shí)鐘組件
1. AnalogClock組件
外觀(guān) : 該組件顯示一個(gè)表盤(pán), 有分針和秒針轉(zhuǎn)動(dòng);
屬性介紹 : 該組件可以設(shè)置表盤(pán)圖片, 時(shí)針 分針顯示圖片;
-- 設(shè)置表盤(pán) : android:dial, 設(shè)置R.drawable資源圖片;
-- 設(shè)置時(shí)針 : android:hand_hour, 設(shè)置R.drawable資源圖片;
-- 設(shè)置分針 : android:hand_minute, 設(shè)置R.drawable資源圖片;
2. DigitalClock組件
外觀(guān) : 該組件就是一個(gè)TextView組件, 顯示的是當(dāng)前時(shí)間的文本;
屬性 : 該組件不能設(shè)置android:text屬性, 設(shè)置了也無(wú)效;
3. 源碼示例
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><!-- 數(shù)字時(shí)鐘, 可以設(shè)置字體大小顏色 --><DigitalClock android:layout_height="wrap_content"android:layout_width="wrap_content"android:textSize="20pt"android:textColor="#DF0101"/><TextView android:layout_height="20dp"android:layout_width="fill_parent"android:background="#01DF01"/><!-- 模擬時(shí)鐘, 可設(shè)置表盤(pán), 分針, 時(shí)針的針腳 --><AnalogClock android:layout_width="200dp"android:layout_height="200dp"android:dial="@drawable/clock"/></LinearLayout>
效果圖 :?
二. 計(jì)時(shí)器Chronometer
格式屬性 : android:format, 指定計(jì)時(shí)器的計(jì)時(shí)格式;
常用方法 : 計(jì)時(shí)器的方法是重點(diǎn)所在, 可以控制計(jì)時(shí)器開(kāi)始, 停止等動(dòng)作;
-- 設(shè)置時(shí)間 : setBase(long), 設(shè)置起始時(shí)間;
-- 設(shè)置格式 : setFormat(string), 設(shè)置時(shí)間顯示格式;
-- 開(kāi)始計(jì)時(shí) : start(), 開(kāi)始計(jì)時(shí)方法;
-- 停止計(jì)時(shí) : stop(), 停止計(jì)時(shí)方法;
-- 設(shè)置監(jiān)聽(tīng) : setOnChronometerTickListener(), 設(shè)置一個(gè)監(jiān)聽(tīng)器, 當(dāng)計(jì)時(shí)器計(jì)時(shí)變化的時(shí)候回調(diào)這個(gè)方法;
實(shí)例 :?
XML源碼 :?
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><Chronometer android:id="@+id/chronometer"android:layout_height="wrap_content"android:layout_width="wrap_content"android:textSize="20pt"android:textColor="#DF0101"/><!-- checked為true的時(shí)候, 顯示開(kāi)始計(jì)時(shí), 此時(shí)沒(méi)有計(jì)時(shí) --><ToggleButton android:id="@+id/toggle"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="15pt"android:textOn="開(kāi)始計(jì)時(shí)"android:textOff="停止計(jì)時(shí)"android:checked="true"/></LinearLayout>
Activity代碼 :?
package shuliang.han.time_date_test;import android.app.Activity; import android.os.Bundle; import android.os.SystemClock; import android.widget.Chronometer; import android.widget.Chronometer.OnChronometerTickListener; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.Toast; import android.widget.ToggleButton;public class ChronometerActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.chronometer);final Chronometer chronometer = (Chronometer) findViewById(R.id.chronometer);ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggle);toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if(isChecked){//從 顯示 停止計(jì)時(shí) 點(diǎn)擊, checked false -> true 停止計(jì)時(shí), 顯示 開(kāi)始計(jì)時(shí)chronometer.stop();}else{// 顯示開(kāi)始計(jì)時(shí) 點(diǎn)擊, checked true -> false 開(kāi)始計(jì)時(shí), 顯示 停止計(jì)時(shí)chronometer.start();chronometer.setBase(SystemClock.elapsedRealtime());}}});//設(shè)置一個(gè)監(jiān)聽(tīng)器, 當(dāng)超過(guò)5秒計(jì)時(shí)的時(shí)候Toast信息chronometer.setOnChronometerTickListener(new OnChronometerTickListener() {@Overridepublic void onChronometerTick(Chronometer chronometer) {if(SystemClock.elapsedRealtime() - chronometer.getBase() > 5 * 1000)Toast.makeText(getApplicationContext(), "5秒了", Toast.LENGTH_LONG).show();}});}}
效果圖 :?
.
作者?:萬(wàn)境絕塵?
轉(zhuǎn)載請(qǐng)注明出處?:?http://blog.csdn.net/shulianghan/article/details/18314667
.
三. 日歷視圖CalendarView
日歷視圖 : 日歷視圖顯示了一個(gè)7 * N 的方格, 即日歷, N可以設(shè)置, 通過(guò)滾動(dòng)視圖, 可以選擇其他月份年份的日期, 同時(shí)也可以設(shè)置日期改變監(jiān)聽(tīng)器, 監(jiān)聽(tīng)日歷選擇事件;
日歷視圖的XML屬性 :?
-- 設(shè)置樣式 : android:dateTextAppearance, 設(shè)置日期文字顯示樣式;
-- 設(shè)置首日 : android:firstDayOfWeek, 設(shè)置星期幾是每周的第一天, 默認(rèn)是周一;
-- 選中顏色 : android:focusedMonthDateColor, 設(shè)置選中日期所在月份日期顏色;
-- 最大日期 : android:maxDate, 設(shè)置支持的最大日期, 以 mm/dd/yyyy 格式指定;
-- 最小日期 : android:minDate, 設(shè)置支持的最小日期, 以 mm/dd/yyyy 格式指定;
-- 選中豎線(xiàn) : android:selectedDateVerticalBar, 設(shè)置被選中日期兩邊的豎線(xiàn)Drawable, 即R.drawable.int資源;
-- 選周顏色 : android:selectedWeekBackground, 設(shè)置被選中日期所在周的背景顏色;
-- 周數(shù)顯示 : android:showWeekNumber, 設(shè)置是否顯示周數(shù);
-- 設(shè)置周數(shù) : android:shownWeekCount, 設(shè)置該日歷組件一共顯示幾周;
-- 未選顏色 : android:unfocusedMonthDateColor, 設(shè)置未被選中的月份的日期顏色;
-- 星期樣式 : android:weekDayTextAppearance, 設(shè)置星期幾的文字樣式;
-- 周號(hào)顏色 : android:weekNumberColor, 設(shè)置周編號(hào)的顏色;
-- 周分割色 : android:weekSeparatorLineColor, 設(shè)置周分隔線(xiàn)顏色;
實(shí)例 :?
XML源碼 :?
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextView android:layout_height="wrap_content"android:layout_width="fill_parent"android:text="日歷視圖"/><!-- android:firstDayOfWeek 屬性, 設(shè)置星期幾是一周的開(kāi)始android:shownWeekCount 屬性, 設(shè)置顯示幾個(gè)星期的日歷android:selectedWeekBackgroundColor 屬性, 設(shè)置當(dāng)前選中日期所在的星期背景顏色android:focusedMonthDateColor 屬性, 顯示當(dāng)前選中月份的日期顏色, 在這個(gè)日歷中可能同時(shí)顯示2個(gè)月份的日歷android:weekSeparatorLineColor 屬性, 設(shè)置將日期分開(kāi)的線(xiàn)條顏色android:unfocusedMonthDateColor 屬性, 設(shè)置沒(méi)有選中的月份日期顏色--><CalendarView android:layout_width="match_parent"android:layout_height="match_parent"android:firstDayOfWeek="7"android:shownWeekCount="4"android:selectedWeekBackgroundColor="#aff"android:focusedMonthDateColor="#f00"android:weekSeparatorLineColor="#ff0"android:unfocusedMonthDateColor="#f9f"android:id="@+id/calendarView" /></LinearLayout>
Activity源碼 :?
package shuliang.han.time_date_test;import android.app.Activity; import android.os.Bundle; import android.widget.CalendarView; import android.widget.CalendarView.OnDateChangeListener; import android.widget.Toast;public class CalendarActivity extends Activity {private CalendarView calendarView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.calendar);calendarView = (CalendarView) findViewById(R.id.calendarView);//設(shè)置日期改變監(jiān)聽(tīng)器, 日期改變的時(shí)候激活該監(jiān)聽(tīng)器calendarView.setOnDateChangeListener(new OnDateChangeListener() {@Overridepublic void onSelectedDayChange(CalendarView view, int year, int month,int dayOfMonth) {Toast.makeText(getApplicationContext(), "選擇的日期是 : " + year +" 年"+ month + " 月 " + dayOfMonth + "日", Toast.LENGTH_LONG).show();}});} }
效果圖 :?
四. 時(shí)間選擇器
TimePicker可以供用戶(hù)選擇時(shí)間, 組件比較美觀(guān), 還可以設(shè)置時(shí)間改變監(jiān)聽(tīng)器, 一旦時(shí)間改變, 就會(huì)觸發(fā)回調(diào)方法;
實(shí)例源碼 :?
XML文件 :?
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextView android:layout_height="wrap_content"android:layout_width="wrap_content"android:text="時(shí)間選擇"/><TimePicker android:id="@+id/time_picker"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"/></LinearLayout>
Activity源碼 :?
package shuliang.han.time_date_test;import java.util.Calendar;import android.app.Activity; import android.os.Bundle; import android.widget.DatePicker; import android.widget.TimePicker; import android.widget.TimePicker.OnTimeChangedListener; import android.widget.Toast;public class TimePickerActivity extends Activity {private TimePicker timePicker;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.time_picker);timePicker = (TimePicker) findViewById(R.id.time_picker);//打印當(dāng)前時(shí)間getCurrentDateTime();timePicker.setOnTimeChangedListener(new OnTimeChangedListener() {@Overridepublic void onTimeChanged(TimePicker view, int hourOfDay, int minute) {Toast.makeText(getApplicationContext(), "改變時(shí)間 : " + hourOfDay + "時(shí)" + minute + "分", Toast.LENGTH_LONG).show();}});}/** 獲取當(dāng)前的日期和時(shí)間, 并將日期時(shí)間Toast出來(lái)*/private void getCurrentDateTime() {//1. 獲取當(dāng)前日歷Calendar calendar = Calendar.getInstance();//2. 獲取時(shí)間日期方法 : calendar.get(Calendar.YEAR)Toast.makeText(getApplicationContext(), "當(dāng)前時(shí)間 : " + calendar.get(Calendar.YEAR) + "年" + calendar.get(Calendar.MONTH) + "月" + calendar.get(Calendar.DATE) + "日" + calendar.get(Calendar.HOUR) + "時(shí)" + calendar.get(Calendar.MINUTE) + "分" + calendar.get(Calendar.SECOND) + "秒", Toast.LENGTH_LONG).show();} }
效果圖 :?
五. 日期選擇器DatePicker
日期選擇器常用屬性 :?
-- 顯示日歷 : android:calendarViewShown, 是否顯示CalendarView日歷組件;
-- 選擇最后 : android:endYear, 該選擇器是否允許選擇最后一年;
-- 最大日期 : android:maxDate, 設(shè)置日期選擇器的最大日期, 格式 mm/dd/yyyy;
-- 最小日期 : android:minDate, 設(shè)置日期選擇器的最小日期, 格式 mm/dd/yyyy;
-- 選擇組件 : android:spinnerShown, 是否顯示Spinner組件;
-- 選擇首年 : android:startYear, 是否允許選擇首年;
實(shí)例:
XML源碼 :?
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><!-- android:startYear 屬性 : 設(shè)置可選擇日期的開(kāi)始年份android:endYear 屬性 : 設(shè)置可選擇日期的結(jié)束年份android:calendarViewShown 屬性 : 設(shè)置是否顯示CalendarView組件android:spinnersShown 屬性 : 設(shè)置是否顯示--><DatePicker android:id="@+id/date_picker"android:layout_height="wrap_content"android:layout_width="wrap_content"android:layout_gravity="center_horizontal"android:startYear="2000"android:endYear="2020"android:calendarViewShown="true"android:spinnersShown="true"/></LinearLayout>
?
.
作者?:萬(wàn)境絕塵?
轉(zhuǎn)載請(qǐng)注明出處? :?http://blog.csdn.net/shulianghan/article/details/18314667
.
源碼下載地址?:
-- CSDN : ?http://download.csdn.net/detail/han1202012/6856737?
-- GitHub :?https://github.com/han1202012/Timer_Date_Test.git
總結(jié)
以上是生活随笔為你收集整理的【Android 应用开发】Android - 时间 日期相关组件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【Android 应用开发】Androi
- 下一篇: 【Android 应用开发】Androi