从零开始学android:Android事件处理—监听日期与时间的改变
2013-11-27 22:39
561 查看
日期选择器(DatePicker)和时间选择器(TimePicker)可以用于进行日期与时间的调整,当两者进行调整的时候也可以采用相关的监听器对其状态进行监听:
日期监听器接口:android.widget.DatePicker.OnDateChangedListener;
时间监听器接口:android.widget.TimePicker.OnTimeChangedListener;
范例:
布局文件:
展示效果:
日期监听器接口:android.widget.DatePicker.OnDateChangedListener;
时间监听器接口:android.widget.TimePicker.OnTimeChangedListener;
范例:
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <EditText android:id="@+id/input" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <DatePicker android:id="@+id/date" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TimePicker android:id="@+id/time" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
程序文件:
package com.richard.ondatetimechangedlistener; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.DatePicker; import android.widget.DatePicker.OnDateChangedListener; import android.widget.EditText; import android.widget.TimePicker; import android.widget.TimePicker.OnTimeChangedListener; public class MyDateTimeDemo extends Activity { private EditText input = null; private DatePicker date = null; private TimePicker time = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_date_time_demo); this.input = (EditText) super.findViewById(R.id.input); this.date = (DatePicker) super.findViewById(R.id.date); this.time = (TimePicker) super.findViewById(R.id.time); this.time.setIs24HourView(true); this.time.setOnTimeChangedListener(new OnTimeChangedListenerImpl()); this.date.init(this.date.getYear(), this.date.getMonth(), this.date.getDayOfMonth(), new OnDateChangedListenerImpl()); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.my_date_time_demo, menu); return true; } public void setDateTime(){ this.input.setText(this.date.getYear() + "-" + (this.date.getMonth() + 1) + "-" + this.date.getDayOfMonth() + " " + this.time.getCurrentHour() + ":" + this.time.getCurrentMinute()); } private class OnDateChangedListenerImpl implements OnDateChangedListener{ @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { MyDateTimeDemo.this.setDateTime(); } } private class OnTimeChangedListenerImpl implements OnTimeChangedListener{ @Override public void onTimeChanged(TimePicker arg0, int arg1, int arg2) { MyDateTimeDemo.this.setDateTime(); } } }
展示效果:
相关文章推荐
- android junit测试
- Android download in windows
- Android入门之界面设计
- 安卓调试html js android remote javascript debug
- Android开发之xml文件的序列化
- Android开发adb环境配置
- 从零开始学android:Android事件处理—下拉列表框与OnItemSelectedListener
- 一步一步学android控件(之二十一)—— ListView & ExpandableListView
- android画集合图形
- android安全卫士实现黑名单管理:黑名单号码的删除修改
- ActionBar和Fragment结合ViewPager实现TabActivity
- 【Android Developers Training】 11. 支持不同语言
- Android系列---JSON数据解析
- android EditText 全面阐述
- 基于adt-bundle-windows-x86的android开发环境搭建
- 从零开始学android:Android事件处理—单选钮与OnCheckedChangeListener
- Android系统Recovery工作原理之使用update.zip升级过程分析(一)---update.zip包的制作
- Android计算文件的MD5和SHA1
- 【Android Developers Training】 10. 序言:支持不同设备
- android安全卫士实现黑名单管理:实现黑名单中添加号码