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

1.DatePicker和TimePicker两种实现动态输入日期和时间的功能

2.DatePickerDialog和TimePickerDialog两种实现动态输入日期和时间的对话框

3.两组监测日期和时间改变的监听器

(1)OnDateChangedListener()和OnTimeChangedListener()

当用户更改了DatePicker里的年、月、日时,将触发OnDateChangedListener监听器的onDateChanged()事件

当用户更改了TimePicker里的时、分时,将触发OnTimeChangedListener监听器的onTimeChanged()事件

(2)OnDateSetListener()和OnTimeSetListener

当用户更改了DatePickerDialog里的年、月、日时,将触发OnDateSetListener监听器的onDateSet()事件

当用户更改了TimePickerDialog里的时、分时,将触发OnTimeSetListener监听器的onTimeSet()事件


activity_main.xml




    

    



MainActivity.java

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.TimePicker;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    private TimePicker timePicker;
    private DatePicker datePicker;
    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)+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);

        datePicker = (DatePicker) findViewById(R.id.datePicker);
        timePicker = (TimePicker) findViewById(R.id.timePicker);

        //datePicker初始化
        datePicker.init(year, month-1, day, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                setTitle(year + "-" + (monthOfYear+1) +  "-" + dayOfMonth);
            }
        });

        //timePicker
        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                setTitle(hourOfDay + ":" + minute);
            }
        });

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

        //TimePickerDialog
        new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                setTitle(hourOfDay + ":" + minute);
            }
        },hour,minute,true).show();
    }
}

4.注意:DatePciker对象以init()这个方法来指定DatePicker初始的年、月、日及OnDateChangedListener()的事件;而TimePicker对象则是直接以setOnTimeChangedListener()事件来处理时间改变时程序要做的操作。




你可能感兴趣的:(Android)