7.1.2 DatePicker结合案例详解

DatePicker是Android的日期设置控件。DatePicker类的继承图如下:
java.lang.Object
   ↳android.view.View
   ↳android.view.ViewGroup
   ↳android.widget.FrameLayout
   ↳android.widget.DatePicker
android.widget.DatePicker继承了android.widget.FrameLayout框架布局类。 DatePicker例子如图7-7所示,从左到右是年、月、日的设置,改变年月日都会触发OnDateChanged事件,当点击“按钮”可以获得当前 设置的时间。
 


图7-7 DatePicker
请参考代码清单7-6,完整代码请参考chapter7_1工程中chapter7_DatePicker代码部分。
【代码清单7-6】

 
 
  1. public class chapter7_DatePicker extends Activity { 
  2. private TextView mDateDisplay; 
  3. private DatePicker datePicker; 
  4. private Calendar c; 
  5. private Button button; 
  6.  
  7. @Override 
  8. public void onCreate(Bundle savedInstanceState) { 
  9. super.onCreate(savedInstanceState); 
  10. setContentView(R.layout.datepicker_layout); 
  11. datePicker = (DatePicker) findViewById(R.id.datePicker); 
  12.  
  13. c = Calendar.getInstance(); 
  14. mDateDisplay = (TextView) findViewById(R.id.datetextview); 
  15. datePicker.init(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c 
  16. .get(Calendar.DAY_OF_MONTH), 
  17. new DatePicker.OnDateChangedListener() { 
  18. public void onDateChanged(DatePicker view, int year, 
  19. int monthOfYear, int dayOfMonth) { 
  20. mDateDisplay.setText("[" + year + "-" 
  21. + (monthOfYear + 1) + "-" + dayOfMonth + "]" 
  22. "[" + view.getYear() + "-" 
  23. + (view.getMonth() + 1) + "-" 
  24. + view.getDayOfMonth() + "]"); 
  25.  
  26.  
  27. }); 
  28.  
  29. button = (Button) findViewById(R.id.Button01); 
  30. button.setOnClickListener(new OnClickListener() { 
  31.  
  32. @Override 
  33. public void onClick(View v) { 
  34. mDateDisplay.setText(String.valueOf(datePicker.getYear()) 
  35. " - " + String.valueOf(datePicker.getMonth() + 1
  36. " - " + String.valueOf(datePicker.getDayOfMonth())); 
  37. }); 
  38. }  


Calendar.getInstance()会获得一个Calendar实例,这是一个日期实例,通过它的 get(Calendar.YEAR)方法可以获得年,get(Calendar.MONTH)方法加1获得月, get(Calendar.DAY_OF_MONTH)方法获得日期。DataPicker控件的核心代码是init方法:
datePicker.init(c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {…}
在init方法中初始化DataPicker和事件的处理。
DataPicker的布局文件请参考代码清单7-7,完整代码请参考chapter7_1工程中datepicker_layout.xml代码部分(chapter7_1/res/layout/datepicker_layout.xml)。
【代码清单7-7】

 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3. android:orientation="vertical" android:layout_width="fill_parent" 
  4. android:layout_height="fill_parent"> 
  5. <TextView android:layout_width="fill_parent" 
  6. android:layout_height="wrap_content" android:text="@string/hello" /> 
  7. <DatePicker android:id="@+id/datePicker" 
  8. android:layout_width="wrap_content" android:layout_height="wrap_content"> 
  9. </DatePicker> 
  10. <TextView android:id="@+id/datetextview" android:layout_width="fill_parent" 
  11. android:layout_height="wrap_content" android:text="@string/hello" /> 
  12.  
  13. <Button android:text="按钮" android:id="@+id/Button01" 
  14. android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
  15. </LinearLayout>  


                                 出自《Android开发案例驱动教程》第七章

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值