Android学习——时间选择对话框(月/日/时/分)

利用3个 NumberPicker分别表示月/日/时/分,当时间发生变化时,需要用到回掉函数来改变DateTimePickerDialog中标题栏。


datetime_picker.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_gravity="center_horizontal" >

    <NumberPicker
        android:id="@+id/date"
        android:layout_width="120dip"
        android:layout_height="wrap_content" 
        android:focusable="true"
        android:focusableInTouchMode="true"/>

    <NumberPicker
        android:id="@+id/hour"
        android:layout_width="50dip"
        android:layout_height="wrap_content" 
        android:focusable="true"
        android:focusableInTouchMode="true"/>

    <NumberPicker
        android:id="@+id/minute"
        android:layout_width="50dip"
        android:layout_height="wrap_content" 
        android:focusable="true"
        android:focusableInTouchMode="true"/>

  

</LinearLayout>


DateTimePicker:

package com.example.ddd;

import java.util.Calendar;
import android.annotation.SuppressLint;
import android.content.Context;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnValueChangeListener;

@SuppressLint("NewApi")
public class DateTimePicker extends FrameLayout {

	private OnValueChangeListener mOnDateChangedListener = new OnValueChangeListener() {

		@Override
		public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
			calendar.add(Calendar.DAY_OF_YEAR, newVal - oldVal);
			updateDateControl();
			onDateTimeChanged();
		}

	};

	private OnValueChangeListener mOnHourChangedListener = new OnValueChangeListener() {

		@Override
		public void onValueChange(NumberPicker picker, int oldVal, int newVal) {

			updateHourControl();
			onDateTimeChanged();
		}

	};

	private OnValueChangeListener mOnMinuteChangedListener = new OnValueChangeListener() {

		@Override
		public void onValueChange(NumberPicker picker, int oldVal, int newVal) {

			updateMinuteControl();
			onDateTimeChanged();
		}

	};

	private NumberPicker datePicker;
	private NumberPicker hourPicker;
	private NumberPicker minutePicker;
	private NumberPicker amPmPicker;

	private Calendar calendar;

	private OnDateTimeChangedListener onDateTimeChangedListener;

	@SuppressLint("NewApi")
	public DateTimePicker(Context context) {
		super(context);
		inflate(context, R.layout.datetime_picker, this);
		calendar = Calendar.getInstance();
		datePicker = (NumberPicker) findViewById(R.id.date);
		datePicker.setMinValue(0);
		datePicker.setMaxValue(6);
		datePicker.setOnValueChangedListener(mOnDateChangedListener);
		updateDateControl();

		hourPicker = (NumberPicker) findViewById(R.id.hour);
		hourPicker.setMinValue(0);
		hourPicker.setMaxValue(23);
		hourPicker.setValue(calendar.get(Calendar.HOUR_OF_DAY));
		hourPicker.setOnValueChangedListener(mOnHourChangedListener);

		minutePicker = (NumberPicker) findViewById(R.id.minute);
		minutePicker.setMinValue(0);
		minutePicker.setMaxValue(59);
		minutePicker.setValue(calendar.get(Calendar.MINUTE));
		minutePicker.setOnValueChangedListener(mOnMinuteChangedListener);


	}

	private void updateDateControl() {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(calendar.getTimeInMillis());
		cal.add(Calendar.DAY_OF_YEAR, -4);
		String[] dateP = new String[7];
		for (int i = 0; i < 7; i++) {
			cal.add(Calendar.DAY_OF_YEAR, 1);
			dateP[i] = (String) DateFormat.format("MM.dd EEEE", cal);
		}
		datePicker.setDisplayedValues(dateP);
		datePicker.setValue(3);
	}

	private void updateHourControl() {

		calendar.set(Calendar.HOUR_OF_DAY, hourPicker.getValue());

	}

	private void updateMinuteControl() {
		calendar.set(Calendar.MINUTE, minutePicker.getValue());
	}

	public void setCalendar(long time) {
		calendar.setTimeInMillis(time);
	}

	public interface OnDateTimeChangedListener {
		void onDateTimeChanged(long milliseconds);

	}

	public void setOnDateTimeChangedListener(
			OnDateTimeChangedListener onDateTimeChangedListener) {
		this.onDateTimeChangedListener = onDateTimeChangedListener;
	}

	protected void onDateTimeChanged() {
		if (onDateTimeChangedListener != null) {
			onDateTimeChangedListener.onDateTimeChanged(getMilliseconds());
		}

	}

	private long getMilliseconds() {
		return calendar.getTimeInMillis();
	}

}

DateTimePickerDialog:

package com.example.ddd;

import java.util.Calendar;

import com.example.ddd.DateTimePicker.OnDateTimeChangedListener;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.text.format.DateUtils;

public class DateTimePickerDialog extends AlertDialog implements
		OnClickListener {

	private DateTimePicker dateTimePicker;
	private Calendar calendar;

	protected DateTimePickerDialog(Context context) {
		super(context);

		setButton2("取消", (OnClickListener) null);
		setButton(DialogInterface.BUTTON_POSITIVE, "确定", this);

		calendar = Calendar.getInstance();
		setTitle("hello");
		dateTimePicker = new DateTimePicker(context);
		dateTimePicker.setCalendar(calendar.getTimeInMillis());
		dateTimePicker
				.setOnDateTimeChangedListener(new OnDateTimeChangedListener() {

					@Override
					public void onDateTimeChanged(long milliseconds) {

						calendar.setTimeInMillis(milliseconds);
						updateTitle();
					}
				});
		setView(dateTimePicker);
		updateTitle();

	}

	protected void updateTitle() {
		int flag = DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE
				| DateUtils.FORMAT_SHOW_TIME;

		setTitle(DateUtils.formatDateTime(this.getContext(),
				calendar.getTimeInMillis(), flag));

	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		// TODO Auto-generated method stub

	}

}



你可能感兴趣的:(TimePicker,时间选择)