Code example for DatePicker

Methods: initsetEnabled, setFocusable

0
    /** 
     * Resets date to today. 
     */ 
    public void clearAnswer() { 
        final Calendar c = new GregorianCalendar();
        mDatePicker.init(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH),
                mDateListener);
    } 
 
 
    public IAnswerData getAnswer() { 
        // clear focus first so the datewidget gets the value in the text box 
        mDatePicker.clearFocus();
        Date d =
                new Date(mDatePicker.getYear() - YEARSHIFT, mDatePicker.getMonth(), mDatePicker
                        .getDayOfMonth());
        return new DateData(d);
    } 
 
 
    /** 
     * Build view for date answer. Includes retrieving existing answer. 
     */ 
    public void buildView(final PromptElement prompt) {
        final Calendar c = new GregorianCalendar();
 
        mDatePicker = new DatePicker(getContext());
        if (!prompt.isReadonly()) {
            mDatePicker.setFocusable(true);
            mDatePicker.setEnabled(true);
        } 
 
        mDateListener = new DatePicker.OnDateChangedListener() {
            public void onDateChanged(DatePicker view, int year, int month, int day) {
                if (prompt.isReadonly()) {
                    if (prompt.getAnswerValue() != null) {
                        Date d = (Date) prompt.getAnswerObject();
                        view.updateDate(d.getYear() + YEARSHIFT, d.getMonth(), d.getDate());
                    } else { 
                        view.updateDate(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c
                                .get(Calendar.DAY_OF_MONTH));
                    } 
                } else { 
                    // handle leap years and number of days in month 
                    // TODO 
                    // http://code.google.com/p/android/issues/detail?id=2081 
                    c.set(year, month, 1);
                    int max = c.getActualMaximum(Calendar.DAY_OF_MONTH);
                    if (day > max) {
                        view.updateDate(year, month, max);
                    } else { 
                        view.updateDate(year, month, day);
                    } 
                } 
            } 
        }; 
 
        if (prompt.getAnswerValue() != null) {
            Date d = (Date) prompt.getAnswerObject();
            mDatePicker.init(d.getYear() + YEARSHIFT, d.getMonth(), d.getDate(), mDateListener);
        } else { 
            // create date widget with now 
            clearAnswer(); 
        }