欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android编程之DatePicker和TimePicke简单时间监听用法分析

程序员文章站 2023-12-16 11:02:46
本文实例讲述了android编程之datepicker和timepicke简单时间监听用法。分享给大家供大家参考,具体如下: datepicker和timepicker都...

本文实例讲述了android编程之datepicker和timepicke简单时间监听用法。分享给大家供大家参考,具体如下:

datepicker和timepicker都是从framelayout派生而来。

简单的例子实现对时间监听。

<?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:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="选择时间"
  />
  <datepicker android:id="@+id/datepicker"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"
  />
  <timepicker
  android:id="@+id/timepicker"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
  />
  <edittext
    android:id="@+id/et"
    android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:editable="false"
  android:cursorvisible="false"
  />
</linearlayout>

package com.ly;
import java.util.calendar;
import android.app.activity;
import android.os.bundle;
import android.widget.datepicker;
import android.widget.datepicker.ondatechangedlistener;
import android.widget.edittext;
import android.widget.timepicker;
import android.widget.timepicker.ontimechangedlistener;
/**
 * @author kurting
 * 两个时间控件监听
 *
 */
public class timeexampleactivity extends activity {
  private int year;
  private int month;
  private int day;
  private int hour;
  private int minute;
  /** called when the activity is first created. */
  @override
  public void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.main);
    datepicker datapicker = (datepicker) findviewbyid(r.id.datepicker);
    timepicker timepicker =(timepicker) findviewbyid(r.id.timepicker);
    calendar c =calendar.getinstance();
    year =c.get(calendar.year);
    month=c.get(calendar.month);
    day=c.get(calendar.day_of_month);
    hour = c.get(calendar.hour);
    minute =c.get(calendar.minute);
    datapicker.init(year, month, day, new ondatechangedlistener() {
      @override
      public void ondatechanged(datepicker view, int year, int monthofyear,
          int dayofmonth) {
    timeexampleactivity.this.year=year;
    timeexampleactivity.this.month=monthofyear;
    timeexampleactivity.this.day=dayofmonth;
    showdate(year,month,day,hour,minute);
      }
    });
    timepicker.setontimechangedlistener(new ontimechangedlistener() {
      @override
      public void ontimechanged(timepicker view, int hourofday, int minute) {
        // todo auto-generated method stub
        timeexampleactivity.this.hour=hourofday;
        timeexampleactivity.this.minute=minute;
      }
    });
  }
  private void showdate(int year, int month, int day, int hour,int minute) {
    edittext et = (edittext) findviewbyid(r.id.et);
    et.settext(year+"年"+month+"月"+day+"日"+hour+"时"+minute+"分");
  }
}

运行效果如图:

Android编程之DatePicker和TimePicke简单时间监听用法分析

更多关于android相关内容感兴趣的读者可查看本站专题:《android日期与时间操作技巧总结》、《android开发入门与进阶教程》、《android多媒体操作技巧汇总(音频,视频,录音等)》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结

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

上一篇:

下一篇: