Android DataPickerDialog only displays the year, month, and day. androidpicker
In the past few days to do another App development, development needs to use DatePickerDialog to show only the year and month, on the Internet to refer to a lot of examples, some do not know, fortunately saw (here please refer to the http://whb198900.blog.163.com/blog/static/766453542012108111920879) this friend's article and workshop.
1 public class SecondActivity extends Activity implements OnClickListener {2/** Called when the activity is first created. */3 private Button button; 4 private EditText textview; 5 private Dialog mdialog; 6 private Calendar calendar = null; 7 8 @ Override 9 public void onCreate (Bundle savedInstanceState) {10 super. onCreate (savedInstanceState); 11 requestWindowFeature (Window. FEATURE_NO_TITLE); 12 SetContentView (R. layout. second); 13 14 initUi (); 15 button. setOnClickListener (this); 16} 17 18 public void initUi () {19 button = (Button) findViewById (R. id. show); 20 textview = (EditText) findViewById (R. id. editText); 21} 22 23 @ Override 24 public void onClick (View v) {25 // TODO Auto-generated method stub 26 showDialog (0 ); // Date pop-up box 27 int SDKVersion = SecondActivity. this. getSDKVersionNumber (); // Obtain the System version 28 System. out. println ("SDKVersion =" + SDKVersion); 29 DatePicker dp = findDatePicker (ViewGroup) mdialog. getWindow () 30. getDecorView (); // you can specify 31 if (dp! = Null) {32 if (SDKVersion <11) {33 (ViewGroup) dp. getChildAt (0 )). getChildAt (1 ). setVisibility (34 View. GONE); 35} else if (SDKVersion> 14) {36 // only display year 37 (ViewGroup) dp. getChildAt (0 )). getChildAt (0) 38. getChildAt (1 ). setVisibility (View. GONE );//. getChildAt (0) 39 // only display the day of the year 40 // (ViewGroup) dp. getChildAt (0 )). getChildAt (0) 41 //. getChildAt (2 ). setVisibility (View. GONE); 42 // only display the date and year 43 // (ViewGroup) dp. getChildAt (0 )). getChildAt (0) 44 //. getChildAt (1 ). setVisibility (View. GONE); 45 // display day of the month 46 // (ViewGroup) dp. getChildAt (0 )). getChildAt (0) 47 //. getChildAt (0 ). setVisibility (View. GONE); 48} 49} 50 51} 52 53 @ Override 54 protected Dialog onCreateDialog (int id) {// corresponds to the showDialog (0) above ); // Date pop-up box 55 mdialog = null; 56 switch (id) {57 case 0: 58 calendar = Calendar. getInstance (); 59 mdialog = new DatePickerDialog (this, 60 new DatePickerDialog. onDateSetListener () {61 @ Override 62 public void onDateSet (DatePicker view, int year, 63 int monthOfYear, int dayOfMonth) {64 textview. setText (year + "-" + (monthOfYear); 65} 66}, calendar. get (Calendar. YEAR), 67 calendar. get (Calendar. MONTH), 68 calendar. get (Calendar. DAY_OF_MONTH); 69 brea K; 70} 71 return mdialog; 72} 73 74/** 75 * Find the DatePicker sub-control 76*77 * @ param group 78 * @ return 79 */80 private DatePicker findDatePicker (ViewGroup group) from the current Dialog) {81 if (group! = Null) {82 for (int I = 0, j = group. getChildCount (); I <j; I ++) {83 View child = group. getChildAt (I); 84 if (child instanceof DatePicker) {85 return (DatePicker) child; 86} else if (child instanceof ViewGroup) {87 DatePicker result = findDatePicker (ViewGroup) child); 88 if (result! = Null) 89 return result; 90} 91} 92} 93 return null; 94} 95 96/** 97 * Get the system SDK version 98*99 * @ return100 */101 public static int getSDKVersionNumber () {102 int sdkVersion; 103 try {104 sdkVersion = Integer. valueOf (android. OS. build. VERSION. SDK); 105} catch (NumberFormatException e) {106 sdkVersion = 0; 107} 108 return sdkVersion; 109} 110}
For more information, see !!!