显示时的Datepicker,首先选择日期

时间:2015-05-29 18:13:38

标签: java android datepicker actionlistener

我有一个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();

            }

enter image description here

提前谢谢!

0 个答案:

没有答案