Accumulated over time: how to hide the date selection of DatePicker
In our daily development process, we may encounter a requirement for choosing the validity period of a credit card, which means we need to select the year and month. However, the system control DatePicker supports year, month, and day options by default, such:
So, how can we make the date selector not display? Let's take a look at the source code of DatePicker:
In the DatePicker source code, you have a private member NumberPicker variable mDaySpinner, which should be the control used for date selection. The access permission for private is of course difficult because we have a powerful "reflection" function. Let's look at the instance code below;
MainActivity. java file:
Public classMainActivity extends Activity {privateButton button; @ Override protectedvoid onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button = (Button) findViewById (R. id. button); button. setOnClickListener (newOnClickListener () {@ Override publicvoid onClick (View v) {DatePickerdatePicker = new DatePicker (MainActivity. this); datePicker. setCalendarViewShown (false); // uses the reflection mechanism to access the private mDaySpinner member and hide it. try {Field daySpinner = datePicker. getClass (). getDeclaredField ("mDaySpinner"); daySpinner. setAccessible (true); (View) daySpinner. get (datePicker )). setVisibility (View. GONE);} catch (NoSuchFieldException e) {e. printStackTrace ();} catch (IllegalArgumentException e) {e. printStackTrace ();} catch (IllegalAccessException e) {e. printStackTrace ();} CalendarminCalendar = Calendar. getInstance (); minCalendar. set (Calendar. HOUR_OF_DAY, 0); minCalendar. set (Calendar. MINUTE, 0); minCalendar. set (Calendar. SECOND, 0); datePicker. setMinDate (minCalendar. getTimeInMillis (); CalendarmaxCalendar = Calendar. getInstance (); maxCalendar. add (Calendar. YEAR, 1); datePicker. setMaxDate (maxCalendar. getTimeInMillis (); CalendarcurCalendar = Calendar. getInstance (); datePicker. init (curCalendar. get (Calendar. YEAR), curCalendar. get (Calendar. MONTH), curCalendar. get (Calendar. DAY_OF_MONTH), null); AlertDialog. builderbuilder = new AlertDialog. builder (MainActivity. this); builder. setView (datePicker); builder. setTitle ("select validity period"); builder. setPositiveButton ("OK", null); AlertDialogdialog = builder. create (); dialog. setCanceledOnTouchOutside (true); dialog. show ();}});}}
Okay. Run the command to see the effect:
In jquery ui, how does one set the date before the current date as unselectable?
There is a minDate (minimum date) in the Datepicker option. If you set minDate to the current date, neither of the previous dates can be selected.
MinDate can be
1. Date object, such as new Date ()
2. It can also be an integer. For example, 3 represents three days later,-1 represents yesterday, and 0 represents today.
3. It can also be a string. For example, 1 w represents one week later, and-2 m represents two months earlier.
Therefore, you can set the date object (current) or 0.
$ ("# Ele"). datepicker ({
MinDate: new Date ()
});
Or
$ ("# Ele"). datepicker ({
MinDate: 0
});
After dateTimepicker selects a date, the calendar is not hidden.
The landlord can
DtpBirth. Format = DateTimePickerFormat. Short;
DtpBirth. CustomFormat = null;
Write
In the dtpBirth_CloseUp () event, I tested it. Yes!