Displaying a DatePicker with or without the year field
I would like to display a DatePicker
where the year field would be visible or no开发者_如何学Ct, according to a checkbox.
But I didn't find anything giving me a way to implement it.
Any idea please ?
Thanks in advance.
set the check box listener as your need to view of checkbox
if(id == R.id.event_checkBox_year)
{
if(ch.isChecked())
{
visiblity = View.GONE;
}
else
{
visiblity = View.VISIBLE;
}
showHideYear(visiblity);
}
private void showHideYear(int visiblity)
{
try {
Field f[] = date.getClass().getDeclaredFields();
for (Field field : f) {
if (field.getName().equals("mYearSpinner") ||field.getName().equals("mYearPicker") )
{
field.setAccessible(true);
Object yearPicker = new Object();
yearPicker = field.get(date);
((View) yearPicker).setVisibility(visiblity);
}
}
} catch (SecurityException e)
{
Log.d("ERROR", e.getMessage());
}
catch (IllegalArgumentException e)
{
Log.d("ERROR", e.getMessage());
} catch (IllegalAccessException e)
{
Log.d("ERROR", e.getMessage());
}
}
As it's not possible to remove the year field from a DatePicker
, I finally decide to add a CheckBox
"ignore the year field" in order to implement a solution.
Here's the solution for not showing year spinner or year picker on date picker dialog
try {
Field f[] = datePickerDialog.getClass().getDeclaredFields();
for (Field field : f) {
if (field.getName().equals("mDatePicker")) {
field.setAccessible(true);
Object datePicker=new Object();
datePicker=field.get(datePickerDialog);
Field[] fields=datePicker.getClass().getDeclaredFields();
for(Field field1:fields){
if(field1.getName().equals("mYearSpinner") || field1.getName().equals("mYearPicker")){
field1.setAccessible(true);
Object ye=field1.get(datePicker);
((View)ye).setVisibility(View.GONE);
}
}
}
}
}
catch (SecurityException e) {
Log.d("ERROR", e.getMessage());
}
catch (IllegalArgumentException e) {
Log.d("ERROR", e.getMessage());
}
catch (IllegalAccessException e) {
Log.d("ERROR", e.getMessage());
}
One liner:
getDatePicker().findViewById(Resources.getSystem().getIdentifier("year", "id", "android")).setVisibility(View.GONE);
精彩评论