天天看点

android 日期时间选择器

在做项目时,需要使用时间选择器,找了DatePickerDialog来选择显示日期时间。在这总结一下怎么去使用和遇到的一些问题及解决方法

android 日期时间选择器

首先需要两个类:DateTimePicker.java DateTimePickerDialog.java。包括dialog的布局和实现回掉方法。

DateTimePicker.java

public class DateTimePicker extends FrameLayout
{
    private final NumberPicker mDateSpinner;
    private final NumberPicker mHourSpinner;
    private final NumberPicker mMinuteSpinner;
    private Calendar mDate;
    private int mHour,mMinute; 
    private String[] mDateDisplayValues = new String[];
    private OnDateTimeChangedListener mOnDateTimeChangedListener;

    public DateTimePicker(Context context)
    {
        super(context);
         mDate = Calendar.getInstance();

         mHour=mDate.get(Calendar.HOUR_OF_DAY);
         mMinute=mDate.get(Calendar.MINUTE);

         inflate(context, R.layout.datedialog, this);

         mDateSpinner=(NumberPicker)this.findViewById(R.id.np_date);
         mDateSpinner.setMinValue();
         mDateSpinner.setMaxValue();
         updateDateControl();
         mDateSpinner.setOnValueChangedListener(mOnDateChangedListener);

         mHourSpinner=(NumberPicker)this.findViewById(R.id.np_hour);
         mHourSpinner.setMaxValue();
         mHourSpinner.setMinValue();
         mHourSpinner.setValue(mHour);
         mHourSpinner.setOnValueChangedListener(mOnHourChangedListener);

         mMinuteSpinner=(NumberPicker)this.findViewById(R.id.np_minute);
         mMinuteSpinner.setMaxValue();
         mMinuteSpinner.setMinValue();
         mMinuteSpinner.setValue(mMinute);
         mMinuteSpinner.setOnValueChangedListener(mOnMinuteChangedListener);
    }

    private NumberPicker.OnValueChangeListener mOnDateChangedListener=new OnValueChangeListener()
    {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal)
        {
            mDate.add(Calendar.DAY_OF_MONTH, newVal - oldVal);
            updateDateControl();
            onDateTimeChanged();
        }
    };

    private NumberPicker.OnValueChangeListener mOnHourChangedListener=new OnValueChangeListener()
    {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal)
        {
            mHour=mHourSpinner.getValue();
            onDateTimeChanged();
        }
    };

      private NumberPicker.OnValueChangeListener mOnMinuteChangedListener=new OnValueChangeListener()
        {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal)
            {
                mMinute=mMinuteSpinner.getValue();
                onDateTimeChanged();
            }
        };

    private void updateDateControl() 
    {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(mDate.getTimeInMillis());
        cal.add(Calendar.DAY_OF_YEAR, - /  - );
        mDateSpinner.setDisplayedValues(null);
        for (int i = ; i < ; ++i) 
        {
            cal.add(Calendar.DAY_OF_YEAR, );
            mDateDisplayValues[i] = (String) DateFormat.format("MM.dd EEEE", cal);
        }
        mDateSpinner.setDisplayedValues(mDateDisplayValues);
        mDateSpinner.setValue( / );
        mDateSpinner.invalidate();
    }

      public interface OnDateTimeChangedListener 
      {
            void onDateTimeChanged(DateTimePicker view, int year, int month, int day,int hour,int minute);
      }

      public void setOnDateTimeChangedListener(OnDateTimeChangedListener callback) 
      {
            mOnDateTimeChangedListener = callback;
       }

      private void onDateTimeChanged() 
      {
            if (mOnDateTimeChangedListener != null)
            {
                mOnDateTimeChangedListener.onDateTimeChanged(this, mDate.get(Calendar.YEAR),
                        mDate.get(Calendar.MONTH), mDate.get(Calendar.DAY_OF_MONTH),mHour, mMinute);
            }
        }
}
           

DateTimePickerDialog.java

public class DateTimePickerDialog extends AlertDialog implements OnClickListener
{
    private DateTimePicker mDateTimePicker;
    private Calendar mDate = Calendar.getInstance();
    private OnDateTimeSetListener mOnDateTimeSetListener;

    @SuppressWarnings("deprecation")
    public DateTimePickerDialog(Context context, long date) 
    {
        super(context);
        mDateTimePicker = new DateTimePicker(context);
        setView(mDateTimePicker);
        mDateTimePicker.setOnDateTimeChangedListener(new OnDateTimeChangedListener()
        {
            @Override
            public void onDateTimeChanged(DateTimePicker view, int year, int month, int day, int hour, int minute)
            {
                mDate.set(Calendar.YEAR, year);
                mDate.set(Calendar.MONTH, month);
                mDate.set(Calendar.DAY_OF_MONTH, day);
                mDate.set(Calendar.HOUR_OF_DAY, hour);
                mDate.set(Calendar.MINUTE, minute);
                mDate.set(Calendar.SECOND, );
                updateTitle(mDate.getTimeInMillis());
            }
        });

        setButton("设置", this);
        setButton2("取消", (OnClickListener)null);
        mDate.setTimeInMillis(date);
        updateTitle(mDate.getTimeInMillis()); 
    }

    public interface OnDateTimeSetListener 
    {
        void OnDateTimeSet(AlertDialog dialog, long date);
    }

    private void updateTitle(long date) 
    {
        int flag = DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY| DateUtils.FORMAT_SHOW_TIME;
        setTitle(DateUtils.formatDateTime(this.getContext(), date, flag));
    }

    public void setOnDateTimeSetListener(OnDateTimeSetListener callBack)
    {
        mOnDateTimeSetListener = callBack;
    }

    public void onClick(DialogInterface arg0, int arg1)
    {
        if (mOnDateTimeSetListener != null) 
        {
            mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis());
        }
    }
}
           

剩下的就是在activity中使用了

DateTimePickerDialog dialog  = new DateTimePickerDialog(this, System.currentTimeMillis());
        dialog.setOnDateTimeSetListener(new OnDateTimeSetListener()
          {
            public void OnDateTimeSet(AlertDialog dialog, long date)
            {
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String dateString = formatter.format(date);
                Toast.makeText(getApplication(), dateString, Toast.LENGTH_SHORT).show();
            }
        });
        dialog.show();
           

这样一个时间选择器就添加好了,可在这遇到一个问题,我的显示是这样的,好难看。。

android 日期时间选择器

查了一下,发现原因是在AndroidManifest.xml中,

activity的theme是android:theme=”@android:style/Theme.NoTitleBar”,在这种theme下的样式难看。

将样式改为android:theme=”@android:style/Theme.Holo.Light”就行了