Entering Decimal values in Edittext?
I have a edit text.开发者_JAVA百科 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?
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);
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.
精彩评论