Android自定义DataTimePicker实例代码(日期选择器)
程序员文章站
2024-02-21 22:55:22
笔者有一段时间没有发表关于android的文章了,关于android自定义组件笔者有好几篇想跟大家分享的,后期会记录在博客中。本篇博客给大家分享的是自定义一个日期选择器,可...
笔者有一段时间没有发表关于android的文章了,关于android自定义组件笔者有好几篇想跟大家分享的,后期会记录在博客中。本篇博客给大家分享的是自定义一个日期选择器,可以让用户同时选择年月日和当前时间。
先看看效果:
实现的效果就是在同一个布局上显示日期选择和时间选择。
自定义一个类:
/datetimepicker/src/com/wwj/datetimepicker/datetimepickdialogutil.java
package com.wwj.datetimepicker; import java.text.simpledateformat; import java.util.calendar; import android.app.activity; import android.app.alertdialog; import android.content.dialoginterface; import android.widget.datepicker; import android.widget.datepicker.ondatechangedlistener; import android.widget.edittext; import android.widget.linearlayout; import android.widget.timepicker; import android.widget.timepicker.ontimechangedlistener; /** * 日期时间选择控件 使用方法: private edittext inputdate;//需要设置的日期时间文本编辑框 private string * initdatetime="2012年9月3日 14:44",//初始日期时间值 在点击事件中使用: * inputdate.setonclicklistener(new onclicklistener() { * * @override public void onclick(view v) { datetimepickdialogutil * datetimepickdialog=new * datetimepickdialogutil(sinvestigateactivity.this,initdatetime); * datetimepickdialog.datetimepickdialog(inputdate); * * } }); * * @author */ public class datetimepickdialogutil implements ondatechangedlistener, ontimechangedlistener { private datepicker datepicker; private timepicker timepicker; private alertdialog ad; private string datetime; private string initdatetime; private activity activity; /** * 日期时间弹出选择框构造函数 * * @param activity * :调用的父activity * @param initdatetime * 初始日期时间值,作为弹出窗口的标题和日期时间初始值 */ public datetimepickdialogutil(activity activity, string initdatetime) { this.activity = activity; this.initdatetime = initdatetime; } public void init(datepicker datepicker, timepicker timepicker) { calendar calendar = calendar.getinstance(); if (!(null == initdatetime || "".equals(initdatetime))) { calendar = this.getcalendarbyinintdata(initdatetime); } else { initdatetime = calendar.get(calendar.year) + "年" + calendar.get(calendar.month) + "月" + calendar.get(calendar.day_of_month) + "日 " + calendar.get(calendar.hour_of_day) + ":" + calendar.get(calendar.minute); } datepicker.init(calendar.get(calendar.year), calendar.get(calendar.month), calendar.get(calendar.day_of_month), this); timepicker.setcurrenthour(calendar.get(calendar.hour_of_day)); timepicker.setcurrentminute(calendar.get(calendar.minute)); } /** * 弹出日期时间选择框方法 * * @param inputdate * :为需要设置的日期时间文本编辑框 * @return */ public alertdialog datetimepickdialog(final edittext inputdate) { linearlayout datetimelayout = (linearlayout) activity .getlayoutinflater().inflate(r.layout.common_datetime, null); datepicker = (datepicker) datetimelayout.findviewbyid(r.id.datepicker); timepicker = (timepicker) datetimelayout.findviewbyid(r.id.timepicker); init(datepicker, timepicker); timepicker.setis24hourview(true); timepicker.setontimechangedlistener(this); ad = new alertdialog.builder(activity) .settitle(initdatetime) .setview(datetimelayout) .setpositivebutton("设置", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbutton) { inputdate.settext(datetime); } }) .setnegativebutton("取消", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbutton) { inputdate.settext(""); } }).show(); ondatechanged(null, 0, 0, 0); return ad; } public void ontimechanged(timepicker view, int hourofday, int minute) { ondatechanged(null, 0, 0, 0); } public void ondatechanged(datepicker view, int year, int monthofyear, int dayofmonth) { // 获得日历实例 calendar calendar = calendar.getinstance(); calendar.set(datepicker.getyear(), datepicker.getmonth(), datepicker.getdayofmonth(), timepicker.getcurrenthour(), timepicker.getcurrentminute()); simpledateformat sdf = new simpledateformat("yyyy年mm月dd日 hh:mm"); datetime = sdf.format(calendar.gettime()); ad.settitle(datetime); } /** * 实现将初始日期时间2012年07月02日 16:45 拆分成年 月 日 时 分 秒,并赋值给calendar * * @param initdatetime * 初始日期时间值 字符串型 * @return calendar */ private calendar getcalendarbyinintdata(string initdatetime) { calendar calendar = calendar.getinstance(); // 将初始日期时间2012年07月02日 16:45 拆分成年 月 日 时 分 秒 string date = splitestring(initdatetime, "日", "index", "front"); // 日期 string time = splitestring(initdatetime, "日", "index", "back"); // 时间 string yearstr = splitestring(date, "年", "index", "front"); // 年份 string monthandday = splitestring(date, "年", "index", "back"); // 月日 string monthstr = splitestring(monthandday, "月", "index", "front"); // 月 string daystr = splitestring(monthandday, "月", "index", "back"); // 日 string hourstr = splitestring(time, ":", "index", "front"); // 时 string minutestr = splitestring(time, ":", "index", "back"); // 分 int currentyear = integer.valueof(yearstr.trim()).intvalue(); int currentmonth = integer.valueof(monthstr.trim()).intvalue() - 1; int currentday = integer.valueof(daystr.trim()).intvalue(); int currenthour = integer.valueof(hourstr.trim()).intvalue(); int currentminute = integer.valueof(minutestr.trim()).intvalue(); calendar.set(currentyear, currentmonth, currentday, currenthour, currentminute); return calendar; } /** * 截取子串 * * @param srcstr * 源串 * @param pattern * 匹配模式 * @param indexorlast * @param frontorback * @return */ public static string splitestring(string srcstr, string pattern, string indexorlast, string frontorback) { string result = ""; int loc = -1; if (indexorlast.equalsignorecase("index")) { loc = srcstr.indexof(pattern); // 取得字符串第一次出现的位置 } else { loc = srcstr.lastindexof(pattern); // 最后一个匹配串的位置 } if (frontorback.equalsignorecase("front")) { if (loc != -1) result = srcstr.substring(0, loc); // 截取子串 } else { if (loc != -1) result = srcstr.substring(loc + 1, srcstr.length()); // 截取子串 } return result; } }
/datetimepicker/src/com/wwj/datetimepicker/datetimepickeractivity.java
package com.wwj.datetimepicker; import android.app.activity; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.edittext; /** * 时间拾取器界面 * * @author wwj_748 * */ public class datetimepickeractivity extends activity { /** called when the activity is first created. */ private edittext startdatetime; private edittext enddatetime; private string initstartdatetime = "2013年9月3日 14:44"; // 初始化开始时间 private string initenddatetime = "2014年8月23日 17:44"; // 初始化结束时间 @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); // 两个输入框 startdatetime = (edittext) findviewbyid(r.id.inputdate); enddatetime = (edittext) findviewbyid(r.id.inputdate2); startdatetime.settext(initstartdatetime); enddatetime.settext(initenddatetime); startdatetime.setonclicklistener(new onclicklistener() { public void onclick(view v) { datetimepickdialogutil datetimepickdialog = new datetimepickdialogutil( datetimepickeractivity.this, initenddatetime); datetimepickdialog.datetimepickdialog(startdatetime); } }); enddatetime.setonclicklistener(new onclicklistener() { public void onclick(view v) { datetimepickdialogutil datetimepickdialog = new datetimepickdialogutil( datetimepickeractivity.this, initenddatetime); datetimepickdialog.datetimepickdialog(enddatetime); } }); } }
自定义界面:
/datetimepicker/res/layout/common_datetime.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <datepicker android:id="@+id/datepicker" android:layout_width="wrap_content" android:layout_height="wrap_content" android:calendarviewshown="false" /> <timepicker android:id="@+id/timepicker" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </linearlayout>
源码下载:demo
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。