原CSDN博客已弃用,文章会逐渐迁移过来。

转帖请注明本文出自weimeig的博客(https://blog.csdn.net/weimeig/article/details/79666458),请尊重他人的辛勤劳动成果,谢谢

应朋友们反馈的Android基础薄弱的问题,决定出一套Android基础教程,帮助大家复习,巩固Android基础,今天要讲的是Android中的DatePicker和TimePicker时间和日期选择器。

xml文件

<?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">  

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

    </TimePicker>  
    <DatePicker  
        android:id="@+id/datePicker_tp"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content">  
    </DatePicker>  
    <TextView  
        android:id="@+id/textView_tv"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="显示当前日期"/>  
</LinearLayout>  

Java代码

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

import java.util.Calendar;  

public class MainActivity extends AppCompatActivity  {  

    private TimePicker mTimePicker;  
    private DatePicker mDatePicker;  
    private TextView mTextView;  
    private int Year,Month,Day,Hour,Minute;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        initView();  
        getCurrentTime();  
    }  
    private void setPicker(){  
        //设置时间选择器为24小时制  
        mTimePicker.setIs24HourView(true);  
        //设置时间选择器为当前时间  
        mTimePicker.setCurrentHour(Hour);//小时  
        mTimePicker.setCurrentMinute(Minute);//分钟  
        mTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {  
            @Override  
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {  
                //TimePicker 对象  hourOfDay 小时  minute 分钟  
                //当时间选择器发生改变时触发  
                System.out.println("From TimePicker:" +hourOfDay + ":" +minute);  
            }  
        });  
        mDatePicker.init(Year, Month, Day, new DatePicker.OnDateChangedListener() {  
            @Override  
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {  
                //当日期选择器发生改变时触发  
                System.out.println("From DatePicker:" + year + "/" +(monthOfYear+1) +"/" +dayOfMonth);//国外计时方法月份从0开始算,因此要在月份后面+1  
            }  
        });  
    }  
    private void getCurrentTime() {  
        //获取系统当前时间  
        Calendar 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);  
        Minute = calendar.get(Calendar.MINUTE);  
    }  

    private void initView() {  
        mTimePicker = (TimePicker) findViewById(R.id.timePicker_tp);  
        mDatePicker = (DatePicker) findViewById(R.id.datePicker_tp);  
        mTextView = (TextView) findViewById(R.id.textView_tv);  
    }  
}