用于选择年份的Android日历

时间:2015-08-08 06:15:20

标签: android

我在EditText点击上使用日历来选择用户的出生日期并在EditText中进行设置。它适用于所有设备。但仅在Swipe Sonic 4.1.2(API 16)中,它显示的是1980年以前的年份。用户不能选择低于1980年的年份。

这是我的代码:

 demo.setOnFocusChangeListener(new View.OnFocusChangeListener() {

public void onFocusChange(View v, boolean hasFocus) {
 if (hasFocus) {
DateDialog();
 }
}
});



public void DateDialog() {

        try {
            DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {

                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                    demo.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
                    edt_contact.requestFocus();
                    // checking for age should be  18 years and above
                    Calendar userAge = new GregorianCalendar(year, monthOfYear, dayOfMonth);
                    Calendar minAdultAge = new GregorianCalendar();
                    minAdultAge.add(Calendar.YEAR, -18);
                    if (minAdultAge.before(userAge)) {
                        Toast.makeText(getApplicationContext(), "Age should be 18 and above", Toast.LENGTH_LONG).show();

                        demo.setText("");
                        // demo.requestFocus();
                    }

                }
            };

            DatePickerDialog dpDialog = new DatePickerDialog(this, listener, year, month, day);
            dpDialog.setCancelable(true);
            dpDialog.show();

        } catch (Exception e)

        {

        e.printStackTrace();
        logsdata.logsdata("MainActivty", "DateDialog", e.getMessage().toString());
    }

}//DateDialog

1 个答案:

答案 0 :(得分:0)

如果你定位API> = 11,你可以尝试设置最小日期,如下所示:

DatePickerDialog dpDialog = new DatePickerDialog(this, listener, year, month, day);
dpDialog.setCancelable(true);
dpDialog.show();
dpDialog.getDatePicker().setMinDate(0); //This will set the minimum date to 01/January/1970