Search code examples
javaandroidandroid-activityandroid-edittext

Put a slash in the date of a EditText


I readed a lot of similar questions, but no one answers mine or can solve my problem. I have a EditText like this in the layout:

    <EditText
        android:id="@+id/editText2"
        android:layout_width="248dp"
        android:layout_height="59dp"
        android:layout_marginStart="21dp"
        android:layout_marginTop="36dp"
        android:width="360dp"
        android:ems="5"
        android:hint="@string/ultimos4Dig"
        android:inputType="number"
        android:maxLength="10"
        android:textSize="24sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

I need that when the user are writing the numers of the month and year, a slash appear or disappearwhen he is writing. If he writes 2 numbers then a slash needs to appear. If he erases and its only one number, then the slash needs to disappear.

I need that in the editText appears the date as: 14/06

Here is my code but its not working.

  protected void onCreate(Bundle savedInstanceState) {

        EditText editText2 = (EditText)  findViewById(R.id.editText2);
        editText2.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }
            @Override
            public void afterTextChanged(Editable text) {
                if (text.length() == 2) {
                    text.append('/');
                }
            }
        });

FYI i made a class for a solution of this, thanks


Solution

  • Thanks you, after some days i got the solution, this class:

    public class EditMMYY extends AppCompatEditText implements TextWatcher
    {
        private String sPrev = "";
        private int iMon = 0;
        private int iYear = 0;
    
        private void InitValue()
        {
            setInputType(InputType.TYPE_CLASS_NUMBER);
            setFilters(new InputFilter[] {new InputFilter.LengthFilter(5)});
            setHint("MM/YY");
        }
    
        public EditMMYY(Context context)
        {
            super(context);
            InitValue();
        }
    
        public EditMMYY(Context context, AttributeSet attrs)
        {
            super(context, attrs);
            InitValue();
        }
    
        public EditMMYY(Context context, AttributeSet attrs, int defStyleAttr)
        {
            super(context, attrs, defStyleAttr);
            InitValue();
        }
    
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {
            // Chequeo que el ingreso sea MM/YY
            String sNew = s.toString();
            int newLen = sNew.length();
    
            if(sNew.equals(sPrev))
            {
                return;
            }
            switch(newLen)
            {
                case 0:
                    iMon = 0;
                    iYear = 0;
                    sPrev = sNew;
                    break;
                case 1:
                    iMon  = Integer.parseInt(sNew);
                    iYear = 0;
                    if(sPrev.length() == 0 && iMon > 1)
                    {    // Si se escribe un número mayor que 1, lo tomo como mes
                        sPrev = String.format("%02d/", iMon);
                    }
                    else
                    {
                        sPrev = sNew;
                    }
                    break;
                case 2:
                    iMon  = Integer.parseInt(sNew);
                    iYear = 0;
                    if(sPrev.length() == 1)
                    {
                        // Si ya es un mes válido, lo completo, sino dejo
                        // sPrev sin cambios hasta que se ingrese algo válido
                        if(iMon >= 1 && iMon <= 12)
                        {
                            sPrev = String.format("%02d/", iMon);
                        }
                    }
                    else
                    {
                        sPrev = sNew;
                    }
                    break;
                case 3:
                    iMon  = Integer.parseInt(sNew.substring(0, 2));
                    iYear = 0;
                    if(sPrev.length() == 2)
                    {
                        iMon = Integer.parseInt(sNew.substring(0, 2));
                        iYear = Integer.parseInt(sNew.substring(2, 3));
                        sPrev = String.format("%02d/%d", iMon, iYear);
                    }
                    else
                    {
                        sPrev = sNew;
                    }
                    break;
                case 4:
                case 5:
                    iMon = Integer.parseInt(sNew.substring(0, 2));
                    iYear = Integer.parseInt(sNew.substring(3, newLen));
                    sPrev = sNew;
                    break;
                default:
                    sPrev = sNew;
                    break;
            }
            setText(sPrev);
            setSelection(sPrev.length());
        }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after)
        {
        }
    
        @Override
        public void afterTextChanged(Editable s)
        {
    
        }
    
        public int getMon()
        {
            return iMon;
        }
    
        public int getYear()
        {
            return iYear;
        }
    }