Date Validation in Android

后端 未结 6 1693
日久生厌
日久生厌 2020-12-03 09:05

I\'m new to Android programming and I\'m currently developing an app. Can someone help me on how to validate the date that is being input by the user if is it valid or not?<

6条回答
  •  一生所求
    2020-12-03 09:45

    DatePicker is quite painful to input birthday. Here is my solution:

    Utils

    public static final SimpleDateFormat BIRTHDAY_FORMAT_PARSER = new SimpleDateFormat("yyyy-MM-dd");
    public static final String DASH_STRING = "-";
    public static Calendar parseDateString(String date) {
            Calendar calendar = Calendar.getInstance();
            BIRTHDAY_FORMAT_PARSER.setLenient(false);
            try {
                calendar.setTime(BIRTHDAY_FORMAT_PARSER.parse(date));
            } catch (ParseException e) {}
            return calendar;
        }
    public static boolean isValidBirthday(String birthday) {
            Calendar calendar = parseDateString(birthday);
            int year = calendar.get(Calendar.YEAR);
            int thisYear = Calendar.getInstance().get(Calendar.YEAR);
            return year >= 1900 && year < thisYear;
        }
    

    Main Activity

    private void setupBirthdayEditText() {
            mBirthdayEditText.addTextChangedListener(new TextWatcher() {
                int beforeTextChangedLength;
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    beforeTextChangedLength = charSequence.length();
                }
    
                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    if (!TextUtils.isEmpty(mBirthdayTextInputLayout.getError())) {
                        mBirthdayTextInputLayout.setError(null);
                        mBirthdayTextInputLayout.setErrorEnabled(false);
                    }
                }
    
                @Override
                public void afterTextChanged(Editable editable) {
                    int length = editable.length();
                    // text is being removed
                    if (beforeTextChangedLength > length) return;
    
                    String str = editable.toString();
                    String[] strArr = str.split(Utils.DASH_STRING);
                    // only add dash after input year with zero dash and input month with one dash
                    if ((length == YEAR_LENGTH && strArr.length == 1) || (length == YEAR_MONTH_LENGTH && strArr.length == 2)) {
                        mBirthdayEditText.setText(str + Utils.DASH_STRING);
                        mBirthdayEditText.setSelection(mBirthdayEditText.length());
                    }
                }
            });
        }
    
    protected boolean isAllFieldsValid() {
            String birthday = mBirthdayEditText.getText().toString().trim();
            if (!Utils.isValidBirthday(birthday)) {
                mBirthdayTextInputLayout.setError(getString(R.string.error_birth_date));
                return false;
            }
    
        return true;
    }
    

    activity_main.xml

    
    
                    
    
                
    

    This is not perfect as we shouldn't check validation with try catch and user have to look to the date format during typing, but it is simple and convenient if you want to avoid default date picker.

提交回复
热议问题