Date Picker leverages Numberpicker

Source: Internet
Author: User

public class Xcydatepicker extends LinearLayout {private static final int min_year = 1900;    private int max_year = 2016;    private static final int min_month = 1;    private static final int max_month = 12;    Private Numberpicker YEARNP;    Private Numberpicker MONTHNP;    Private Numberpicker DAYNP;        Public Xcydatepicker (Context context) {super (context);    Init (context);        } public Xcydatepicker (context context, AttributeSet Attrs) {Super (context, attrs);    Init (context); } public Xcydatepicker (context context, AttributeSet attrs, int defstyleattr) {Super (context, Attrs, defstyleat        TR);    Init (context);        } private void Init (context context) {Layoutinflater.from (context). Inflate (R.layout.xcy_date_picker, this);        YEARNP = (numberpicker) Findviewbyid (R.ID.YEAR_NP);        MONTHNP = (numberpicker) Findviewbyid (R.ID.MONTH_NP);        DAYNP = (numberpicker) Findviewbyid (R.ID.DAY_NP); Initialize the displayed date by default today sEtnpminvalandmaxval (YEARNP, Min_year, max_year);        Setnpminvalandmaxval (MONTHNP, Min_month, Max_month);        Setdisplayvalue (New Date ()); Monthnp.setonvaluechangedlistener (New Numberpicker.onvaluechangelistener () {@Override public void O Nvaluechange (numberpicker picker, int oldval, int newval) {daynp.setmaxvalue (Getdaysinmonthandyear (yearNp).            GetValue (), newval));        }        }); Yearnp.setonvaluechangedlistener (New Numberpicker.onvaluechangelistener () {@Override public void on ValueChange (numberpicker picker, int oldval, int newval) {Daynp.setmaxvalue (getdaysinmonthandyear,            Monthnp.getvalue ()));    }        }); }/** * Gets the number of days in a year January * * @param years * @param month value range 1-12 * @return */private int getDays        Inmonthandyear (int year, int month) {Calendar calendar = calendar.getinstance ();      Calendar.set (Calendar.year, year);  Calendar.set (Calendar.month, month-1);    Return Calendar.getactualmaximum (;  }/** * Displays the current displayed date according to date * * @param date */public void Setdisplayvalue (date date) {Calendar        Calendar = Calendar.getinstance ();        Calendar.settime (date);        Yearnp.setvalue (Calendar.get (calendar.year));        Monthnp.setvalue (Calendar.get (calendar.month) + 1);        Setnpminvalandmaxval (DAYNP, 1, Getdaysinmonthandyear (Yearnp.getvalue (), Monthnp.getvalue ()));    Daynp.setvalue (Calendar.get (calendar.day_of_month)); */** * * * * * @param year * @param month * @param day */public void Setdisplayva, depending on date of month Lue (int year, int month, Int. day) {Checknumisinrange (year, Min_year, Max_year, "enter" + min_year + "~ ~ ~" + Max_yea        R + "year between");        Yearnp.setvalue (year);        Checknumisinrange (month, Min_month, Max_month, "Please enter" + Min_month + "~ ~ ~" + max_month + "months between");        Monthnp.setvalue (month); INT Maxdays = Getdaysinmonthandyear (Yearnp.getvalue (), Monthnp.getvalue ());        Setnpminvalandmaxval (DAYNP, 1, maxdays);        Checknumisinrange (day, 1, maxdays, "Please enter the number of days between" + 1 + "to" + Maxdays + ");    Daynp.setvalue (day);     /** * Check if the number entered is within range * * @param checknum * @param minnum * @param maxnum * @param message */private void Checknumisinrange (int checknum, int minnum, int maxnum, String message) {if (Checknum < Minn Um | |        Checknum > Maxnum) {throw new numberformatexception (message); }}/** * Gets the date type of the current presentation is the datetime * * @return The return value is date type */public date getdisplaydate () {Calen        Dar Calendar = calendar.getinstance ();        Calendar.set (Yearnp.getvalue (), Monthnp.getvalue ()-1, Daynp.getvalue ());    return Calendar.gettime (); /** * Gets the date string in the specified format * * @param pattern * @return * */public string getdisplaydatestr (string Patt        Ern) {Calendar calendar = Calendar.getinstance ();        Calendar.set (Yearnp.getvalue (), Monthnp.getvalue ()-1, Daynp.getvalue ());        Date date = Calendar.gettime ();        SimpleDateFormat SimpleDateFormat = new SimpleDateFormat (pattern);    return Simpledateformat.format (date);     }/** * Gets the year of the current display * * @return return YEAR */public int getdisplayyear () {return yearnp.getvalue (); }/** * Gets the current display month * * @return return month */public int getdisplaymonth () {return Monthnp.getva    Lue (); }/** * Returns the day ordinal of a January that is currently displayed * @return returns the day of the month */public int getdisplayday () {return Daynp.getv    Alue (); /** * Set Numberpicker range of values * * @param NP * @param minval * @param maxval */private void SE        Tnpminvalandmaxval (numberpicker np, int minval, int maxval) {np.setminvalue (minval);    Np.setmaxvalue (Maxval); }}
<pre name= "code" class= "HTML" ><?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= " Http:// "    android:layout_width=" match_parent "    android:layout_height= "Match_parent"    android:orientation= "Horizontal" >    <numberpicker        android:id= "@+id/year_np"        android:layout_width= "0DP"        android:layout_height= "wrap_content"        android:layout_weight= "1" >< /numberpicker>    <numberpicker        android:id= "@+id/month_np"        android:layout_width= "0DP"        android:layout_height= "Wrap_content"        android:layout_marginleft= "4DP"        android:layout_weight= "1" > </NumberPicker>    <numberpicker        android:id= "@+id/day_np"        android:layout_width= "0DP"        android:layout_height= "wrap_content"        android:layout_marginleft= "4DP"        android:layout_weight= "1" ></NumberPicker></LinearLayout>

Paste the code directly

Date Picker leverages Numberpicker

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.