DatePicker class Diagram
Main methods
public void init (int year, int monthofyear, int dayofmonth, datepicker.ondatechangedlistener Ondatechangedlistener)
Class Timepicker class diagram
setontimechangedlistenerpublic void Setontimechangedlistener (Timepicker.ontimechangedlistener Ontimechangedlistener)
This paper mainly implements a DatePicker and timepicker combination of time selector, for subsequent SMS timing send time to do foreshadowing.
Words don't say much, on the code.
See Main.xml file First
<relativelayout 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:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "> <button android:id=" @+id/datebutton "android:layout_width=" Match_parent " android:layout_height= "40dip" android:layout_margintop= "20dip" android:text= "Year-month-day" Android : textsize= "18dip" android:background= "@drawable/bg" android:onclick= "Picksenddate"/> <b Utton android:id= "@+id/timebutton" android:layout_below= "@id/datebutton" android:layout_width= "Match_ Parent "android:layout_height=" 40dip "android:layout_margintop= "1dip" android:text= "Hour:minute" android:textsize= "18dip" Android:backgroun d= "@drawable/bg" android:onclick= "Picksendtime"/> <datepicker android:id= "@+id/datepick Er "android:layout_below=" @id/timebutton "android:layout_width=" Wrap_content "android:layout_height=" Wrap_content "android:layout_centerhorizontal=" true "android:layout_margintop=" 20DP "Android:startyea R= "android:endyear=" 2020 "android:visibility=" Gone "/> <timepicker Android Oid:id= "@+id/timepicker" android:layout_below= "@id/timebutton" android:layout_width= "Wrap_content" an droid:layout_height= "Wrap_content" android:layout_centerhorizontal= "true" android:layout_margintop= "20DP" Android:visibility= "Gone"/></relativelayout>
Use the relative layout as a whole, and note the android:layout_centerhorizontal= "true" when you want to center the control horizontally. Instead of android:layout_gravity= "center_vertical" (This is the way the LinearLayout layout works). DatePicker can be set to a minimum and maximum age, very convenient. The Listener for the button control is not setonclickbuttonlistener in code, use Android:onclick directly, followed by the method name. (available in two different ways)
There is hard coding in the code, the actual project to note, please ignore here.
Main interface Mainactivity
Mainactivity.java
Package Com.beny.pickdate;import Java.text.simpledateformat;import Java.util.date;import android.app.Activity; Import Android.os.bundle;import android.view.menu;import Android.view.view;import Android.widget.button;import Android.widget.datepicker;import android.widget.datepicker.ondatechangedlistener;//Monitor Date change import Android.widget.timepicker;import android.widget.timepicker.ontimechangedlistener;//Monitor time change public class Mainactivity extends Activity {private int year; private int month; private int day; private int hour; private int minute; Private Timepicker timepicker = null; Private DatePicker DatePicker = null; Private Button Timebutton = null; Private Button Datebutton = null; Protected string[] time = null; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Timebutton = (Button) Findviewbyid (R.id.timebutton); Datebutton = (BUtton) Findviewbyid (R.id.datebutton); Timepicker = (timepicker) Findviewbyid (R.id.timepicker); Timepicker.setis24hourview (TRUE); Set the default time for the current system time/* or you can use Timepicker's Getcurrenthour () and Getcurrentminute () methods to get the current time */times = GetCurrentTime (); Timepicker.setcurrenthour (integer.valueof (time[0)); Timepicker.setcurrentminute (integer.valueof (time[1)); Set Date DatePicker = (datePicker) Findviewbyid (R.id.datepicker); Datepicker.setcalendarviewshown (FALSE); Set the default datetime on the button Timebutton.settext (timepicker.getcurrenthour () + ":" +timepicker.getcurrentminute ()); Datebutton.settext (Datepicker.getyear () + "year" + (Datepicker.getmonth () +1) + "month" +datepicker.getdayofmonth () + "Day"); Sets the date selector to be visible by default datepicker.setvisibility (view.visible); Datebutton.setbackgroundresource (R.COLOR.BUTTON_PRESSED_BG); Set Listener Timepicker.setontimechangedlistener (new SendtimecHangedlistener ()); Datepicker.init (Datepicker.getyear (), Datepicker.getmonth () +1, Datepicker.getdayofmonth (), New Senddatechangedlistener ()); } public string[] GetCurrentTime () {//Get current hour minute to SimpleDateFormat Curtimeformat = new simpled Ateformat ("hh-mm"); String curtime = Curtimeformat.format (New Date ()); String[] time = new STRING[2]; Time = Curtime.split ("-"); return time; } public string[] Getcurrentdate () {//Get the current date of the month and day SimpleDateFormat Curdateformat = new Simpledateform at ("Yyyy-mm-dd"); String curdate = Curdateformat.format (New Date ()); string[] Date = new String[3]; Date = Curdate.split ("-"); return date; } public void Picksenddate (view view) {datepicker.setvisibility (view.visible); Timepicker.setvisibility (view.invisible); Timebutton.setbackgroundresource (R.COLOR.BUTTON_BG); Datebutton.setbackgroundresource (r.coLOR.BUTTON_PRESSED_BG); } public void Picksendtime (view view) {datepicker.setvisibility (view.invisible); Timepicker.setvisibility (view.visible); Timebutton.setbackgroundresource (R.COLOR.BUTTON_PRESSED_BG); Datebutton.setbackgroundresource (R.COLOR.BUTTON_BG); }//todo//Set date is earlier than the current date private Boolean isbeforecurdate () {return false; }//todo//Set time is earlier than the current time private Boolean isbeforecurtime () {return false; } class Sendtimechangedlistener implements ontimechangedlistener{@Override public void ontimechanged (Tim Epicker view, int hourofday, int minute) {Timebutton.settext (hourofday+ ":" +minute); MainActivity.this.hour = Hourofday; MainActivity.this.minute = minute; }} class Senddatechangedlistener implements ondatechangedlistener{@Override public void OnDa Techanged (DatePicker view, int year, int month, Int. day) { Datebutton.settext (year+ "year" + (month+1) + "month" +day+ "Day"); MainActivity.this.year = year; MainActivity.this.month = month; MainActivity.this.day = day; }}//Create menu @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; this adds Items to the Action bar if it is present. Getmenuinflater (). Inflate (R.menu.main, menu); return true; }}
One thing to keep in mind is that month months add 1, otherwise the month is wrong.
Color.xml
<?xml version= "1.0" encoding= "Utf-8"?><resources> <color name= "BUTTON_PRESSED_BG" > #DD5D04 </color> <color name= "BUTTON_BG" > #F7F7F7 </color></resources>
Bg.xml
<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android "> <item android:state_pressed=" true " android:drawable=" @color/button_pressed_bg " / > <item android:drawable= "@color/button_bg" /></selector>
1. By default, the Default Check Date button displays the DatePicker view, which loads the system's current date by default.
2. Click the Time button control, the view switches to Timepicker, and the default is the system time when loading enters.
3. Select a date or time at the top and bottom of the date, the text information in the button control or Time button control changes, thanks to the ondatechanged (DatePicker view, int year, int month, after setting the listener to the control). int day) and ontimechanged (timepicker view, int hourofday, int minute) methods.