TimePicker來展示時(shí)分秒
DatePicker來展示年月日
代碼實(shí)現(xiàn)
- 頁面布局的實(shí)現(xiàn)
<?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"
>
<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
- 功能的實(shí)現(xiàn)
datePicker=(DatePicker) findViewById(R.id.datePicker);
timePicker=(TimePicker) findViewById(R.id.timePicker);
//獲取一個(gè)日歷對象
calendar=Calendar.getInstance();
//獲取年月日時(shí)分秒的信息
year=calendar.get(Calendar.YEAR);
// //它是從零開始計(jì)月份的
month=calendar.get(Calendar.MONTH)+1;
day=calendar.get(Calendar.DAY_OF_MONTH);
hour=calendar.get(Calendar.HOUR_OF_DAY);
minute=calendar.get(Calendar.MINUTE);
setTitle(year+":"+month+":"+day+":"+hour+":"+minute);
對時(shí)間的設(shè)置進(jìn)行初始化 使用OnDateChangedListener來監(jiān)聽時(shí)間的變化進(jìn)行顯示
//初始化操作
datePicker.init(year, calendar.get(Calendar.MONTH), day, new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
setTitle(year+":"+(monthOfYear+1)+":"+dayOfMonth);
}
});
對時(shí)分秒的設(shè)置進(jìn)行初始化 使用OnTimeChangedListener來監(jiān)聽時(shí)間的變化進(jìn)行顯示
timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
setTitle(hourOfDay+":"+minute);
}
});
我們一對話框的形式
*展示時(shí)間 使用 OnDateSetListener來監(jiān)聽時(shí)間的變化進(jìn)行顯示
new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
setTitle(year+":"+month+":"+dayOfMonth);
}
},year,calendar.get(Calendar.MINUTE),day).show();
*展示時(shí)分 使用 OnTimeSetListener來監(jiān)聽時(shí)間的變化進(jìn)行顯示
//以對話框的形式展示時(shí)分秒
new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
setTitle(hourOfDay+":"+minute);
}
},hour,minute,true).show();