時間與日期選擇器——Mars Android開發視頻之第一季第十六集(重)

1· TimePicker的使用方法

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

輸入圖片說明

1.1 獲取用戶選擇的時間:

聲明成員變量: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

輸入圖片說明

1.2 實現使用24小時制的TimePicker:

很簡單:it

TimePicker.setIs24HourView(true);

1.3 實現點擊按鈕獲取TimePicker的時間:

在按鈕監聽器的監聽方法中使用TimePicker的get方法來獲取時間:

class ButtonClickListener implements OnClickListener {
		public void onClick(View v) {
			int hour = timePicker.getCurrentHour();
			int minute = timePicker.getCurrentMinute();
			System.out.println("獲取到的時間爲:" + hour + ":" + minute);
		}
	}

輸入圖片說明

2· DatePicker的使用方法

4.2版本的DatePicker:

輸入圖片說明

2.1 添加DatePicker

  • 聲明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);

2.2 如何獲取用戶選擇的日期

  • 點擊按鈕獲取日期

建立並實現按鈕監聽器類:

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 開始算起;

3· AnalogClock模擬時鐘

這是模擬時鐘: 輸入圖片說明

3.1 添加模擬時鐘

聲明:

<?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>

輸入圖片說明

4· 練習:

  • 添加數字時鐘:

輸入圖片說明

<?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>
相關文章
相關標籤/搜索