0

I have an EditText which should look like that 2018-11-11-1 So i wanna append the "-" automatically, so the user only has to type in numbers.

But when using input type number (because of the keyboard) it does not append, only working with inputtype text.

            <EditText
            android:id="@+id/idEdit"
            android:layout_width="match_parent"
            android:maxWidth="500dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginHorizontal="20dp"
            android:layout_below="@+id/imageScroller"
            android:hint="Scheinnummer eingeben"
            android:inputType="number"
            android:imeOptions="actionDone"
            android:layout_centerInParent="true"/>

The way I append the hyphen after 4 characters.

        int len=0;
    @Override
    public void afterTextChanged(Editable s) {
        String str = _idEdit.getText().toString();
        if(str.length()==4&& len <str.length()){//len check for backspace
            _idEdit.append("-");
        }
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

        String str = _idEdit.getText().toString();
        len = str.length();
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }
Damaro
  • 55
  • 1
  • 8
  • If you set `inputType` to `number` the EditText will only accept numeric values. You can try this workaround: https://stackoverflow.com/a/8776491/3811895 – vinsce Nov 21 '18 at 13:45

2 Answers2

0

Can you try this? I am not sure,but i use it before for show 12.34.56.78 text

 android:digits="0123456789."
 android:inputType="number"

Your code should be like in xml resource file

 android:digits="0123456789-"
 android:inputType="number"
enesgonez
  • 166
  • 6
0

You should set the input type to number (not mandatory, but it will show the most suitable keyboard for your aim) and also the allowed digits to include the - char.

<!-- this line will show the knumeric keyboard -->
android:inputType="number"
<!-- this line will allow all and only the shown digits to be written -->
android:digits="0123456789-"

Note: parsing the resulting text to a number directly will not succed, you have store it as a String or firstly you have to remove minus characters.

Pier Giorgio Misley
  • 5,305
  • 4
  • 27
  • 66