Automatically passing focus betwen EditText

2019-08-03 11:56发布

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 alternative way to accomplish what I need.

So, my idea is to have four EditText that have 4 char length. Now what I want is to change the focus as the user type, from an EdutText to another when the limit is reached. Example: Let's say we have EditText A, B, C, D; the first to gain focus is EditText A, then when the chars length at A reach 4, the focus passes to EditText B and so on and stops At D.

I'm trying something like the code bellow, but the focus is not requested when the length reaches 4 chars.

inputFieldA.requestFocus();

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

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

        }

        @Override
        public void afterTextChanged(Editable s) {

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

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

        }

        @Override
        public void afterTextChanged(Editable s) {

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

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

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
    inputFieldD.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            if(s.toString().trim().length() == 4){
                //send all to Main EditText (A + B + C + D)
            }
        }

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

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

So I'm here asking if there is a way to do that.

3条回答
贼婆χ
2楼-- · 2019-08-03 12:36
 if(inputFieldA.toString().trim().length() == 4){
            inputFieldB.requestFocus();
  }

replace this lines in onTextChange() methode.

查看更多
时光不老,我们不散
3楼-- · 2019-08-03 12:38

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

查看更多
孤傲高冷的网名
4楼-- · 2019-08-03 12:44

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)
            }
        }
    });
查看更多
登录 后发表回答