我有一个EditText,当我点击它时,他会显示adatepicker。 我希望当我点击我的editText时,日期出现在第一个选项中,并在选择月和日之后。
这是我的代码,一个简单的倾听者
date.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
mYear = myCalendar.get(Calendar.YEAR);
//add+1 month, mycalendar start at month 0
mmMonth = myCalendar.get(Calendar.MONTH);
mMonth = mmMonth + 1;
mDayOfMonth = myCalendar.get(Calendar.DAY_OF_MONTH);
final DatePickerDialog mDatePicker = new DatePickerDialog(Register.this, new DatePickerDialog.OnDateSetListener() {
@Override
//method for setDate to edit text
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
//set edittext to selected date by user
if (year <= (mYear - 18) && date.toString().length() >= 5) {
date.setText(dayOfMonth + "/" + monthOfYear + "/" + year);
setAnnee(mYear);
setMois(mMonth);
setJour(mDayOfMonth);
date.setTextColor(Color.BLACK);
} else {
date.setText("wrong");
date.setTextColor(Color.RED);
setAnnee(0);
setMois(0);
setJour(0);
}
}
}, mYear, mMonth, mDayOfMonth);
mDatePicker.setTitle("Select birthday date");
mDatePicker.show();
}
提前谢谢!