The User can enter only one digit in the edit text. if he enters the value in
Try TextWatcher instead of onKeyListener
B'coz if want to edit your password, in that case TextWatcher will give you more method to dealt with..
Edited:-
StringBuilder sb=new StringBuilder();
edtPasscode1.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
if(sb.length()==0&edtPasscode1.length()==1)
{
sb.append(s);
edtPasscode1.clearFocus();
edtPasscode2.requestFocus();
edtPasscode2.setCursorVisible(true);
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
if(sb.length()==1)
{
sb.deleteCharAt(0);
}
}
public void afterTextChanged(Editable s) {
if(sb.length()==0)
{
edtPasscode1.requestFocus();
}
}
});
Hope this work.
set the length to editetxt as android:maxLength="1"
and follow the below code
((EditText) findViewById(R.id.edi1)).addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
if(((EditText) findViewById(R.id.edi1)).getText().toString().length()==1)
{
((EditText) findViewById(R.id.edi1)).clearFocus();
((EditText) findViewById(R.id.edi2)).requestFocus();
((EditText) findViewById(R.id.edi2)).setCursorVisible(true);
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
((EditText) findViewById(R.id.edi2)).addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
if(((EditText) findViewById(R.id.edi2)).getText().toString().length()==1)
{
((EditText) findViewById(R.id.edi2)).clearFocus();
((EditText) findViewById(R.id.edi3)).requestFocus();
((EditText) findViewById(R.id.edi3)).setCursorVisible(true);
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
((EditText) findViewById(R.id.edi3)).addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
if(((EditText) findViewById(R.id.edi3)).getText().toString().length()==1)
{
((EditText) findViewById(R.id.edi3)).clearFocus();
((EditText) findViewById(R.id.edi4)).requestFocus();
((EditText) findViewById(R.id.edi4)).setCursorVisible(true);
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
This worked for my case, also max_length of edit text should be 1.
otp_1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
//
if(editable.length()>0){
otp_1.clearFocus();
otp_2.requestFocus();
otp_2.setCursorVisible(true);
}
}
});
otp_2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
//
if(editable.length() > 0) {
otp_2.clearFocus();
otp_3.requestFocus();
otp_3.setCursorVisible(true);
}
}
});
otp_3.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if(editable.length() > 0) {
otp_3.clearFocus();
otp_4.requestFocus();
otp_4.setCursorVisible(true);
}
}
});
The solution coding is OK,
Below codes indicate that auto move to next Edit Text and auto move to previous Edit Text.
It also OK if you are using Rxjava + Data Binding + RxBinding in below :
// Show button Active code when enough fields active code
Observable<Boolean> mObsPhoneVerify1 = RxTextView.textChanges(db.etPhoneVerify1)
.observeOn(AndroidSchedulers.mainThread())
.map(charSequence -> {
if (!charSequence.toString().equals("")) {
db.etPhoneVerify2.requestFocus();
return true;
} else {
db.etPhoneVerify1.requestFocus();
return false;
}
});
Observable<Boolean> mObsPhoneVerify2 = RxTextView.textChanges(db.etPhoneVerify2)
.observeOn(AndroidSchedulers.mainThread())
.map(charSequence -> {
if (!charSequence.toString().equals("")) {
db.etPhoneVerify3.requestFocus();
return true;
} else {
db.etPhoneVerify1.requestFocus();
return false;
}
});
Observable<Boolean> mObsPhoneVerify3 = RxTextView.textChanges(db.etPhoneVerify3)
.observeOn(AndroidSchedulers.mainThread())
.map(charSequence -> {
if (!charSequence.toString().equals("")) {
db.etPhoneVerify4.requestFocus();
return true;
} else {
db.etPhoneVerify2.requestFocus();
return false;
}
});
Observable<Boolean> mObsPhoneVerify4 = RxTextView.textChanges(db.etPhoneVerify4)
.observeOn(AndroidSchedulers.mainThread())
.map(charSequence -> {
if (!charSequence.toString().equals("")) {
hideKeyboard();
return true;
} else {
/*
If enough text for all fields, no need cursor
Otherwise, focus previous edit text
*/
if (Utils.isValidEditText(db.etPhoneVerify1) && Utils.isValidEditText(db.etPhoneVerify2) && Utils.isValidEditText(db.etPhoneVerify3)) {
db.etPhoneVerify3.requestFocus();
} else {
db.etPhoneVerify1.clearFocus();
db.etPhoneVerify2.clearFocus();
db.etPhoneVerify3.clearFocus();
db.etPhoneVerify4.clearFocus();
}
return false;
}
});
disposable = Observable
.combineLatest(mObsPhoneVerify1, mObsPhoneVerify2, mObsPhoneVerify3, mObsPhoneVerify4,
(PhoneVerify1, PhoneVerify2, PhoneVerify3, PhoneVerify4)
-> PhoneVerify1 && PhoneVerify2 && PhoneVerify3 && PhoneVerify4)
.compose(regisObserver(false))
.subscribe(aBoolean -> {
db.btnActiveCode.setEnabled(aBoolean);
});
set android:maxLength="1"
to all your ExitText
in xml
Try the following code
edtxt1 = (EditText) findViewById(R.id.edtxt_phonenumber_one);
edtxt2 = (EditText) findViewById(R.id.edtxt_phonenumber_two);
edtxt3 = (EditText) findViewById(R.id.edtxt_phonenumber_three);
edtxt1.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if (s.length() ==1) {
edtxt2.requestFocus();
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
});
edtxt2.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if (s.length() == 1) {
edtxt3.requestFocus();
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
});
edtxt3.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if (s.length() == 1) {
edtxt1.requestFocus();
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
});
This should work
My kotlin solution for this
private fun addEditTextListener(editText: EditText?, nextEditText: EditText?) {
editText?.setOnFocusChangeListener { view, focused ->
if (focused) {
editText.text = null
}
}
editText?.addTextChangedListener {
if (it.toString().isNotEmpty()) {
if (nextEditText != null) {
nextEditText.requestFocus()
} else {
viewModel.confirmCode(
binding?.digit1?.text?.toString(),
binding?.digit2?.text?.toString(),
binding?.digit3?.text?.toString(),
binding?.digit4?.text?.toString(),
)
}
}
}
}
And setting the listeners
override fun setupViews() {
addEditTextListener(binding?.digit1, binding?.digit2)
addEditTextListener(binding?.digit2, binding?.digit3)
addEditTextListener(binding?.digit3, binding?.digit4)
addEditTextListener(binding?.digit4, null)
}