<LinearLayoutxmlns: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"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin" > <TextViewAndroid:id= "@+id/tv"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Please enter your birthday ..." /> <!--define a DatePicker control, set the ID property to facilitate the acquisition of objects in a Java file - <DatePickerAndroid:id= "@+id/dp"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content" /></LinearLayout>
Packagecom.example.yanlei.yl2;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.widget.DatePicker;ImportAndroid.widget.DatePicker.OnDateChangedListener;ImportAndroid.widget.TextView;//Import the required packages Public classMainactivityextendsappcompatactivity {PrivateTextView Tv;//Define result text labels PrivateDatePicker Dp;//define date Get control//define mainactivity inherit from activity@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);//call the OnCreate method of the parent class//set the current page's layout file to Activity_main by using the Setcontentview methodSetcontentview (R.layout.activity_main); Findview (); //Get Control ObjectSetlistener ();//setting up a DatePicker listener } Private voidSetlistener () {//TODO auto-generated Method Stub//initializes the DatePicker object and sets the listener for the date changeDp.init (1990, 10, 12,NewOndatechangedlistener () {//callback Ondatechanged method when the date of DP is changed@Override Public voidOndatechanged (DatePicker view,intYearintMonthofyear,intdayofmonth) { //TODO auto-generated Method Stub//Gets the value of the date and month of the DP, shown in TextViewTv.settext ("Year:" +dp.getyear () + "\nmonth:" + (Dp.getmonth () +1) + "\nday:" +dp.getdayofmonth ()); } }); } Private voidFindview () {//TODO auto-generated Method Stub//get the corresponding control object by FindviewbyidTv =(TextView) Findviewbyid (r.id.tv); Dp=(DatePicker) Findviewbyid (R.ID.DP); }}
Android DatePicker use