从自定义DatePicker DialogPreference调用自定义时间DialogPreference

时间:2013-02-20 16:12:00

标签: android custom-controls timepicker listpreference android-datepicker

我需要在项目中实现自定义TimePicker和DatePicker DialogPreferences。应调用第一个DatePicker对话框,然后调用TimePicker。我在thisthis教程的帮助下将它们都实现到我的项目中。

我认为我应该在DatePreference的onDialogClosed方法中调用TimePreference。 我这样做:

    @Override
protected void onDialogClosed(boolean shouldSave) {
    if (shouldSave && this.changedValueCanBeNull != null) {
      setTheDate(this.changedValueCanBeNull);
      this.changedValueCanBeNull = null;
      Log.i("curr context", ctx.getClass().toString());
      try{

          new TimePreference(ctx,attrs).getDialog().show();
      }
      catch(Exception e)
      {
          e.printStackTrace();
      }
    }
}

但是我得到一个NullPointerException。问题是如何在DatePreference工作之后以编程方式调用我的自定义TimePreference对话框,并将两个值都设置为DatePreference。或者我应该创建不同的自定义ListPreference,它应该实现这两个项目?

1 个答案:

答案 0 :(得分:0)

我创建了自定义View,它看起来像ListPreference并实现了这个解决方案。

  private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
            public void onTimeSet(TimePicker view, int hourOfDay, int minuteOfHour) {
              mHour = hourOfDay;
              mMinute = minuteOfHour;
              updateDisplay();

            }
          };

             DatePickerDialog.OnDateSetListener mDateSetListener =
                     new DatePickerDialog.OnDateSetListener() {
                     public void onDateSet(DatePicker view, int year, int monthOfYear,
                                     int dayOfMonth) {
                             mYear = year;
                             mMonth = monthOfYear;
                             mDay = dayOfMonth;

                             updateDisplay();
                     }
             };


@Override
        protected Dialog onCreateDialog(int id) {
                switch (id) {
                case DATE_DIALOG_ID:
                        return new DatePickerDialog(this,
                                mDateSetListener,
                                mYear, mMonth, mDay);

                case TIME_DIALOG_ID:
                    return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute,
                        false);
                }
                return null;
        }
        protected void onPrepareDialog(int id, Dialog dialog) {
                switch (id) {
                case DATE_DIALOG_ID:
                        ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
                        break;}
                }
        private void updateDisplay() {
            String minutes = null;
            if (mMinute>9)
                minutes = Integer.toString(mMinute);
            else 
                minutes = "0"+Integer.toString(mMinute);
            if (mMonth<10)
            deadlineCus.setSummary(
                    (new StringBuilder()
                    // Month is 0 based so add 1
                    .append(mYear).append("-").append("0")
                    .append(mMonth + 1).append("-")
                    .append(mDay)
                    .append(" ").append(mHour).append(":").append(minutes)).toString()
                    );
            else 
                deadlineCus.setSummary(
                        (new StringBuilder()
                        // Month is 0 based so add 1
                        .append(mYear).append("-")
                        .append(mMonth + 1).append("-")
                        .append(mDay)
                        .append(" ").append(mHour).append(":").append(minutes)).toString()
                        );
            showDialog(TIME_DIALOG_ID);

    }
onCreate()方法中的

 DatePickerDialog.OnDateSetListener mDateSetListener =
                    new DatePickerDialog.OnDateSetListener() {
                    public void onDateSet(DatePicker view, int year, int monthOfYear,
                                    int dayOfMonth) {
                            mYear = year;
                            mMonth = monthOfYear;
                            mDay = dayOfMonth;

                            updateDisplay();
                    }


            };
            TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
                public void onTimeSet(TimePicker view, int hourOfDay, int minuteOfHour) {
                  mHour = hourOfDay;
                  mMinute = minuteOfHour;
                  updateDisplay();

                }
              };

  deadlineCus.setOnTouchListener(new OnTouchListener()
      {

        public boolean onTouch(View arg0, MotionEvent arg1) 

        {
              showDialog(DATE_DIALOG_ID);
              return false;
        }

      });