1

我曾经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); 
4

1 回答 1

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

    }
};
于 2013-04-25T04:07:59.567 回答