Request focus on edittext is not working correctly

huangapple go评论82阅读模式
英文:

Request focus on edittext is not working correctly

问题

我有4个EditText,它们充当"输入您的PIN"字段,每个EditText设置为只能输入一个数字,然后请求焦点到下一个EditText。如果我为第一个PIN输入一个数字,它会请求焦点到第二个,然后第三个,依此类推。

现在我正在尝试的是,如果用户点击第二个EditText或第三个EditText,而第一个字段尚未输入,它应该返回并聚焦在第一个字段上。

我尝试使用OnTouchListener来实现这一点,我注意到的是,如果首先点击第二个EditText而不是点击左侧/第一个EditText,我可以看到焦点会返回到第一个EditText,但几乎在一瞬间又立刻返回到第二个EditText,最终仍然保持焦点在第二个EditText上。

我还尝试使用OnClickListener。它可以工作,但需要两次点击。所以我会点击第二个EditText,什么也不会发生,然后再次点击它,然后焦点会返回到第一个EditText,但我需要在第一次点击时发生。

以下是代码的一部分:

pin1EditTxt.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 (pin1EditTxt.getText().toString().length() == 1) {
            pin2EditTxt.requestFocus();
        }
    }
});

pin2EditTxt.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (pin1EditTxt.length() == 0) {
            pin1EditTxt.requestFocus();
        }
    }
});

pin2EditTxt.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 (pin2EditTxt.getText().toString().length() == 1) {
            pin3EditTxt.requestFocus();
        }
    }
});

pin3EditTxt.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 (pin3EditTxt.getText().toString().length() == 1) {
            pin4EditTxt.requestFocus();
        }
    }
});

pin4EditTxt.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) {
        //隐藏键盘
        InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
        editTxtWrapper.requestFocus();
    }
});
英文:

I have 4 edittexts that act as a "enter your pin" field with each edittext set to only be able to take a single number before requesting focus to the next edittext. If I enter a number for the 1st pin, it then request focus to the 2nd, and then 3rd, etc.

What I am trying to do now is, if the user clicks on the 2nd edittext or 3rd and the 1st field has not been entered, it should go back and focus on the 1st field.

The edittext fields

I tried to do this using an onTouchListener and what I noticed is that after clicking on the 2nd edittext first instead of clicking on the left/1st edittext, I can see the focus going back to the first edittext but then immediately go back to the 2nd edittext almost a split second and ultimately still remain focus on the 2nd edittext.

I also tested using onClickListener. It does work but it takes two clicks. So I'll click on the 2nd edittext, nothing happens, and then I click it again which then moves the focus back to the 1st edittext but I need it to happen on the first click.

Here is part of the code

pin1EditTxt.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(pin1EditTxt.getText().toString().length()==1)     //size as per your requirement
{
pin2EditTxt.requestFocus();
}
}
});
//        pin2EditTxt.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                if(pin1EditTxt.length() == 0){
//                    pin2EditTxt.clearFocus();
//                    pin1EditTxt.requestFocus();
//                }
//                return false;
//            }
//        });
pin2EditTxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(pin1EditTxt.length() == 0){
pin1EditTxt.requestFocus();
}
}
});
pin2EditTxt.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(pin2EditTxt.getText().toString().length()==1)     //size as per your requirement
{
pin3EditTxt.requestFocus();
}
}
});
pin3EditTxt.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(pin3EditTxt.getText().toString().length()==1)     //size as per your requirement
{
pin4EditTxt.requestFocus();
}
}
});
pin4EditTxt.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) {
//Hide keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
editTxtWrapper.requestFocus();
}
});

答案1

得分: 1

我猜你应该在onclick方法中使用pin1EditTxt.getText().toString().length()==0,而不是pin1EditTxt.length()==0

英文:

I guess you should be using pin1EditTxt.getText().toString().length()==0 instead of pin1EditTxt.length()==0 inside onclick.

huangapple
  • 本文由 发表于 2020年8月12日 10:31:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63368907.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定