在EditText

时间:2017-03-30 05:25:27

标签: android android-edittext autofocus

由于我提出的关于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) {

        }
    });

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

3 个答案:

答案 0 :(得分:1)

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

在onTextChange()方法中替换这一行。

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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

替换

c = INDEX(LINEST(y,LN(x)),1)
b = INDEX(LINEST(y,LN(x)),1,2)

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

注意:也要更新您的病情。

if (currEdittext.getText().toString().length() >= 4) 可以是currEdittextinputFieldAinputFieldB,具体取决于您的要求