限制用户在datepicker中选择将来的日期

时间:2013-04-25 03:55:17

标签: android

我使用DatePicker来计算用户输入生日日期的年龄。现在我想限制用户选择将来的日期......这是我在mainActivity.java中使用的代码

@SuppressLint("NewApi")

public static class DatePickerFragment extends DialogFragment                            
    implements DatePickerDialog.OnDateSetListener
{    
    @Override   
    public Dialog onCreateDialog(Bundle savedInstanceState) 
    {       
        // Use the current date as the default date in the picker        
        final Calendar c = Calendar.getInstance();        
        int mBirthYear = c.get(Calendar.YEAR);        
        int mMonthOfYear = c.get(Calendar.MONTH);        
        int mDayOfMonth= c.get(Calendar.DAY_OF_MONTH);        
        // Create a new instance of DatePickerDialog and return it       
return new DatePickerDialog(getActivity(), this, mBirthYear,mMonthOfYear ,mDayOfMonth  );

}           


public void onDateSet(DatePicker view, int mBirthYear, int mMonthOfYear, int mDayOfMonth) 
{
  int mAge;  
  mAge = Year-mBirthYear;
  if ((mMonthOfYear == Month && Day < mDayOfMonth) || ( Month < mMonthOfYear) )  
         {
                    mAge--;
          }                

                 String years = String.valueOf(mAge);
                  MainActivity.textView6.setText(years); 

1 个答案:

答案 0 :(得分:0)

这是反向操作。限制选择过期日期。

我不知道这个

的任何特殊方法

但我这样解决了,这可能不是标准的但对我有用

    public void onDateSet(DatePicker view, int selectedYear,
            int selectedMonth, int selectedDay) {
        final Calendar cf = Calendar.getInstance();
        year = cf.get(Calendar.YEAR);
        month = cf.get(Calendar.MONTH);
        day = cf.get(Calendar.DAY_OF_MONTH);

        // Log.e("year", selectedYear + "year " + year);
        if (selectedYear < year) {
            Toast.makeText(getApplicationContext(),
                    "Please choose future date", 1).show();
        }
        if (selectedMonth < month) {
            if (selectedYear <= year) {
                Toast.makeText(getApplicationContext(),
                        "Please choose future date", 1).show();
            } else {
                year = selectedYear;
                month = selectedMonth;
                day = selectedDay;

                // set selected date into textview
                shipdate.setText(new StringBuilder().append(month + 1)
                        .append("-").append(day).append("-").append(year)
                        .append(" "));
            }
        }
        if (selectedDay < day) {
            if (selectedMonth <= month) {
                if (selectedYear <= year) {
                    Toast.makeText(getApplicationContext(),
                            "Please choose future date", 1).show();

                } else {
                    year = selectedYear;
                    month = selectedMonth;
                    day = selectedDay;

                    // set selected date into textview
                    shipdate.setText(new StringBuilder().append(month + 1)
                            .append("-").append(day).append("-")
                            .append(year).append(" "));
                }

            } else {
                year = selectedYear;
                month = selectedMonth;
                day = selectedDay;

                // set selected date into textview
                shipdate.setText(new StringBuilder().append(month + 1)
                        .append("-").append(day).append("-").append(year)
                        .append(" "));
            }

        } else {
            year = selectedYear;
            month = selectedMonth;
            day = selectedDay;

            // set selected date into textview
            shipdate.setText(new StringBuilder().append(month + 1)
                    .append("-").append(day).append("-").append(year)
                    .append(" "));
        }

    }
};