TimePicker的樣子(4.2版本): java
下面,咱們來添加一個TimePicker:android
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TimePicker android:id="@+id/timePicker_1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
這樣,就添加上了:git
聲明成員變量:code
private TimePicker timePicker;
接着獲取這個控件:xml
timePicker = (TimePicker) findViewById(R.id.timePicker_1);
而後實現TimePicker的監聽器:對象
當TimePicker的時間發生改變時,就會調用這個監聽器; 參數timePicker爲所操做的TimePicker對象圖片
class TimePickerListener implements OnTimeChangedListener { public void onTimeChanged(TimePicker timePicker, int hours, int minute) { System.out.println("當前選擇的時間是:" + hours + ":" + minute); } }
接着生成監聽器對象,並掛接好:utf-8
TimePickerListener listener = new TimePickerListener(); timePicker.setOnTimeChangedListener(listener);
此時當TimePicker的時間發生改變時,就會打印出改變的時間:get
很簡單:it
TimePicker.setIs24HourView(true);
在按鈕監聽器的監聽方法中使用TimePicker的get方法來獲取時間:
class ButtonClickListener implements OnClickListener { public void onClick(View v) { int hour = timePicker.getCurrentHour(); int minute = timePicker.getCurrentMinute(); System.out.println("獲取到的時間爲:" + hour + ":" + minute); } }
4.2版本的DatePicker:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <DatePicker android:id="@+id/datePicker" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/btn_GetDate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/datePicker" android:text="Get Date"/> </RelativeLayout>
private DatePicker datePicker; private Button btn_GetDate;
btn_GetDate = (Button) findViewById(R.id.btn_GetDate); datePicker = (DatePicker) findViewById(R.id.datePicker);
建立並實現按鈕監聽器類:
class ButtonClickListener implements OnClickListener { public void onClick(View v) { //獲取日期時間 int year = datePicker.getYear(); int month = datePicker.getMonth() + 1; int day = datePicker.getDayOfMonth(); System.out.println("獲取到的日期爲:" + year + "/" + month + "/" +day); } }
ButtonClickListener btnListener = new ButtonClickListener(); btn_GetDate.setOnClickListener(btnListener);
如今點擊按鈕,就能獲取用戶選擇的日期:
設置DatePicker的默認日期
datePicker.updateDate(2013, 4, 2);
注意 java中,月份從0 開始算起;
這是模擬時鐘:
聲明:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <AnalogClock android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <DigitalClock android:id="@+id/digitalClock1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="DigitalClock" /> </RelativeLayout>