本文实例讲述了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;
public class TimeExampleActivity extends Activity {
private int year;
private int month;
private int day;
private int hour;
private int minute;
@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相关内容感兴趣的读者可查看本站专题:《Android日期与时间操作技巧总结》、《Android开发入门与进阶教程》、《Android多媒体操作技巧汇总(音频,视频,录音等)》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。
您可能感兴趣的文章:Android中TimePicker与DatePicker时间日期选择组件的使用实例Android日期选择器对话框DatePickerDialog使用详解Android之日期时间选择控件DatePicker和TimePicker实例android中DatePicker和TimePicker的使用方法详解Android DatePicker和DatePickerDialog基本用法示例android指定DatePickerDialog样式并不显示年的实现代码Android修改DatePicker字体颜色及分割线颜色详细介绍android之datepicker控件的用法Android开发之DatePicker和TimePicker实现选择日期时间功能示例