I am new to Android. when I'm stuck in coding, I always use stackoverflow for reference. I learned a lot from stackoverflow. This is the first time I dared to answer this question. Forgive me if I am mistaken, and any suggestion regarding coding or a way to write code in stackoverflow is much appreciated. Thanks..
I did something similar in Fragments. Take 4 EditText and set the maxLength attribute to 1 in xml for all 4 EditTexts. You can change the EditText to suit your requirement.
Note. The OnKey method may or may not be called for DEL (BackSpace) on the Android Android keyboard.
public class VerifyCodeFrag extends Fragment implements TextWatcher,View.OnKeyListener,View.OnFocusChangeListener { private EditText et_digit1, et_digit2, et_digit3, et_digit4;
This method is used for intuition of representations.
private void initializeView(View view) { et_digit1 = (EditText) view.findViewById(R.id.et_vfcode_digit1); et_digit2 = (EditText) view.findViewById(R.id.et_vfcode_digit2); et_digit3 = (EditText) view.findViewById(R.id.et_vfcode_digit3); et_digit4 = (EditText) view.findViewById(R.id.et_vfcode_digit4); setListners(); }
This method is designed to set listeners for each EditTexts.
private void setListners() { et_digit1.addTextChangedListener(this); et_digit2.addTextChangedListener(this); et_digit3.addTextChangedListener(this); et_digit4.addTextChangedListener(this); et_digit1.setOnKeyListener(this); et_digit2.setOnKeyListener(this); et_digit3.setOnKeyListener(this); et_digit4.setOnKeyListener(this); et_digit1.setOnFocusChangeListener(this); et_digit2.setOnFocusChangeListener(this); et_digit3.setOnFocusChangeListener(this); et_digit4.setOnFocusChangeListener(this); }
This is the OnFocusChangeListner interface override method, with which I check which EditText is currently focused on, where it is useful to extract the number from the corresponding EditText blocks in the afterTextChnged method (TextWatcher override method).
@Override public void onFocusChange(View v, boolean hasFocus) { switch(v.getId()) { case R.id.et_vfcode_digit1: whoHasFocus=1; break; case R.id.et_vfcode_digit2: whoHasFocus=2; break; case R.id.et_vfcode_digit3: whoHasFocus=3; break; case R.id.et_vfcode_digit4: whoHasFocus=4; break; default: break; } }
This is a method to override the TextWatcher interface. Here in this afterTextChanged (override) method I get a number from EdiTexts by storing them in the corresponding charArray index. And as soon as the user enters a number in EditText, the next EditText will receive focus using the requestfocus method (Example: et_digit2.requestFocus ()).
@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) { switch (whoHasFocus) { case 1: if(!et_digit1.getText().toString().isEmpty()) { code[0]= et_digit1.getText().toString().charAt(0); et_digit2.requestFocus(); } break; case 2: if(!et_digit2.getText().toString().isEmpty()) { code[1]= et_digit2.getText().toString().charAt(0); et_digit3.requestFocus(); } break; case 3: if(!et_digit3.getText().toString().isEmpty()) { code[2]= et_digit3.getText().toString().charAt(0); et_digit4.requestFocus(); } break; case 4: if(!et_digit4.getText().toString().isEmpty()) { code[3]= et_digit4.getText().toString().charAt(0); } break; default: break; } }
This method will function as a delete key (BackSpace).
In this override method, I check if the EditText is empty and DEL (pressing the backspace key on the keyboard). if true, the previous EditText will get focus.
@Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN) { if (keyCode == KeyEvent.KEYCODE_DEL) { switch(v.getId()) { case R.id.et_vfcode_digit2: if (et_digit2.getText().toString().isEmpty()) et_digit1.requestFocus(); break; case R.id.et_vfcode_digit3: if (et_digit3.getText().toString().isEmpty()) et_digit2.requestFocus(); break; case R.id.et_vfcode_digit4: if (et_digit4.getText().toString().isEmpty()) et_digit3.requestFocus(); break; default: break; } } } return false; } }
Sample image.
[1]: https://i.stack.imgur.com/DAc9y.jpg