Android 使用DatePicker以及TimePicker显示当前日期和时间

课程内容
1.介绍DatePicker和TimePicker两种实现动态输入日期和事件的功能
2.介绍DatePickerDialog和TimePickerDialog来年耕种实现动态输入日期和事件的对话框
3.介绍两组针对检测日期时间改变的监听器包含:
 onDateChangedListener和OnTimeChangedListener
 onDateSetListener和OnTimeSetListener
当用户更改了DatePicker里的年、月、日时,将出发OnDateCHangedListener监听器的onDateChange()事件
当用户更改了TimePicker里的时、分时,将出发OnTimeCHangedListener监听器的onTimeChanged事件
当用户更改了DatePickerDialog里的年、月、日时,将出发OnDateSetListener监听器的onDateSet()事件
当用户更改了TimePickerDialog里的时、分时,将出发OnTimeSetListener监听器的onTimeSet()事件

需求功能
默认在标题栏中展示当前日期和事件,通过监听器去获取用户选择日期或事件的结果,并将变更的日期或时间展示在标题栏上
通过setTitle()设置标题。

DatePicker实时更改年、月、日:

        datePicker = (DatePicker) findViewById(R.id.datePicker1);
        datePicker.init(year, month, day, new OnDateChangedListener() {

            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                setTitle(String.format("%d-%02d-%02d", year, monthOfYear+1, dayOfMonth));
            }
            
        });

TimePicker实时更新时、分:

        timePicker = (TimePicker) findViewById(R.id.timePicker1);
        timePicker.setOnTimeChangedListener(new OnTimeChangedListener() {
            
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                setTitle(String.format("%02d:%02d", hourOfDay, minute));
            }
        });

新建一个DatePickerDialog用于展示:

        new DatePickerDialog(this, new OnDateSetListener() {
            
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                setTitle(String.format("%d-%02d-%02d", year, monthOfYear+1, dayOfMonth));
            }
        }, year, month, day).show();

新建一个TimePickerDialog用于展示:

        new TimePickerDialog(this, new OnTimeSetListener() {

            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                setTitle(String.format("%02d:%02d", hourOfDay, minute));
            }
            
        }, hour, minute, true).show();
package com.example.datepickertimepicker;

import java.util.Calendar;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

public class MainActivity extends ActionBarActivity {
    
    private DatePicker datePicker;
    private TimePicker timePicker;
    private Calendar calendar;
    private int year;
    private int month;
    private int day;
    private int hour;
    private int minute;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH);
        day = calendar.get(Calendar.DAY_OF_MONTH);
        hour = calendar.get(Calendar.HOUR_OF_DAY);
        minute = calendar.get(Calendar.MINUTE);
        setTitle(String.format("%d-%02d-%02d %02d:%02d", year, month+1, day, hour, minute));
        
        datePicker = (DatePicker) findViewById(R.id.datePicker1);
        datePicker.init(year, month, day, new OnDateChangedListener() {

            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                setTitle(String.format("%d-%02d-%02d", year, monthOfYear+1, dayOfMonth));
            }
            
        });
        
        timePicker = (TimePicker) findViewById(R.id.timePicker1);
        timePicker.setOnTimeChangedListener(new OnTimeChangedListener() {
            
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                setTitle(String.format("%02d:%02d", hourOfDay, minute));
            }
        });
        
        new DatePickerDialog(this, new OnDateSetListener() {
            
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                setTitle(String.format("%d-%02d-%02d", year, monthOfYear+1, dayOfMonth));
            }
        }, year, month, day).show();
        
        new TimePickerDialog(this, new OnTimeSetListener() {

            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                setTitle(String.format("%02d:%02d", hourOfDay, minute));
            }
            
        }, hour, minute, true).show();
    }
}
MainActivity.java
<LinearLayout 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"
    android:orientation="vertical">

    <DatePicker
        android:id="@+id/datePicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TimePicker
        android:id="@+id/timePicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
activity_main.xml

注意事项:
1.DatePicker对象以init()这个方法来指定DatePicker初始的年、月、日及OnDateCHangedListener()的事件;而TimePicker对象则是直接以setOnTimeCHangedListener()事件来处理事件改变程序要做的操作。
2.DatePickerDialog与TimePickerDialog与前面这两种类型的对象最大的差别在于DatePicker与TImePicker是直接显示在屏幕画面上,而DatePickerDialog与TimePickerDialog对象则是以弹出Dialog的方式来显示。

效果:

         

 

posted @ 2016-04-20 16:46  月光诗人  阅读(5333)  评论(0编辑  收藏  举报