Preface
You only need to display the calendar controls of the month and the day, and do not want to customize the controls. The simplest way is to hide the box for displaying the year, but DatePickerDialog does not directly provide a method for operations, here is a stupid way to share :)
Statement
You are welcome to repost, but please keep the original source of the article :)
Blog: http://www.cnblogs.com
Farmer's uncle: http://over140.cnblogs.com
Body
I,
1.1 default
1.2 after processing
II. Implementation Code
2.1 code snippet 1
/**
* Find the DatePicker sub-control from the current Dialog.
*
* @ Param group
* @ Return
*/
Private DatePicker findDatePicker (ViewGroup group ){
If (group! = Null ){
For (int I = 0, j = group. getChildCount (); I <j; I ++ ){
View child = group. getChildAt (I );
If (child instanceof DatePicker ){
Return (DatePicker) child;
} Else if (child instanceof ViewGroup ){
DatePicker result = findDatePicker (ViewGroup) child );
If (result! = Null)
Return result;
}
}
}
Return null;
}
Code Description:
You can also see the DatePicker sub-control in the ContentView of Dialog through the breakpoint. Here, you can find this control through traversal.
2.2 Use Code
Final Calendar cal = Calendar. getInstance ();
MDialog = new CustomerDatePickerDialog (getContext (), this,
Cal. get (Calendar. YEAR), cal. get (Calendar. MONTH ),
Cal. get (Calendar. DAY_OF_MONTH ));
MDialog. show ();
DatePicker dp = findDatePicker (ViewGroup) mDialog. getWindow (). getDecorView ());
If (dp! = Null ){
(ViewGroup) dp. getChildAt (0). getChildAt (0). setVisibility (View. GONE );
}
Code Description:
The source code shows that DatePicker has three built-in NumberPicker controls, indicating year, month, and day in sequence, and hiding the first one.
Iii. Supplement
The title bar should also be changed in subsequent use. You can view the source code of DatePickerDialog by customizing and implementing the onDateChanged method, as shown in the following code:
Class CustomerDatePickerDialog extends DatePickerDialog {
Public CustomerDatePickerDialog (Context context,
OnDateSetListener callBack, int year, int monthOfYear,
Int dayOfMonth ){
Super (context, callBack, year, monthOfYear, dayOfMonth );
}
@ Override
Public void onDateChanged (DatePicker view, int year, int month, int day ){
Super. onDateChanged (view, year, month, day );
MDialog. setTitle (month + 1) + "month" + day + "day ");
}
}