开发者

Android numberDecimal, considering locale in xml layout

开发者 https://www.devze.com 2023-02-13 11:20 出处:网络
For e.g. an EditText it is normal to specify android:inputType=\"numberDecimal\" for a text field supposed to contain a decimal number. But this assumes that \'.\' is used as the decimal separator and

For e.g. an EditText it is normal to specify android:inputType="numberDecimal" for a text field supposed to contain a decimal number. But this assumes that '.' is used as the decimal separator and in some countries ',' is use开发者_开发知识库d instead. Is it possible to specify in xml that the users locale needs to be considered or do I have to do it manually in my code?


This is known bug, see here for more information.

A possible workaround is to set android:inputType="numberDecimal" (so the digit keyboard will be opend) and android:digits="0123456789.," (so the comma can be entered into the EditText). Then add a TextChangedListener and do something like Double.parseDouble(editable.toString().replace(',', '.')); in afterTextChanged.

0

精彩评论

暂无评论...
验证码 换一张
取 消