在 EditText 之间自动传递焦点

Automatically passing focus betwen EditText

由于我提出的关于 separating characters that are typed in an EditText into groups or blocks 的问题没有受到太多关注,我带着另一个问题来到这里,用另一种方式来完成我需要的事情。

所以,我的想法是有四个长度为 4 个字符的 EditText。现在我想要的是在达到限制时将焦点更改为用户类型,从一个 EdutText 更改为另一个。 示例:假设我们有 EditText A、B、C、D;首先获得焦点的是EditText A,然后当A处的字符长度达到4时,焦点传递到EditText B,依此类推并停止在D。

我正在尝试类似下面的代码,但是当长度达到 4 个字符时不会请求焦点。

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) {

        }
    });

所以我在这里询问是否有办法做到这一点。

不是在 CharSeq 长度的基础上添加验证,而是在 edittext 文本的基础上添加验证,如下所示:

替换

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

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

注意:同时更新您的条件。

currEdittext 将是 inputFieldAinputFieldBinputFieldC,具体取决于您的要求

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

在 onTextChanged() 方法中替换此行。

在 afterTextChanged 方法中写入您的条件并进行如下更改:

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)
            }
        }
    });