In recent days to do an app development, development needs to use Datepickerdialog to show only the years, in the online reference a lot of examples, some of their own not enough, fortunately see (please refer to Http://whb198900.blog.163.com/blog /static/766453542012108111920879) This friend's article as well as the code of the http://download.csdn.net/detail/u010372772/6027761 here, the summary of their own, in short and completed his own task.
1PublicClass SecondactivityExtends ActivityImplementsOnclicklistener {2/**Called when the activity is first created.*/3PrivateButton button;4PrivateEditText TextView;5PrivateDialog Mdialog;6Private Calendar Calendar =Null;78@Override9PublicvoidOnCreate (Bundle savedinstancestate) {10Super. OnCreate (Savedinstancestate);11Requestwindowfeature (Window.feature_no_title);12Setcontentview (R.layout.second);1314Initui ();Button.setonclicklistener (This);16}1718PublicvoidInitui () {button =(Button) Findviewbyid (r.id.show);TextView =(EditText) Findviewbyid (R.id.edittext);21st}2223@Override24PublicvoidOnClick (View v) {25//TODO auto-generated Method StubShowDialog (0);//Date pop-up box27int sdkversion = secondactivity.This.getsdkversionnumber ();//Get System versionSystem.out.println ("sdkversion =" +Sdkversion);DatePicker DP =Finddatepicker ((ViewGroup) Mdialog.getwindow ()Getdecorview ());//Set Popup Date31if (DP! =Null) {32if (Sdkversion < 11) {((viewgroup) dp.getchildat (0)). Getchildat (1). Setvisibility (34View.gone);35}Elseif (Sdkversion > 14) {36//Only show the MonthPanax Notoginseng ((ViewGroup) ((viewgroup) dp.getchildat (0)). Getchildat (0))Getchildat (1). setvisibility (View.gone);//. Getchildat (0)39//Show only the day of the year40//((ViewGroup) ((viewgroup) dp.getchildat (0)). Getchildat (0))41//. Getchildat (2). setvisibility (View.gone);42//Only show the Month43//((ViewGroup) ((viewgroup) dp.getchildat (0)). Getchildat (0))44//. Getchildat (1). setvisibility (View.gone);45//Show Month Day46//((ViewGroup) ((viewgroup) dp.getchildat (0)). Getchildat (0))47//. Getchildat (0). setvisibility (View.gone);48}49}5051}5253@Override54Protected Dialog Oncreatedialog (int id) {//Corresponds to the above ShowDialog (0);//Date pop-up boxMdialog =Null;56Switch(ID) {57Case 0:The calendar =Calendar.getinstance ();Mdialog =New Datepickerdialog (This,60NewDatepickerdialog.ondatesetlistener () {61@Override62Publicvoid Ondateset (DatePicker view,IntYear63int Monthofyear,IntDayOfMonth) {Textview.settext (Year + "-" +(Monthofyear));65}66}, Calendar.get (Calendar.year),67Calendar.get (Calendar.month),68Calendar.get (Calendar.day_of_month));69Break;70}71ReturnMdialog;72}7374/**75* Find DatePicker child controls from the current dialog76*77*@paramGroup78*@return79*/80PrivateDatePicker Finddatepicker (ViewGroup group) {81if (Group! =Null) {82for (int i = 0, j = group.getchildcount (); I < J; i++) {The View child =Group.getchildat (i);84if (childinstanceofDatePicker) {85Return(DatePicker) child;86}Elseif (childinstanceofViewGroup) {DatePicker result =Finddatepicker ((viewgroup) child);88if (Result! =Null)89ReturnResult90}91}92}93ReturnNull;94}9596/**97* Get the System SDK version98*99*@return100*/101PublicStaticIntGetsdkversionnumber () {102 int sdkversion;< Span style= "color: #008080;" >103 try {104 Sdkversion = integer.valueof (Android.os.Build.VERSION.SDK); catch (NumberFormatException e) {106 sdkversion = 0; 107 }108 return Sdkversion; 109 }110}
What are the shortcomings, please advise!!! 2015-3-30
Android Datapickerdialog Build only displays month or day