2015-01-21 55 views
1

我想在我的應用程序中使用DatePickerDialog。它在每個API級別大於11的設備上都能正常工作,但它在api級別上的工作不到11級。api級別小於11的DatePickerDialog

這裏是我的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"); 

和皮棉說,像這樣

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 

我的問題是:

它是位置嗎?能夠在api級別工作這三個方法小於11(getDatePicker(),setMinDate(),setMaxDate())。

如果是的話如何?其他 - 有沒有其他的選擇?

+0

http://stackoverflow.com/questions/10836679/android-datepicker-min- MAX-日期前-API級-11 – PPD 2015-01-21 07:06:48

回答

0

如果日期是要超出範圍就可以開始日期時間選擇器(我的意思是NT中的最小值和最大值)

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); 
        } 
         } 
        }); 

     } 
相關問題