2

I need the user to enter text in an EditText according to this specfic pattern:

123.456-7890-123.456

The user can input any number of integers, so they could as well enter 123.456-7

I do not want the user to enter . or - just the numbers, like an input mask.

Also the numeric keyboard should only show.

I've searched StackOverflow extensively and have seen examples that use InputFilter, ChangedListener, TextWatcher but have not found anything simlar to what I'm trying to do. I've tried in various implementations of what I've found, but I'm inexperienced in using these so I may have overlooked something.

Any suggestions would be welcome.

user2254532
  • 1,851
  • 2
  • 14
  • 14

4 Answers4

0

You're going to have to use a TextWatcher and a regular expression pattern matcher to accomplish what you're trying to do.

This answer should be helpful: Android AutoCompleteTextView with Regular Expression?

Community
  • 1
  • 1
Bill Mote
  • 12,644
  • 7
  • 58
  • 82
0

You can create your own class that implements InputFilter. Then you would apply it as follows:

MyInputFilter filter = new MyInputFilter(...);
editText.setFilters(new InputFilter[]{filter});

Refer to the docs for how InputFilter is intended to work, then refer to the source code for some of the InputFilters used in Android for some ideas how to implement them.

Karakuri
  • 38,365
  • 12
  • 84
  • 104
0

After many failed attempts to implement InputFilter or Regular Expressions I opted for something a little more straight forward:

public void onTextChanged(CharSequence s, int start, int before, int count) { 
    String a = "";
    String str = id.getText().toString(); 
    String replaced = str.replaceAll(Pattern.quote("."),""); 
    replaced = replaced.replaceAll(Pattern.quote("-"),"");
                char[] id_char = replaced.toCharArray();
                int id_len = replaced.length();
    for(int i = 0; i < id_len; i++) {
                    if(i == 2 || i == 12) {
                        a += id_char[i] + ".";                      
                    }
                    else if (i == 5 || i == 9) {
                        a += id_char[i] + "-";
                    }
                    else a += id_char[i];
                }
    id.removeTextChangedListener(this);
    id.setText(a);
    if(before > 0) id.setSelection(start);
    else id.setSelection(a.length());
    id.addTextChangedListener(this);                

}

I don't know if this is the best approach but it does work. One problem I still haven't solved is how to handle cursor placement after the user deletes or inserts a number. If the user inserts the cursor somewhere in the EditText and enters a new number the cursor jumps to the end of the EditText. I would like the cursor to stay where it is at. Another problem if the user inserts the cursor within the EditText number and backspaces to delete a number, then the first key entry doesn't work and on the second key the number is entered. I can only guess this has to do with focus?

user2254532
  • 1,851
  • 2
  • 14
  • 14
-3

Use this: https://github.com/alobov/SimpleMaskWatcher.

Just set your mask for this watcher (###.###-####-###.###). It will add special symbols automatically and wont check your input string for being complete. But showing the numeric keyboard you must handle by your own using android:inputType="number" tag for your EditText.

チーズパン
  • 2,752
  • 8
  • 42
  • 63