I am trying to make Pin entering page and I created 4 EditTexts and I created a function which is below in my activity. My problem is when i click backspace button I want to focus Current EditText to Before(Back) EditText but Key listener not works.
the logic is simple, when user enters a number to edittext, it losts focus and next edittext is getting focus then it goes like that. but the problem is when I click back space I want to go back before edittext to enter number again. I tried to insert keyListener inside of beforeTextChange but it is not worked.
private void SetTextChange(final EditText etCurrent, final EditText etForward,final EditText etBack, final boolean isLast, final int currentPosition)
{
etCurrent.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) {
if (etCurrent.getText().length() == 1 && !isLast ) {
etCurrent.setTypeface(null, Typeface.NORMAL);
etCurrent.clearFocus();
etForward.requestFocus();
etForward.setCursorVisible(true);
etCurrent.getBackground().setColorFilter(getResources().getColor(R.color.lavender_indigo), PorterDuff.Mode.SRC_ATOP);
} else if (etCurrent.length() == 0) {
etCurrent.getBackground().setColorFilter(getResources().getColor(R.color.french_gray), PorterDuff.Mode.SRC_ATOP);
etCurrent.setTypeface(null, Typeface.SANS_SERIF.getStyle());
etCurrent.setTypeface(null, Typeface.NORMAL);
etCurrent.clearFocus();
etBack.requestFocus();
etBack.setCursorVisible(true);
}
if (etCurrent.length() != 0) {
Integer currentKey = Integer.parseInt(etCurrent.getText().toString());
keyList.set(currentPosition, currentKey);
} else
keyList.set(currentPosition, -1);
if (keyList.size() > 3)
showToast(keyList.get(0) + " " + keyList.get(1) + " " + keyList.get(2) + " " + keyList.get(3));
}
@Override
public void afterTextChanged(Editable s) {
}
});
/*curText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View arg0, boolean arg1) {
curText = etCurrent;
backText = etBack;
curText.setText("");
}
});*/
}
This is also one of my EditText sample xml.(Others are the same)
<EditText
android:layout_column="1"
android:layout_columnWeight="1"
android:id="@+id/etActivationDigit1"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:textSize="80dp"
android:textAlignment="center"
android:gravity="center_horizontal"
android:hint="*"
android:textColorHint="@color/french_gray"
android:backgroundTint="@color/french_gray"
android:fontFamily="sans-serif"
android:textColor="@color/perfume"
android:maxLength="1"
android:inputType="number"
/>
I just wanted to erase and focus back when user enters wrong number. Attention : when you fill all fields you will see you can delete and go back but I want to go back middle of pin code. Thank you.
etCurrent.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(etCurrent.getText().toString() == "" && keyCode == event.KEYCODE_DEL)
{
etCurrent.setText("");
etCurrent.clearFocus();
etBack.requestFocus();
etBack.setText("");
}
return false;
}
});
My Backspace listener not triggers when I touch backspace but it triggers all other characters.