Java中的十进制输入edittext [英] Decimal input edittext in java

查看:108
本文介绍了Java中的十进制输入edittext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个带有诸如0.00之类的值的编辑文本,并且光标必须在左侧.当用户按下1时,它应该是1.00;当用户按下2时,它应该是12.00;当用户按下"时.光标应移至小数点的另一侧.当用户按3移到小数点的另一侧后,应为12.30,按4时应为12.34.同样,在反压时,它应该是12.30-> 12.00-> 1.00-> 0.00.样本

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) {

    }
}

推荐答案

我已经将此解决方案保留了两位小数位.请检查伙计们.这适用于小数点后两位.

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

公共类TxLabzTextWatcher实现了TextWatcher {

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;
}

}

这篇关于Java中的十进制输入edittext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆