api级别小于11的DatePickerDialog

时间:2015-01-21 06:54:50

标签: android datepicker

我正在尝试在我的应用程序中使用DatePickerDialog。它在API级别大于11的每个设备中都能正常工作,但它的api级别低于11。

这是我的DatePickerDialog

package com.rh.bookmany.fragments;

import java.util.Calendar;

import com.rh.bookmany.interfaces.OnShowDateSelectedListener;


import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.widget.DatePicker;

//DatePicker
    public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {


        int max;        
        OnShowDateSelectedListener onShowDateSelected;

        public DatePickerFragment(,int max) {
            this.max = max;
        }

        public Dialog onCreateDialog(Bundle savedInstanceState) {

            // Use the current date as the default date in the picker
            final Calendar c = Calendar.getInstance();
            final Calendar c2 = Calendar.getInstance();
            c2.add(Calendar.DATE, max);
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);


            // new instance of DatePickerDialog
            DatePickerDialog dd = new DatePickerDialog(getActivity(),this,year, month, day);
            dd.getDatePicker().setMinDate(c.getTimeInMillis()); //ERROR HERE
            dd.getDatePicker().setMaxDate(c2.getTimeInMillis()); //ERROR HERE

            /*
                Lint says: 

                Call requires API level 11 (current min is 8): android.app.DatePickerDialog#getDatePicker
                Call requires API level 11 (current min is 8): android.widget.DatePicker#setMinDate
                Call requires API level 11 (current min is 8): android.widget.DatePicker#setMaxDate 
            */


            return dd;
        }

        @Override
        public void onDateSet(DatePicker view, int year, int month, int day) {

            Log.d("X","Date Picked:"+year+"-"+month+"-"+day);


        }

    }

我称之为

// Showing Date Picker with 4 days after today
DialogFragment datePickerFragment = new DatePickerFragment(4);
datePickerFragment.show(getActivity().getSupportFragmentManager(),"datepicker");

并且lint像这样说

Call requires API level 11 (current min is 8): android.app.DatePickerDialog#getDatePicker
Call requires API level 11 (current min is 8): android.widget.DatePicker#setMinDate
Call requires API level 11 (current min is 8): android.widget.DatePicker#setMaxDate

我的问题是:

是否可以在小于11的api级别(getDatePicker(),setMinDate(),setMaxDate())中使用这三种方法。

如果是的话怎么样?别的 - 有没有其他选择?

1 个答案:

答案 0 :(得分:0)

如果日期超出范围(我的意思是在最小和最大范围内),你可以开始日期时间选择器

Calendar cal = Calendar.getInstance(Locale.getDefault());
        DatePickerDialog dd = new DatePickerDialog(getActivity(),this,year, month, day);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                    dd.getDatePicker().setMinDate(c.getTimeInMillis()); 
                    dd.getDatePicker().setMaxDate(c2.getTimeInMillis());

        } else {
            final int minYear = c.getTimeInMillis().get(Calendar.YEAR);
            final int minMonth = c.getTimeInMillis().get(Calendar.MONTH);
            final int minDay = c.getTimeInMillis().get(Calendar.DAY_OF_MONTH);

          final int maxYear = c2.getTimeInMillis().get(Calendar.YEAR);
            final int maxMonth = c2.getTimeInMillis().get(Calendar.MONTH);
            final int maxDay = c2.getTimeInMillis().get(Calendar.DAY_OF_MONTH);

            this.datePicker.init(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
                                cal.get(Calendar.DAY_OF_MONTH),
                    new OnDateChangedListener() {

                        public void onDateChanged(DatePicker view, int year,
                                int month, int day) {
                            Calendar newDate = Calendar.getInstance();
                            newDate.set(year, month, day);

                            if (c.after(newDate)) {
                                view.init(minYear, minMonth, minDay, this);
                            }else if(c2.before(newDate){
                    view.init(maxYear, maxMonth, maxDay, this);
                     }
                        }
                    });

        }