Android之日期及时间选择对话框用法实例分析
程序员文章站
2023-11-25 09:49:16
本文实例讲述了android之日期及时间选择对话框用法。分享给大家供大家参考。具体如下:
清单文件:
本文实例讲述了android之日期及时间选择对话框用法。分享给大家供大家参考。具体如下:
清单文件:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ljq.dialog" android:versioncode="1" android:versionname="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".alertdialog" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> <uses-sdk android:minsdkversion="7" /> <uses-permission android:name="android.permission.write_calendar" /> </manifest>
main.xml布局文件:
<?xml version="1.0" encoding="utf-8"?> <linearlayout android:id="@+id/linearlayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> <edittext android:id="@+id/et" android:layout_width="fill_parent" android:layout_height="wrap_content" android:editable="false" android:cursorvisible="false" /> <button android:text="日期对话框" android:id="@+id/datebtn" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <button android:text="时间对话框" android:id="@+id/timebtn" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <digitalclock android:text="@+id/digitalclock" android:textsize="20dip" android:gravity="center" android:id="@+id/digitalclock01" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <analogclock android:id="@+id/analogclock" android:gravity="center" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </linearlayout>
alertactivity类:
package com.ljq.dialog; import java.util.calendar; import android.app.activity; import android.app.datepickerdialog; import android.app.dialog; import android.app.timepickerdialog; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.datepicker; import android.widget.edittext; import android.widget.timepicker; public class alertdialog extends activity { private button datebtn = null; private button timebtn = null; private edittext et=null; private final static int date_dialog = 0; private final static int time_dialog = 1; private calendar c = null; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); et=(edittext)findviewbyid(r.id.et); datebtn = (button) findviewbyid(r.id.datebtn); timebtn = (button) findviewbyid(r.id.timebtn); datebtn.setonclicklistener(new view.onclicklistener(){ public void onclick(view v) { showdialog(date_dialog); } }); timebtn.setonclicklistener(new view.onclicklistener(){ public void onclick(view v) { showdialog(time_dialog); } }); } /** * 创建日期及时间选择对话框 */ @override protected dialog oncreatedialog(int id) { dialog dialog = null; switch (id) { case date_dialog: c = calendar.getinstance(); dialog = new datepickerdialog( this, new datepickerdialog.ondatesetlistener() { public void ondateset(datepicker dp, int year,int month, int dayofmonth) { et.settext("您选择了:" + year + "年" + (month+1) + "月" + dayofmonth + "日"); } }, c.get(calendar.year), // 传入年份 c.get(calendar.month), // 传入月份 c.get(calendar.day_of_month) // 传入天数 ); break; case time_dialog: c=calendar.getinstance(); dialog=new timepickerdialog( this, new timepickerdialog.ontimesetlistener(){ public void ontimeset(timepicker view, int hourofday, int minute) { et.settext("您选择了:"+hourofday+"时"+minute+"分"); } }, c.get(calendar.hour_of_day), c.get(calendar.minute), false ); break; } return dialog; } }
运行结果:
希望本文所述对大家的android程序设计有所帮助。
上一篇: Android模拟美团客户端进度提示框