The edit text only accepts numbers, not decimal numbers in android

My edit text should only accept a three-digit number, not any other decimal numbers. I used the regex below for this, but it even accepts characters like "." as well as "-". So how to avoid decimal values?

        pattern=Pattern.compile("[0-9]{0,2}");

even i set the edit text

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

This does not work. How to solve?

+4
source share
6 answers

Try the following:

android:inputType="number|none"
android:maxLength="3"
android:digits="0123456789"
+10
source

Define them in xml edittext ...

android:inputType="numberDecimal"
android:maxLength="3"
+1
source

EditText

android:inputType:"number"
android:maxLength:"3"
0

, TextWacher

editText.addTextChangedListener(new TextWatcher()
{
    Handler handler = new Handler();
    Runnable delayedAction = null;

    @Override
    public void onTextChanged( CharSequence s, int start, int before, int count)
    {}

    @Override
    public void beforeTextChanged( CharSequence s, int start, int count, int after)
    {}

    @Override
    public void afterTextChanged( final Editable s)
    {}
});
0

Try putting this in an xmlTextText declaration:

android:inputType="number|none"

Enjoy the coding :)

0
source

You can use this answer to solve your problem. Also, to limit the number of digits you can use:

android:maxLength="3"

as other users will answer you above.

Good luck.

0
source

Source: https://habr.com/ru/post/1526515/


All Articles