EditText on Android?

Is there a way to limit that EditTextonly 2 digits can be entered in Runtime.

Example:

If I set mine android:inputType="numberDecimal", and I enter value into it. It takes the value 123.000000000000. But I want to limit it to 123.00

Is there any way to do this?

+3
source share
2 answers

Take a look at the following post, maybe it will help you:

Input prevention

EditText txtInput = (EditText) findViewById(R.id.txtInput);
txtInput.addTextChangedListener(new TextWatcher() 
{
    public void afterTextChanged(Editable edt) 
    {
        String temp = edt.toString();
        int posDot = temp.indexOf(".");
        if (posDot <= 0) return;
        if (temp.length() - posDot - 1 > 2)
        {
            edt.delete(posDot + 3, posDot + 4);
        }
    }

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

    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
});
+6
source

You can add validation on EditTextusing TextWatcher .

+2
source

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


All Articles