天天看點

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”就行了