[Android] Only displays the DatePickerDialog of month and day

Source: Internet
Author: User

 

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 ");
}
}

 

 

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.