<1>簡介
一個選擇年月日的日曆布局視圖
<2>類繼承
java.lang.Object
android.view.View
android.view.ViewGroup
android.widget.FrameLayout
android.widget.DatePicker
<3>方法
public int getDayOfMonth ()
擷取選擇的天數
public int getMonth ()
擷取選擇的月份。(注意:返回數值為0..11,需要自己+1來顯示)
public int getYear ()
擷取選擇的年份
public void init (int year, int monthOfYear, int dayOfMonth, DatePicker.OnDateChangedListener onDateChangedListener)
初始化狀態。(譯者註:初始化年月日)
參數
year 初始年(譯者註:注意使用new Date()初始化年時,需要+1900,如下:date.getYear()
+ 1900)
monthOfYear 初始月。
dayOfMonth 初始日。
onDateChangedListener
日期改變時通知使用者的事件監聽,可以為空白(null)。
public void setEnabled (boolean enabled)
設定視圖的啟用狀態。該啟用狀態隨子類的不同而有不同的解釋。
參數
enabled 設定為true表示啟動視圖,反之禁用。
public void updateDate (int year, int monthOfYear, int dayOfMonth)
更新日期
範例:
package android.test;import android.app.Activity;import android.os.Bundle;import android.widget.DatePicker;import android.widget.Toast;public class TestActivity extends Activity { /** Called when the activity is first created. */private DatePicker datepicker = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); datepicker = (DatePicker)findViewById(R.id.datepicker); String date = "年:"+datepicker.getYear()+"月:"+datepicker.getMonth()+"日:"+datepicker.getDayOfMonth(); Toast.makeText(TestActivity.this, date, Toast.LENGTH_SHORT).show(); }}
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="日期選取器(DatePicker)" /> <DatePicker android:id="@+id/datepicker" android:layout_width="fill_parent" android:layout_height="wrap_content" /></LinearLayout>