Format credit card in edit text in android

后端 未结 29 2036
耶瑟儿~
耶瑟儿~ 2020-11-30 19:18

How to make EditText accept input in format:

4digit 4digit 4digit 4digit 

I tried Custom format edit text input android to acc

29条回答
  •  Happy的楠姐
    2020-11-30 19:51

    You may have figured it out already, but here is what I did. The only method I had to override was AfterTextChanged.

    Check if the form of the credit card is already valid, base case to prevent infinite recursion

    If the form is not valid, remove all whitespace, and copy over into another string, inserting white space where appropriate.

    Then simply replace the editable with your new string.

    If you need code for a particular step, feel free to ask.

    And Preethi, the reason you can't delete spaces is because you can't change text in the onTextChanged callback. From the developer site:

    public abstract void onTextChanged (CharSequence s, int start, int before, int count) Added in API level 1

    This method is called to notify you that, within s, the count characters beginning at start have just replaced old text that had length before. It is an error to attempt to make changes to s from this callback.

提交回复
热议问题