Android中TimePicker与DatePicker时间日期选择组件的使用实例
程序员文章站
2024-03-01 17:53:40
效果和代码都非常直观:
实例1:timepicker
效果和代码都非常直观:
实例1:timepicker
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <timepicker android:id="@+id/timepic1" android:layout_height="wrap_content" android:layout_width="match_parent"/> <button android:id="@+id/buttone1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/timepic1" android:text="获取timepick时间"/> </relativelayout>
package com.android.xiong.times; import android.os.bundle; import android.app.activity; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.timepicker; import android.widget.timepicker.ontimechangedlistener; public class mainactivity extends activity { private timepicker timepick1; private button buttone1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); timepick1=(timepicker)findviewbyid(r.id.timepic1); buttone1=(button)findviewbyid(r.id.buttone1); onchangelistener buc=new onchangelistener(); buttone1.setonclicklistener(buc); //是否使用24小时制 timepick1.setis24hourview(true); timelistener times=new timelistener(); timepick1.setontimechangedlistener(times); } class onchangelistener implements onclicklistener{ @override public void onclick(view v) { // todo auto-generated method stub int h=timepick1.getcurrenthour(); int m=timepick1.getcurrentminute(); system.out.println("h:"+h+" m:"+m); } } class timelistener implements ontimechangedlistener{ /** * view 当前选中timepicker控件 * hourofday 当前控件选中timepicker 的小时 * minute 当前选中控件timepicker 的分钟 */ @override public void ontimechanged(timepicker view, int hourofday, int minute) { // todo auto-generated method stub system.out.println("h:"+ hourofday +" m:"+minute); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate the menu; this adds items to the action bar if it is present. getmenuinflater().inflate(r.menu.main, menu); return true; } }
实例2:datepicker
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <datepicker android:id="@+id/datepick1" android:layout_height="wrap_content" android:layout_width="match_parent" /> <button android:id="@+id/button1" android:layout_below="@id/datepick1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="获取datepicker的值"/> </relativelayout>
package com.android.xiong.datepicker; import android.os.bundle; import android.app.activity; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.datepicker; public class mainactivity extends activity { private datepicker datepicker1; private button button1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); datepicker1=(datepicker)findviewbyid(r.id.datepick1); //设置默认的时间 比如2055年 9月9日 datepicker1.updatedate(2012, 8, 9); button1=(button)findviewbyid(r.id.button1); oncliclisers cl=new oncliclisers(); button1.setonclicklistener(cl); } class oncliclisers implements onclicklistener{ @override public void onclick(view v) { // todo auto-generated method stub int y=datepicker1.getyear(); int m=datepicker1.getmonth()+1; int d=datepicker1.getdayofmonth(); system.out.println("y:"+y+" m:"+m+" d:"+d); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate the menu; this adds items to the action bar if it is present. getmenuinflater().inflate(r.menu.main, menu); return true; } }
推荐阅读
-
Android中TimePicker与DatePicker时间日期选择组件的使用实例
-
Android之日期时间选择控件DatePicker和TimePicker实例
-
Android开发中DatePicker日期与时间控件实例代码
-
Android之日期时间选择控件DatePicker和TimePicker实例
-
Android开发中DatePicker日期与时间控件实例代码
-
Android中DatePicker日期选择器的使用和获取选择的年月日
-
Android中TimePicker时间选择器的使用和获取选择的时和分
-
Android中DatePicker日期选择器的使用和获取选择的年月日
-
Android中TimePicker时间选择器的使用和获取选择的时和分