Entering Decimal values in Edittext?

▼魔方 西西 提交于 2019-12-07 14:35:50

问题


I have a edit text. i want to enter the decimal values in it.

that is when i have enter a first number. it should be like this: .01

then i have enter a second number. it should be like this: .12

then for third one. it should be like this: 1.23

it will go like this.... how to implement this scenario. Any Idea?


回答1:


You should add a TextWatcher to the EditText. This will notify you when the text changes.

When you know the text has changed, you can run it through a DecimalFormat:

DecimalFormat decimalFormat = new DecimalFormat();
decimalFormat.setDecimalSeparatorAlwaysShown(true);
decimalFormat.setMaximumFractionDigits(2);



回答2:


You can use a TextWatcher to look for modifications to the value in the field and insert a decimal point in the appropriate position as they type. See this SO question for an example of using a TextWatcher.



来源:https://stackoverflow.com/questions/4854360/entering-decimal-values-in-edittext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!