2

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:

  1. 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

  2. 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:

  1. The user pressed Button B, the Edittext format will be like ABCD1234EFGH5678
  2. Then, The user pressed Button A, the Edittext format will be like ABCD 1234 EFGH 5678
  3. When The user pressed Button B again, the Edittext format will be like ABCD 1234 EFGH 5678 instead of ABCD1234EFGH5678

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 );
    }
}
James Z
  • 12,209
  • 10
  • 24
  • 44
Anne
  • 31
  • 1
  • 6

1 Answers1

0

I think you can use just one TextWatcher:

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//here evaluate the value of radio button
 if(mRadioButton1.isChecked()){
      //set pattern
   }else if(mRadioBUtton2.isChecked()){
     //set another pattern
   }
}
Igmer Rodriguez
  • 164
  • 4
  • 18
  • I've done this before, but still, whenever Button A called, the EditText will only follow the Button A format even though I pressed the Button B @Igmer Rodriguez – Anne Jul 31 '21 at 02:34