How to prevent the user to enter a "." point before any number?

Asked

Viewed 57 times

2

I’m using the TextWatcher to make other changes, but I’m stuck on this part, I’m taking a value double and using it for various calculations so I cannot receive the value ". 55" for example.

If I didn’t need to do calculations, Android would put the 0 before the point automatically for me. Does anyone know how to solve this problem?

3 answers

0

If you’re doing calculations with double, then by default, numbers with "." on the front are accepted and do not disturb the calculation. But if you want to prohibit the use of "." before a number you can use regular expressions.

0

If you enter the data type in the xml of editText can solve your problem?

<EditText
                android:id="@+id/editText1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="numberDecimal"/>

0

Try to use, for mathematical calculations, the Bigdecimal. In addition to working accurately, it has good interaction with views.

Try this:

String strValue = myTextView.getText().toString();
BigDecimal decimalValue = new BigDecimal(valueStr.replace('.', ','));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.