简体   繁体   中英

Delete second dot/comma in EditText

I want to add a few numbers together from an EditText. I want it to work both for european and other countries so it has to work with commas and dots as seperators. The problem is the app crashes if the user accidently puts in 2 or more dots or a dot and a comma. Is there a short way to check and delete the unnecessary seperators? Thanks a lot!

This is my code so far:

android:inputType="number|numberDecimal"
android:digits="0123456789.,"

.replace(",", "."));

This should do the trick,

.replaceAll("\\,+", ".").replaceAll("\\.+", ".")

if there are any commas first one will replace all the commas with dots and the second one will replace all the dots with a one dot

This will replace the whole string with 0. It's not pretty but at least it keeps the app from crashing. I guess this will have to do for now ;)

if (string.getText().toString().matches(". \\.. \\..*")) {string.setText("0");}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM