Decimal value of EditText

I am implementing EditText , and I am faced with one problem ... I assume that inputType is numberDecimal , but at the time I type on EditText , I would like to put "." or "," automatically ... I can specify, for example, 100.00 or 90.00 , it depends on the user ... Is this possible?

I tried creating a TextWatcher and doing some things on onTextChanged , for example, see if charSequence-lenght()==2 adds. "" . or "," but does not work ...

Can I determine when to put a comma or period, even if I have a decimal format, for example ##.## or ###.## ? As I said, it can be 100.00 or 80.00 .

Edit

  @Override public void afterTextChanged(Editable editable) { //et1 and et2 are not empty if(et1.getText().toString().length() == 5 && et2.getText().toString().length() == 5){ double pe1 , pe2; pe1 = Double.parseDouble(et1.getText().toString()); pe2 = Double.parseDouble(et2.getText().toString()); double res = (pe1+pe2)/2; String formattedString = String.format("%,.2f", res); tvPr.setText(formattedString); } 

The problem in textInput is not on afterTextChanged I think ... because I need to put "." . manually, and I want to avoid this.

EDIT2

My friend also suggested that I could ignore. and, since the labels are always 100, 80, 32, etc. on editText just enter these numbers, but then treat them as double, and I would like to put a, 00, when the user finishes editing editText is this possible?

Is the comparison good if(et1.getText().toString().length() == 5 && et2.getText().toString().length() == 5){ while check wheter is edittext not empty? I have to update the TextView once when the edittext is not empty and do some operations, so I tried this way, is there another one better? (I put lenght () == 5, because in EditText I put maxlenght () = 5;

-one
java android android-edittext
May 23 '16 at 18:36
source share
1 answer

All you have to do is something like this

 final DecialFormat df = new DecimalFormat("#.00"); input.addTextChangedListener(new TextWatcher(){ @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void onTextChanged(CharSequence s, int start, int before, int count) {} @Override public void afterTextChanged(Editable s) { String value = s.toString(); if(TextUtils.isEmpty(value)){ // do what you want if it is empty return; } try { DecimalFormat df = new DecimalFormat("#.00"); String testFormat = df.format(Double.parseDouble(s.toString())); // Manage the cursor position int newPos; try { int pos = et.getSelectionStart(); int sizeDiff = testFormat.length() - s.length(); newPos = pos + sizeDiff; }catch(Exception e1){ e1.printStackTrace(); newPos = testFormat.length(); } // Modify the edit text to hold the double value et.removeTextChangedListener(this); et.setText(testFormat); et.setSelection(newPos); et.addTextChangedListener(this); }catch(Exception e){ e.printStackTrace(); et.removeTextChangedListener(this); et.setText(""); et.addTextChangedListener(this); } } } 

Modify the above code to do what you need, but this is the basic idea that the value is in decimal form. It is worth noting that when you change the text in this way, the cursor position is always “funny” to try and get how you want it to. What happens if you have "100.00" and someone puts the cursor in a place, for example, "100.0 | 0", and presses 5. If the result is "100.0 | 5" or "100.05 |", and if this is the last, how do you deal with them by adding the following value, do you use it to round the previous value? This is an interesting situation, but it will help you get started.

+1
May 24 '16 at 17:25
source share



All Articles