Skip to content Skip to sidebar Skip to footer

Automatically Passing Focus Betwen Edittext

Since my question asking about separating characters that are typed in an EditText into groups or blocks didn't receive much attention, I've come here with another question on an a

Solution 1:

Write your conditions in afterTextChanged method and make changes as follows :

inputFieldA.requestFocus();

    inputFieldA.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if(inputFieldA.getText().toString().trim().length() == 4){
                inputFieldB.requestFocus();
            }
        }
    });
    inputFieldB.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if(inputFieldB.getText().toString().trim().length() == 4){
                inputFieldC.requestFocus();
            }
        }
    });
    inputFieldC.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if(inputFieldC.getText().toString().trim().length() == 4){
                inputFieldD.requestFocus();
            }
        }
    });
    inputFieldD.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if(inputFieldD.getText().toString().trim().length() == 4){
                //send all to Main EditText (A + B + C + D)
            }
        }
    });

Solution 2:

 if(inputFieldA.toString().trim().length() == 4){
            inputFieldB.requestFocus();
  }

replace this lines in onTextChange() methode.

Solution 3:

instead of adding validation on the basis of CharSeq length add validation on the basis of edittext text like this:

replace

    if(s.toString().trim().length() == 4)

with

    if (currEdittext.getText().toString().length() >= 4) 

Note: update your condition as well.

currEdittext will be either inputFieldA, inputFieldB, inputFieldC depending on your requirement

Post a Comment for "Automatically Passing Focus Betwen Edittext"