How can I avoid typing a character into an EditBox in Android Studio?
I want to prevent the user from entering a character in EditText
the second time, for example if you enter a zero number, this is fine, but if you want to enter a number zero a second time, this action must be canceled.
In other words, I don't want the user to write 00.
Is there a way to do this?
+3
source to share
2 answers
You can do it like this:
EditText editText = new EditText(this);
InputFilter inputFilter = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
//Write your logic to remove the zero
//remove the zero here and return it.
return null;
}};
editText.setFilters(new InputFilter[]{inputFilter});
+2
source to share
Try it. This implementation removes the redundant character, but you get an idea:
mEditText.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) {
int length = s.length();
for(int i=1;i<length;i++){
if (s.charAt(i-1) == s.charAt(i)) {
mEditText.setText(s.subSequence(0, i));
mEditText.setSelection(i);
}
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
0
source to share