ANDROID Specifies that the Datepickerdialog style does not show the implementation code for the year _android

Source: Internet
Author: User

There are many places to use Datepickerdialog. But sometimes the theme of the project is a very ugly style, the show is really ugly.


And the style we really want is this.


This is much more beautiful. And a lot of times do not need to display "year", the following look by the specified theme to show their desired style, and through the reflection mechanism to hide the "year", and can extrapolate, the above things to do task modification.

 private void Showdatepickerdialog () {Calendar calendar = calendar.getinstance ();
    Calendar.settime (New Date ());
    int year = Calendar.get (calendar.year);
    Final int month = Calendar.get (calendar.month);

    int day = Calendar.get (calendar.day_of_month); Datepickerdialog Datepickerdialog = new Datepickerdialog (getactivity (), Alertdialog.theme_holo_light,
    Ondatesetlistener, year, month, day);
    Datepickerdialog.setcancelable (TRUE);
    DatePicker DP = Datepickerdialog.getdatepicker ();
    Set the minimum value dp.setmindate (Calendar.gettimeinmillis ()) for the day;
    Setting the maximum value is 7 days calendar.set (Calendar.day_of_month, day + 6);
    Dp.setmaxdate (Calendar.gettimeinmillis ());
      try {//Get the specified fields field field = Dp.getclass (). Getdeclaredfield ("Myearspinner");
      Solution Encapsulation Field.setaccessible (TRUE);
      Gets the value of the current instance numberpicker NP = ((Numberpicker) Field.get (DP));
    Np.setvisibility (View.gone);
   catch (Nosuchfieldexception e) {e.printstacktrace (); catch (Illegalaccessexception e) {e.printstacktrace ();

  } datepickerdialog.show (); }

When instantiating Datepickerdialog, the second argument specifies that the theme is Theme_holo_light is the desired style, and there is a fun place where you can pass any value of type int to see what kind of clutter your phone supports. Of course this is just playing, or formal point to write this specific topic.

Try{}catch () {} Inside began to use reflection to get the layout of "year", because this field in the source code used the private decoration, and no get method provided, so use Getdeclarefield () To get this field. And then solution encapsulation, otherwise also can not get, that is, setaccessible (true), meaning is accessible, this time to get the value of the current instance is not, just get the class inside a field information, the last sentence, Filed.get ( Object) is to get the current instance has the value of this field, of course, the display of the "year", you can operate on this, and I just need to hide it. There are some online look at some said through the layout of a layer to obtain, this method is not readable, and different versions of the possibility of change is very large, so that the "year", relatively speaking, the possibility of changing the field name should be smaller.

End, this is only to provide a thought, there may be a better way, the great God not to spray!

The above Android specified Datepickerdialog style does not show the implementation of the year Code is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

Related Article

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: info-contact@alibabacloud.com 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.