效果和代码都非常直观:
实例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中TimePicker与DatePicker时间日期选择组件的使用实例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)