I'm working on a simple android app project for myself. I found an article that made me decided to use TextWatcher. So simply, I add TextWatcher into my code. But here's the thing. I have designed 2 RadioButtons and 1 EditText. Both RadioButton will determine the format of my one and only EditText.
Here's how I want the apps to be reproduced:
If the user pressed Button A, then the input format for the Edittext will be like this:
ABCD 1234 EFGH 5678
So, it has space in every 4 characters
If the user pressed Button B, then the input format for the EditText will be like this:
ABCD1234EFGH5678
So, it has no space in every 4 characters
Even the user pressed the opposites again, the format of the EditText will follow the condition
This is What I've tried:
Use the if-else condition, and even create 2 TextWatchers, but still, the flow will be like this:
Make 2 TextWatchers to determine the case, but still here's what I've got:
- The user pressed Button B, the Edittext format will be like
ABCD1234EFGH5678
- Then, The user pressed Button A, the Edittext format will be like
ABCD 1234 EFGH 5678
- When The user pressed Button B again, the Edittext format will be like
ABCD 1234 EFGH 5678
instead ofABCD1234EFGH5678
How am I supposed to do to reach this approach?
Here's my MainViewGenerator.java:
this.editTextNumberSpaceTextWatcher = new EditTextNumberSpaceTextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
super.onTextChanged(s, start, before, count);
editTextString = s.toString();
}
};
this.editTextNumberNormalTextWatcher = new EditTextNumberNormalTextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
super.onTextChanged(s, start, before, count);
editTextString = s.toString().replaceAll(" ", "");
}
};
withId(R.id.my_main_editText, new Anvil.Renderable() {
@Override
public void view() {
if (!btnRadioGroupValue.isEmpty()){
if (btnRadioGroupValue.equals(valueButtonA)){
onTextChanged(EditTextNumberSpaceTextWatcher);
text(editTextString);
} else if (btnRadioGroupValue.equals(valueButtonB)){
onTextChanged(EditTextNumberNormalTextWatcher);
text(editTextString);
}
}
}
});
Here's my EditTextNumberSpaceTextWatcher.java:
public abstract class EditTextNumberSpaceTextWatcher extends PhoneTextWatcher {
private static final String MY_NUMBER_FORMAT = "**** **** **** **** **** **** **** ****";
public EditTextNumberSpaceTextWatcher() {
super(MY_NUMBER_FORMAT );
}
@Override
public char[] getSeparatorCharacters() {
return new char[]{' '};
}
}
Here's my EditTextNumberNormalTextWatcher.java:
public abstract class EditTextNumberNormalTextWatcherextends PhoneticTextWatcher {
private static final String MY_NUMBER_FORMAT = "********************************";
public EditTextNumberNormalTextWatcher() {
super(MY_NUMBER_FORMAT );
}
}