DatePicker也是Android自带的日期选择控件,使用这个控件的时候主要注意以下几点:
1.DatePicker无法实现日期选择监听
2.java的月份默认是从0开始,所以我们在得到月份信息的时候要加1
配置文件:
<span style="white-space:pre"> </span><DatePicker
android:id="@+id/dp_first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tp_first"/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:layout_below="@id/dp_first"/>
主Activity中,对button设置监听:
public void onClick(View v) {
// TODO Auto-generated method stub
int y=dp_first.getYear();
int month=dp_first.getMonth()+1;
int day=dp_first.getDayOfMonth();
Toast.makeText(mContext, "year:"+y+"month:"+month+"day:"+day, Toast.LENGTH_SHORT).show();
}
OK,基本功能已实现,至于效果,如果我们在具体需要的时候再来探索!