Decimal input edittext in java

有些话、适合烂在心里 提交于 2020-02-06 08:43:26

问题


I want to have an edit text with some value like 0.00 and cursor must be on the left side. when user press 1 it should be 1.00, when user presses 2 it should be 12.00, when user press "." cursor should move to the other side of decimal. After shifting to the other side of decimal when user press 3 it should be 12.30 and on pressing 4 it should be 12.34. Similarly on backpress it should be 12.30 -> 12.00 -> 1.00 -> 0.00.sample

 import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;

import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AmountTextWatcher implements TextWatcher {

    private EditText editText;
    private int num_npt;
    private String previous;
    private boolean isBackSpacePressed;

    public AmountTextWatcher(EditText editText, int num_npt) {
        this.editText = editText;
        this.num_npt = num_npt;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        /*if (StorageUtils.INTERNAL_TRANSFER_AMOUNT != 0.00)
            previous = String.format(Locale.getDefault(), "%.2f", StorageUtils.INTERNAL_TRANSFER_AMOUNT);
        else*/
        if (after < count) {
            isBackSpacePressed = true;
        } else {
            isBackSpacePressed = false;
        }
        previous = editText.getText().toString();
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        editText.removeTextChangedListener(this);


        if (isBackSpacePressed) {
            String old_val = previous;

            int point = old_val.indexOf(".");
            if (point == -1) point = old_val.length();

            int selectionStart = editText.getSelectionStart() + 1;
            int selectionEnd = editText.getSelectionEnd();

            String new_val = "";
            if (selectionStart < selectionEnd) {
                if (selectionEnd <= point) {
                    new_val = old_val.substring(0, selectionStart) + old_val.substring(selectionEnd);
                } else if (selectionStart > point) {
                    new_val = old_val.substring(0, selectionStart) + "0" + old_val.substring(selectionEnd);
                    if (selectionEnd - selectionStart == num_npt) new_val += "0";
                } else if (selectionStart <= point && selectionEnd > point) {
                    new_val = old_val.substring(0, selectionStart) + ".";
                    for (int z = 0; z < selectionEnd - point - 1; z++) new_val += "0";
                    new_val += old_val.substring(selectionEnd);
                }

                if (new_val.indexOf(".") == 0) new_val = "0" + new_val;

            } else if (selectionStart == 1 && point == 1) {
                new_val = "0" + old_val.substring(1);
                selectionStart = 0;
            } else if (selectionStart > 0 && selectionStart <= point) {
                new_val = old_val.substring(0, selectionStart - 1) + old_val.substring(selectionStart);
                selectionStart--;
            } else if (selectionStart == point + 1) {
                new_val = old_val;
                selectionStart = point;
            } else if (selectionStart > point + 1) {
                new_val = old_val.substring(0, selectionStart - 1) + old_val.substring(selectionStart) + "0";
                selectionStart--;
            } else {
                new_val = old_val;
            }

            if (new_val.equals("")) new_val = "0";
            editText.setText(new_val);
            editText.setSelection(selectionStart);
            editText.addTextChangedListener(this);
        } else {

            int point = previous.indexOf(".");
            if (point == -1) point = previous.length();

            int selectionStart = editText.getSelectionStart() - 1;
            int selectionEnd = editText.getSelectionEnd() - 1;

            String c = s.subSequence(start, start + count).toString();
            if (c.equals("-") || c.equals(" ")) {
                editText.setText(previous);
                editText.setSelection(selectionStart);
                editText.addTextChangedListener(this);
                return;
            }
            String new_val = "";

            if (selectionStart > -1 && selectionStart < editText.getText().length() - 1) {
                if (c != null && c.length() > 0) {
                    if (c.equals(",")) c = ".";
                    if (Character.isDigit(c.charAt(0)) || c.equals(".")) {
                        if (selectionStart < selectionEnd) {
                            if (selectionEnd <= point) {
                                new_val = previous.substring(0, selectionStart) + c + previous.substring(selectionEnd);
                            } else if (selectionStart > point) {
                                new_val = previous.substring(0, selectionStart) + c + previous.substring(selectionEnd);
                                for (int x = 0; x < selectionEnd - selectionStart - 1; x++)
                                    new_val += "0";
                            } else if (selectionStart <= point && selectionEnd > point && !c.equals(".")) {
                                new_val = previous.substring(0, selectionStart) + c + ".";
                                for (int y = 0; y < selectionEnd - point - 1; y++)
                                    new_val += "0";
                                new_val += previous.substring(selectionEnd);
                            }
                        } else if ((num_npt == 0 && previous.equals("0")) || (previous.substring(0, 2).equals("0.") && selectionStart == 0)) {
                            if (c.equals(".")) {
                                new_val = previous;
                                editText.setSelection(0);
                            } else
                                new_val = c + previous.substring(1);
                        } else if (selectionStart > point && selectionStart <= point + num_npt) {
                            if (c.equals(".")) {
                                if (editText.getSelectionStart() > previous.indexOf("."))
                                    new_val = previous.substring(0, selectionStart) + previous.substring(selectionStart);
                                else
                                    new_val = previous.substring(0, selectionStart - 1) + c + previous.substring(selectionStart);
                                editText.setSelection(selectionStart - 1);
                            } else
                                new_val = previous.substring(0, selectionStart) + c + previous.substring(selectionStart + 1);
                        }
                        // this is the code where our edittext lags :(
                        else {
                            if (c.equals("."))
                                if (editText.getSelectionStart() - 1 == 0) {
                                    if (c.equals("."))
                                        new_val = previous.substring(0, 1) + previous.substring(selectionEnd + 1);
                                    else
                                        new_val = previous.substring(0, 1) + c + previous.substring(selectionEnd + 2);
                                } else {
                                    if (selectionStart > 0 && selectionStart < previous.indexOf("."))
                                        new_val = previous.substring(0, selectionStart + 1) + previous.substring(selectionEnd + 1);
                                    else
                                        new_val = previous.substring(0, selectionStart) + c + previous.substring(selectionEnd + 1);
                                }
                            else
                                new_val = previous.substring(0, selectionStart) + c + previous.substring(selectionEnd);
                            if (new_val.substring(new_val.length() - 1).equals("."))
                                new_val = new_val.substring(0, new_val.length() - 1);
                        }
                        // *********//

                        if (new_val.substring(0, 1).equals("0") && !new_val.substring(0, 2).equals("0.")) {
                            new_val = new_val.substring(1);
                            selectionStart--;
                        }

                        Pattern pattern = Pattern.compile("^[0-9]+(\\.[0-9]{0," + num_npt + "})?$");
                        Matcher matcher = pattern.matcher(new_val);
                        boolean doNothing = false;
                        if (matcher.matches()) {
                            editText.setText(new_val);
                            if (!c.equals("."))
                                selectionStart++;
                            else if (c.equals(".") && selectionStart == previous.indexOf("."))
                                selectionStart++;
                            editText.setSelection(selectionStart);
                        } else if (c.equals(".")) {
                            selectionStart++;
                            editText.setSelection(selectionStart);
                        }
                        editText.addTextChangedListener(this);
                    }
                } else {
                    editText.setText(previous);
                    editText.setSelection(selectionStart);
                    editText.addTextChangedListener(this);
                }
            } else {
                int length = editText.getText().length();
                if (selectionStart < 0)
                    selectionStart = 0;
                else if (selectionStart > length || selectionEnd < length)
                    selectionStart = length - 1;
                editText.setText(previous);
                editText.setSelection(selectionStart);
                editText.addTextChangedListener(this);
            }
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}

回答1:


Try to use DecimalFormat Class in Java. It may help you in a better way.

String pattern = "##.##";
DecimalFormat decimalFormat = new DecimalFormat(pattern);
String output = decimalFormat.format( YOUR NUMBER HERE );



回答2:


You can use String.format("%.2f", d) inside your onTextChanged to format

Update 1:

etText.setFilters(new InputFilter[]{new DecimalDigitsInputFilter(5, 2)});
class DecimalDigitsInputFilter implements InputFilter {
   private Pattern mPattern;
   DecimalDigitsInputFilter(int digitsBeforeZero, int digitsAfterZero) {
      mPattern = Pattern.compile("[0-9]{0," + (digitsBeforeZero - 1) + "}+((\\.[0-9]{0," + (digitsAfterZero - 1) + "})?)||(\\.)?");
   }
   @Override
   public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
      Matcher matcher = mPattern.matcher(dest);
      if (!matcher.matches())
      return "";
      return null;
   }
}



回答3:


xml

<EditText
    android:id="@+id/et_des_input"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"
    android:gravity="right"
    android:text="0.00"
    android:digits=".0123456789"/>

Set TextWacher

EditText et_des_input = (EditText) findViewById(R.id.et_des_input);
et_des_input.addTextChangedListener(new AmountTextWatcher(et_des_input, 2));

TextWacher class is same as you post. It is working as you ask for.

See the output




回答4:


I have made this solution for two decimal places. Please check it guys. this works for two decimal places.

public class TxLabzTextWatcher implements TextWatcher {

EditText editText;
String tempText = "";
StringBuilder stringBuilder;
Context ctx;
boolean isBackSpacePressed = false;
int digitsAfteZero;
int count;
boolean isFirstDigitBeforeDecimalZero = true;


public TxLabzTextWatcher(Context context, EditText editText, int digitsAfterZero) {

    this.editText = editText;
    tempText = editText.getText().toString();
    stringBuilder = new StringBuilder();
    this.ctx = context;
    this.digitsAfteZero = digitsAfterZero;
    count = 0;

}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    if (after < count) {
        isBackSpacePressed = true;
    } else {
        isBackSpacePressed = false;
    }

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    editText.removeTextChangedListener(this);

    if (stringBuilder.toString().isEmpty()) {
        stringBuilder.append("0.00");
    }

    if (!isBackSpacePressed) {
        editText.removeTextChangedListener(this);
        stringBuilder.setLength(0);
        stringBuilder.append(s);
        char ch = s.charAt(start + before);

        if (!isClickDot(ch)) {
            if (stringBuilder.length() > 0 && stringBuilder.indexOf(".") - 1 == editText.getSelectionStart() && stringBuilder.charAt(stringBuilder.indexOf(".") - 1) == '0' && ch != '0' && isFirstDigitBeforeDecimalZero) {
                stringBuilder.deleteCharAt(stringBuilder.indexOf(".") - 1);
                editText.setText(stringBuilder.toString());
                editText.setSelection(editText.getText().length() - 3);
                isFirstDigitBeforeDecimalZero = false;
            } else if (ch == '0' && stringBuilder.indexOf(".") - 1 == editText.getSelectionStart()) {
                stringBuilder.deleteCharAt(stringBuilder.indexOf(".") - 1);
                editText.setText(stringBuilder.toString());
                editText.setSelection(editText.getText().length() - 3);
                isFirstDigitBeforeDecimalZero = false;
            }
        }


        // if i press dot then move to the other side of
        if (isClickDot(ch) && StringUtils.countMatches(stringBuilder.toString(), ".") >= 1) {
            stringBuilder.deleteCharAt(s.toString().indexOf('.'));
            editText.setText(stringBuilder.toString());
            editText.setSelection(s.toString().indexOf('.') + 1);
        }

        // replace first digit after decimal
        if (stringBuilder.indexOf(".") + 2 == editText.getSelectionStart() && !isClickDot(ch)) {
            int currIndex = stringBuilder.indexOf(".") + 1;
            stringBuilder.deleteCharAt(currIndex + 1);
            editText.setText(stringBuilder.toString());
            editText.setSelection(currIndex + 1);
            count++;
        }

        // replace last digit after decimal
        if (stringBuilder.indexOf(".") + 3 == editText.getSelectionStart() && !isClickDot(ch)) {
            int currIndex = stringBuilder.indexOf(".") + 2;
            stringBuilder.deleteCharAt(currIndex + 1);
            editText.setText(stringBuilder.toString());
            editText.setSelection(currIndex + 1);
            count++;
        }

        // if string builder after decimal have more than 2 length then delete it
        if (editText.getSelectionEnd() == stringBuilder.length() && stringBuilder.subSequence(stringBuilder.indexOf(".") + 1, stringBuilder.length()).length() > digitsAfteZero) {
            stringBuilder.deleteCharAt(editText.getSelectionEnd() - 1);
            editText.setText(stringBuilder.toString());
            editText.setSelection(stringBuilder.length());
        }


        editText.addTextChangedListener(this);
    } else {
        editText.removeTextChangedListener(this);

        if (s.toString().length() == 3 && s.toString().contains(".") && s.toString().substring(0, s.toString().indexOf('.')).length() == 0) {
            // place zero befor decimal
            String zeroBeforeDecimal = "0" + s.toString();
            stringBuilder.setLength(0);
            stringBuilder.append(zeroBeforeDecimal);
            editText.setText(stringBuilder.toString());
            isFirstDigitBeforeDecimalZero = true;
        }

        // if cursor is moved to other side of decimal now move it to left side of decimal
        if (stringBuilder.toString().contains(".") && editText.getSelectionStart() == stringBuilder.indexOf(".")) {
            stringBuilder.setLength(0);
            stringBuilder.append(s);
            stringBuilder.insert(stringBuilder.length() - 2, ".");
            editText.setText(stringBuilder.toString());
            editText.setSelection(stringBuilder.indexOf("."));
        }

        // if cursor is after one digit after decimal
        if (stringBuilder.indexOf(".") + 1 == editText.getSelectionStart()) {
            int currIndex = stringBuilder.indexOf(".") + 1;
            stringBuilder.deleteCharAt(currIndex);
            stringBuilder.insert(currIndex, "0");
            editText.setText(stringBuilder.toString());
            editText.setSelection(currIndex - 1);
        }

        // if cursor is on last digit then replace it with zero
        if (stringBuilder.indexOf(".") + 2 == editText.getSelectionStart()) {
            int currIndex = stringBuilder.indexOf(".") + 2;
            stringBuilder.deleteCharAt(currIndex);
            stringBuilder.insert(currIndex, "0");
            editText.setText(stringBuilder.toString());
            editText.setSelection(currIndex);
        }

        editText.addTextChangedListener(this);
    }
}

@Override
public void afterTextChanged(Editable arg0) {
}

boolean isClickDot(char dot) {
    if (dot == '.')
        return true;
    return false;
}

}



来源:https://stackoverflow.com/questions/59387099/decimal-input-edittext-in-java

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