Android 程式开发:(十一)选择控件 —— 11.2 DatePicker

DataPicker和上一节讲的TimePicker很相似。使用DatePicker,可以让用户选择一个特定的日期。下面展示如何使用DatePicker。

1、使用上一节的工程,BasicViews4,修改main.xml。

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6. <Buttonandroid:id="@+id/btnSet"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="Iamallset!"
  10. android:onClick="onClick"/>
  11. <DatePickerandroid:id="@+id/datePicker"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"/>
  14. <TimePickerandroid:id="@+id/timePicker"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"/>
  17. </LinearLayout>
2、F11调试。竖屏模式显式DatePicker稍微有一些窄,按Ctrl+F11,可以更改屏幕的显示方向。

Android 程式开发:(十一)选择控件 —— 11.2 DatePicker

3、在BasicViews4Activity.java中添加一些代码

  1. packagenet.learn2develop.BasicViews4;
  2. importjava.text.SimpleDateFormat;
  3. importjava.util.Calendar;
  4. importjava.util.Date;
  5. importandroid.app.Activity;
  6. importandroid.app.DatePickerDialog;
  7. importandroid.app.Dialog;
  8. importandroid.app.TimePickerDialog;
  9. importandroid.os.Bundle;
  10. importandroid.view.View;
  11. importandroid.widget.DatePicker;
  12. importandroid.widget.TimePicker;
  13. importandroid.widget.Toast;
  14. publicclassBasicViews4ActivityextendsActivity{
  15. TimePickertimePicker;
  16. DatePickerdatePicker;
  17. inthour,minute;
  18. intyr,month,day;
  19. staticfinalintTIME_DIALOG_ID=0;
  20. staticfinalintDATE_DIALOG_ID=1;
  21. /**Calledwhentheactivityisfirstcreated.*/
  22. @Override
  23. publicvoidonCreate(BundlesavedInstanceState){
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.main);
  26. timePicker=(TimePicker)findViewById(R.id.timePicker);
  27. timePicker.setIs24HourView(true);
  28. //showDialog(TIME_DIALOG_ID);
  29. datePicker=(DatePicker)findViewById(R.id.datePicker);
  30. //---getthecurrentdate---
  31. Calendartoday=Calendar.getInstance();
  32. yr=today.get(Calendar.YEAR);
  33. month=today.get(Calendar.MONTH);
  34. day=today.get(Calendar.DAY_OF_MONTH);
  35. showDialog(DATE_DIALOG_ID);
  36. }
  37. @Override
  38. protectedDialogonCreateDialog(intid)
  39. {
  40. switch(id){
  41. caseTIME_DIALOG_ID:
  42. returnnewTimePickerDialog(
  43. this,mTimeSetListener,hour,minute,false);
  44. caseDATE_DIALOG_ID:
  45. returnnewDatePickerDialog(
  46. this,mDateSetListener,yr,month,day);
  47. }
  48. returnnull;
  49. }
  50. privateDatePickerDialog.OnDateSetListenermDateSetListener=
  51. newDatePickerDialog.OnDateSetListener()
  52. {
  53. publicvoidonDateSet(
  54. DatePickerview,intyear,intmonthOfYear,intdayOfMonth)
  55. {
  56. yr=year;
  57. month=monthOfYear;
  58. day=dayOfMonth;
  59. Toast.makeText(getBaseContext(),
  60. "Youhaveselected:"+(month+1)+
  61. "/"+day+"/"+year,
  62. Toast.LENGTH_SHORT).show();
  63. }
  64. };
  65. privateTimePickerDialog.OnTimeSetListenermTimeSetListener=
  66. newTimePickerDialog.OnTimeSetListener()
  67. {
  68. publicvoidonTimeSet(
  69. TimePickerview,inthourOfDay,intminuteOfHour)
  70. {
  71. hour=hourOfDay;
  72. minute=minuteOfHour;
  73. SimpleDateFormattimeFormat=newSimpleDateFormat("hh:mmaa");
  74. Datedate=newDate(0,0,0,hour,minute);
  75. StringstrDate=timeFormat.format(date);
  76. Toast.makeText(getBaseContext(),
  77. "Youhaveselected"+strDate,
  78. Toast.LENGTH_SHORT).show();
  79. }
  80. };
  81. publicvoidonClick(Viewview){
  82. Toast.makeText(getBaseContext(),
  83. "Dateselected:"+(datePicker.getMonth()+1)+
  84. "/"+datePicker.getDayOfMonth()+
  85. "/"+datePicker.getYear()+"\n"+
  86. "Timeselected:"+timePicker.getCurrentHour()+
  87. ":"+timePicker.getCurrentMinute(),
  88. Toast.LENGTH_SHORT).show();
  89. }
  90. }
4、点击按钮。Android 程式开发:(十一)选择控件 —— 11.2 DatePicker