Android开发 自定义日期选择对话框(扩展功能)

Android提供了DatePickerDialog和TimePickerDialog控件用于弹出日期和时间选择对话框,单是它们都是独立的,不能方便的设置:”yyy-MM-dd HH:mm:ss“型日期时间,所以我们需要自己定义DatePickerDialog组件来设置。

首先需要写布局界面:该界面的XML代码如下:

布局名称为:datetime.xml

<?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">
	 <DatePicker
	          android:id="@+id/datepicker"
		  android:layout_width="fill_parent"
		  android:layout_height="wrap_content" />	
	 <TimePicker
	          android:id="@+id/timepicker"
		  android:layout_width="fill_parent"
		  android:layout_height="wrap_content" />
</LinearLayout>

分析上述代码:在布局中放入两个时间选择空间,一个用来显示日期,一个用来显示时间

然后我们开始写自定义的时间选择器的类,代码如下:

类的名称为:DateTimePickerDialog

package com.jinbi.app.ui;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
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;

import com.jinbi.R;

public class DateTimePickerDialog implements OnDateChangedListener,OnTimeChangedListener{
	private DatePicker datePicker;
	private TimePicker timePicker;
	private AlertDialog ad;
	private String dateTime;
	private String initDateTime;
	private Activity activity;
	
	/**
	 * 日期时间弹出选择框构
	 * @param activity:调用的父activity
	 */
	public DateTimePickerDialog(Activity activity) {
		this.activity = activity;
	}
	
	public void init(DatePicker datePicker,TimePicker timePicker) {
		Calendar calendar = Calendar.getInstance();
		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)+
					   calendar.get(Calendar.SECOND);
		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));
	}
	
	public AlertDialog dateTimePickDialog(final EditText dateTimeTextEdite,int type) {
		Calendar c = Calendar.getInstance();
		switch (type) {
		case 1:
			new DatePickerDialog(activity, 
					new DatePickerDialog.OnDateSetListener() {
						public void onDateSet(DatePicker view, int year, int monthOfYear,
								int dayOfMonth) {
							Calendar calendar = Calendar.getInstance();
							calendar.set(datePicker.getYear(), datePicker.getMonth(),datePicker.getDayOfMonth());
							SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd");
							dateTime = sdf.format(calendar.getTime());
							dateTimeTextEdite.setText(dateTime);
						}
					},
					c.get(Calendar.YEAR), 
					c.get(Calendar.MONTH), 
					c.get(Calendar.DATE)).show();
			return null;
		case 2:
			new TimePickerDialog(activity,
					new TimePickerDialog.OnTimeSetListener() {
						@Override
						public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
							Calendar calendar = Calendar.getInstance();
							calendar.set(Calendar.YEAR,Calendar.MONTH,Calendar.DAY_OF_MONTH,
									     timePicker.getCurrentHour(),timePicker.getCurrentMinute());
							SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
							dateTime = sdf.format(calendar.getTime());
							dateTimeTextEdite.setText(dateTime);
						}
					},
					c.get(Calendar.HOUR_OF_DAY),
					c.get(Calendar.MINUTE),
					true).show();
			return null;
		default:
			LinearLayout dateTimeLayout = (LinearLayout)activity.getLayoutInflater().inflate(R.layout.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)
					        {
					         dateTimeTextEdite.setText(dateTime);
					        }
					       }).setNegativeButton("取消",
					       new DialogInterface.OnClickListener()
					       {
					        public void onClick(DialogInterface dialog,
					          int whichButton)
					        {
					         dateTimeTextEdite.setText("");
					        }
					       }).show();
			onDateChanged(null, 0, 0, 0);
			return ad;
		}
		}
	
	@Override
	public void onTimeChanged(TimePicker arg0, int arg1, int arg2) {
		onDateChanged(null, 0, 0, 0);
	}

	@Override
	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("yyy-MM-dd HH:mm:ss");
		dateTime = sdf.format(calendar.getTime());
		ad.setTitle(dateTime);
	}
}
至此,该控件就算写完成了

使用该控件的方法,如下:

		dietTime.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				   DateTimePickerDialog dateTimePicKDialog = new DateTimePickerDialog(
						     RecordActivity.this);
			       dateTimePicKDialog.dateTimePickDialog(dietTime, 0);
			}
		});

由于项目是直接部署在手机上的,所以截图不大方便,现在先不截图了,以后有机会再上图。

如果不太明白的,提供一些参考资料:

自定义日期和时间Demo



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值