当前位置: 移动技术网 > IT编程>移动开发>Android > Android中日期与时间设置控件用法实例

Android中日期与时间设置控件用法实例

2019年07月24日  | 移动技术网IT编程  | 我要评论

中国龙眼之乡,甜蜜杀机,拳皇漫画全集

本文实例讲述了android中日期与时间设置控件用法。分享给大家供大家参考。具体如下:

1、日期设置控件:datepickerdialog

2、时间设置控件:timepickerdialog

实例代码:

页面添加两个button,单击分别显示日期设置控件和时间设置控件,还是有textview控件,用于显示设置后的系统时间

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
<textview android:id="@+id/dateandtime"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="@string/hello"
 />
<button
 android:id="@+id/setdate"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="set the date"></button>
<button android:id="@+id/settime"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="set the time"></button>
</linearlayout>

chronodemo.java如下:

package yyl.android;
import java.text.dateformat;
import java.util.calendar;
import java.util.locale;
import android.app.activity;
import android.app.datepickerdialog;
import android.app.timepickerdialog;
import android.os.bundle;
import android.view.view;
import android.widget.button;
import android.widget.datepicker;
import android.widget.textview;
import android.widget.timepicker;
public class chronodemo extends activity {
 //获取日期格式器对象
 dateformat fmtdateandtime = dateformat.getdatetimeinstance();
 //定义一个textview控件对象
 textview dateandtimelabel = null;
 //获取一个日历对象
 calendar dateandtime = calendar.getinstance(locale.china);
 //当点击datepickerdialog控件的设置按钮时,调用该方法
 datepickerdialog.ondatesetlistener d = new datepickerdialog.ondatesetlistener()
 {
 @override
 public void ondateset(datepicker view, int year, int monthofyear,int dayofmonth) {
  //修改日历控件的年,月,日
  //这里的year,monthofyear,dayofmonth的值与datepickerdialog控件设置的最新值一致
  dateandtime.set(calendar.year, year);
  dateandtime.set(calendar.month, monthofyear);
  dateandtime.set(calendar.day_of_month, dayofmonth); 
  //将页面textview的显示更新为最新时间
  updatelabel();  
 } 
 };
 timepickerdialog.ontimesetlistener t = new timepickerdialog.ontimesetlistener() {
 //同datepickerdialog控件
 @override
 public void ontimeset(timepicker view, int hourofday, int minute) {
  dateandtime.set(calendar.hour_of_day, hourofday);
  dateandtime.set(calendar.minute, minute);
  updatelabel();
 }
 };
 @override
 public void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.main);
 //得到页面设定日期的按钮控件对象
 button datebtn = (button)findviewbyid(r.id.setdate);
 //设置按钮的点击事件监听器
 datebtn.setonclicklistener(new view.onclicklistener() {
  @override
  public void onclick(view v) {
  //生成一个datepickerdialog对象,并显示。显示的datepickerdialog控件可以选择年月日,并设置
  new datepickerdialog(chronodemo.this,
   d,
   dateandtime.get(calendar.year),
   dateandtime.get(calendar.month),
   dateandtime.get(calendar.day_of_month)).show();
  }
 });
 button timebtn = (button)findviewbyid(r.id.settime);
 timebtn.setonclicklistener(new view.onclicklistener() {
  //同上原理
  @override
  public void onclick(view v) {
  new timepickerdialog(chronodemo.this,
   t,
   dateandtime.get(calendar.hour_of_day),
   dateandtime.get(calendar.minute),
   true).show();
  }
 });
 dateandtimelabel=(textview)findviewbyid(r.id.dateandtime);
 updatelabel();
 }
 //更新页面textview的方法
 private void updatelabel() {
 dateandtimelabel.settext(fmtdateandtime
 .format(dateandtime.gettime()));
 }
}

希望本文所述对大家的android程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网