WenJieWangFlyToWorld

导航

日期 时间选择器(DatePicker和TimePicker)实现用户选择

日期和时间

作者的设计TimePicker时,大小分布不合理,我调整宽度为match-parent高度为wrap-parent就可以了。

  1. public class MainActivity extends Activity
  2. {
  3. // 定义5个记录当前时间的变量
  4. private int year;
  5. private int month;
  6. private int day;
  7. private int hour;
  8. private int minute;
  9. @Override
  10. public void onCreate(Bundle savedInstanceState)
  11. {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.main);
  14. DatePicker datePicker = (DatePicker)findViewById(R.id.datePicker);
  15. TimePicker timePicker = (TimePicker)findViewById(R.id.timePicker);
  16. // 获取当前的年、月、日、小时、分钟
  17. Calendar c = Calendar.getInstance();
  18. year = c.get(Calendar.YEAR);
  19. month = c.get(Calendar.MONTH);
  20. day = c.get(Calendar.DAY_OF_MONTH);
  21. hour = c.get(Calendar.HOUR);
  22. minute = c.get(Calendar.MINUTE);
  23. // 初始化DatePicker组件,初始化时指定监听器
  24. datePicker.init(year, month, day, new OnDateChangedListener()
  25. {
  26. @Override
  27. public void onDateChanged(DatePicker arg0, int year
  28. , int month, int day)
  29. {
  30. MainActivity.this.year = year;
  31. MainActivity.this.month = month;
  32. MainActivity.this.day = day;
  33. // 显示当前日期、时间
  34. showDate(year, month, day, hour, minute);
  35. }
  36. });
  37. timePicker.setEnabled(true);
  38. // TimePicker指定监听器
  39. timePicker.setOnTimeChangedListener(new OnTimeChangedListener()
  40. {
  41. @Override
  42. public void onTimeChanged(TimePicker view
  43. , int hourOfDay, int minute)
  44. {
  45. MainActivity.this.hour = hourOfDay;
  46. MainActivity.this.minute = minute;
  47. // 显示当前日期、时间
  48. showDate(year, month, day, hour, minute);
  49. }
  50. });
  51. }
  52. // 定义在EditText中显示当前日期、时间的方法
  53. private void showDate(int year, int month
  54. , int day, int hour, int minute)
  55. {
  56. EditText show = (EditText) findViewById(R.id.show);
  57. show.setText("您的购买日期为:" + year + ""
  58. + (month + 1) + "" + day + " " + hour + "" + minute + "");
  59. }
  60. }

 

XML文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <TextView
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:text="选择购买本书的具体时间"/>
  10. <!-- 定义一个DatePicker组件 -->
  11. <DatePicker android:id="@+id/datePicker"
  12. android:layout_width="wrap_content"
  13. android:layout_height="200dp"
  14. android:layout_gravity="center_horizontal"
  15. android:startYear="2000"
  16. android:endYear="2016"
  17. android:calendarViewShown="true"
  18. android:spinnersShown="true"/>
  19. <!-- 定义一个TimePicker组件 -->
  20. <TimePicker android:id="@+id/timePicker"
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content"
  23. android:layout_gravity="center_horizontal"/>
  24. <!-- 显示用户输入日期、时间的控件 -->
  25. <EditText android:id="@+id/show"
  26. android:layout_width="match_parent"
  27. android:layout_height="wrap_content"
  28. android:editable="false"
  29. android:cursorVisible="false"/>
  30. </LinearLayout>

 

效果

posted on 2017-06-01 19:57  WenjieWangFlyToWorld  阅读(1699)  评论(0编辑  收藏  举报