Android中日期和时间控件的使用
16lz
2021-12-04
Android中日期和时间控件的使用
分类:Android 进阶 2012-02-25 00:29 1647人阅读 评论(1) 收藏 举报 android layout dialog button calendar date
本文主要讲述Android中的日期控件和时间控件的使用,以一个Demo的例子来展示日期和时间控件的使用,先看下如下效果图:
从效果图中可以看到该Demo是通过单击【选择日期】按钮和【选择时间】按钮弹出日期或者时间的对话框,然后设置日期或者时间,设置完成后会在文本框中显示设置的日期或时间值。
【1】Demo程序框架图:
【2】布局文件 res/layout/main.xml 源码:
[html] view plain copy- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns: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:gravity="center"android:text="欢迎关注Andy.ChenBlog"/>
- <TextView
- android:layout_width="fill_parent"android:layout_height="wrap_content"
- android:gravity="center"android:text="日期和时间控件的使用DEMO"/>
- <LinearLayoutandroid:orientation="horizontal"
- android:layout_width="fill_parent"android:layout_height="wrap_content">
- <EditTextandroid:id="@+id/showdate"android:layout_width="fill_parent"
- android:layout_height="wrap_content"android:layout_weight="1"/>
- <Buttonandroid:id="@+id/pickdate"android:layout_width="wrap_content"
- android:layout_height="wrap_content"android:text="选择日期"/>
- </LinearLayout>
- <LinearLayoutandroid:orientation="horizontal"
- android:layout_width="fill_parent"android:layout_height="wrap_content">
- <EditTextandroid:id="@+id/showtime"android:layout_width="fill_parent"
- android:layout_height="wrap_content"android:layout_weight="1"/>
- <Buttonandroid:id="@+id/picktime"android:layout_width="wrap_content"
- android:layout_height="wrap_content"android:text="选择时间"/>
- </LinearLayout>
- </LinearLayout>
【3】包com.andyidea.calenderdemo下MainActivity.java源码:
[html] view plain copy- packagecom.andyidea.calenderdemo;
- importjava.util.Calendar;
- importandroid.app.Activity;
- importandroid.app.DatePickerDialog;
- importandroid.app.Dialog;
- importandroid.app.TimePickerDialog;
- importandroid.os.Bundle;
- importandroid.os.Handler;
- importandroid.os.Message;
- importandroid.view.View;
- importandroid.widget.Button;
- importandroid.widget.DatePicker;
- importandroid.widget.EditText;
- importandroid.widget.TimePicker;
- publicclassMainActivityextendsActivity{
- privateEditTextshowDate=null;
- privateButtonpickDate=null;
- privateEditTextshowTime=null;
- privateButtonpickTime=null;
- privatestaticfinalintSHOW_DATAPICK=0;
- privatestaticfinalintDATE_DIALOG_ID=1;
- privatestaticfinalintSHOW_TIMEPICK=2;
- privatestaticfinalintTIME_DIALOG_ID=3;
- privateintmYear;
- privateintmMonth;
- privateintmDay;
- privateintmHour;
- privateintmMinute;
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- initializeViews();
- finalCalendarc=Calendar.getInstance();
- mYear=c.get(Calendar.YEAR);
- mMonth=c.get(Calendar.MONTH);
- mDay=c.get(Calendar.DAY_OF_MONTH);
- mHour=c.get(Calendar.HOUR_OF_DAY);
- mMinute=c.get(Calendar.MINUTE);
- setDateTime();
- setTimeOfDay();
- }
- /**
- *初始化控件和UI视图
- */
- privatevoidinitializeViews(){
- showDate=(EditText)findViewById(R.id.showdate);
- pickDate=(Button)findViewById(R.id.pickdate);
- showTime=(EditText)findViewById(R.id.showtime);
- pickTime=(Button)findViewById(R.id.picktime);
- pickDate.setOnClickListener(newView.OnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- Messagemsg=newMessage();
- if(pickDate.equals((Button)v)){
- msg.what=MainActivity.SHOW_DATAPICK;
- }
- MainActivity.this.dateandtimeHandler.sendMessage(msg);
- }
- });
- pickTime.setOnClickListener(newView.OnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- Messagemsg=newMessage();
- if(pickTime.equals((Button)v)){
- msg.what=MainActivity.SHOW_TIMEPICK;
- }
- MainActivity.this.dateandtimeHandler.sendMessage(msg);
- }
- });
- }
- /**
- *设置日期
- */
- privatevoidsetDateTime(){
- finalCalendarc=Calendar.getInstance();
- mYear=c.get(Calendar.YEAR);
- mMonth=c.get(Calendar.MONTH);
- mDay=c.get(Calendar.DAY_OF_MONTH);
- updateDateDisplay();
- }
- /**
- *更新日期显示
- */
- privatevoidupdateDateDisplay(){
- showDate.setText(newStringBuilder().append(mYear).append("-")
- .append((mMonth+1)<10?"0"+(mMonth+1):(mMonth+1)).append("-")
- .append((mDay<10)?"0"+mDay:mDay));
- }
- /**
- *日期控件的事件
- */
- privateDatePickerDialog.OnDateSetListenermDateSetListener=newDatePickerDialog.OnDateSetListener(){
- publicvoidonDateSet(DatePickerview,intyear,intmonthOfYear,
- intdayOfMonth){
- mYear=year;
- mMonth=monthOfYear;
- mDay=dayOfMonth;
- updateDateDisplay();
- }
- };
- /**
- *设置时间
- */
- privatevoidsetTimeOfDay(){
- finalCalendarc=Calendar.getInstance();
- mHour=c.get(Calendar.HOUR_OF_DAY);
- mMinute=c.get(Calendar.MINUTE);
- updateTimeDisplay();
- }
- /**
- *更新时间显示
- */
- privatevoidupdateTimeDisplay(){
- showTime.setText(newStringBuilder().append(mHour).append(":")
- .append((mMinute<10)?"0"+mMinute:mMinute));
- }
- /**
- *时间控件事件
- */
- privateTimePickerDialog.OnTimeSetListenermTimeSetListener=newTimePickerDialog.OnTimeSetListener(){
- @Override
- publicvoidonTimeSet(TimePickerview,inthourOfDay,intminute){
- mHour=hourOfDay;
- mMinute=minute;
- updateTimeDisplay();
- }
- };
- @Override
- protectedDialogonCreateDialog(intid){
- switch(id){
- caseDATE_DIALOG_ID:
- returnnewDatePickerDialog(this,mDateSetListener,mYear,mMonth,
- mDay);
- caseTIME_DIALOG_ID:
- returnnewTimePickerDialog(this,mTimeSetListener,mHour,mMinute,true);
- }
- returnnull;
- }
- @Override
- protectedvoidonPrepareDialog(intid,Dialogdialog){
- switch(id){
- caseDATE_DIALOG_ID:
- ((DatePickerDialog)dialog).updateDate(mYear,mMonth,mDay);
- break;
- caseTIME_DIALOG_ID:
- ((TimePickerDialog)dialog).updateTime(mHour,mMinute);
- break;
- }
- }
- /**
- *处理日期和时间控件的Handler
- */
- HandlerdateandtimeHandler=newHandler(){
- @Override
- publicvoidhandleMessage(Messagemsg){
- switch(msg.what){
- caseMainActivity.SHOW_DATAPICK:
- showDialog(DATE_DIALOG_ID);
- break;
- caseMainActivity.SHOW_TIMEPICK:
- showDialog(TIME_DIALOG_ID);
- break;
- }
- }
- };
- }
【4】程序运行效果图:
更多相关文章
- Android(安卓)常用控件讲解
- android控件常用属性区别
- android 时间
- Android实训_2020/6/16
- Android常见控件初探
- Android怎样修改系统时间?
- Android中设置控件可见与不可见详…
- FrameLayout 帧布局
- Android(安卓)自定义控件-SnakeLayout (仿gallery)