如何在使用EditText Watcher时为十进制数字添加逗号

时间:2018-09-03 11:21:57

标签: java android android-edittext decimalformat

”“我在这里添加了我的代码,我通过使用setperfectDecimal方法限制了十进制数,在我使用默认键盘但使用##标题## Swif enter code here tkey时,我的代码工作正常键盘无法正常工作”

          edittext.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                //System.out.println(TAG+" >>> beforeTextChanged "+charSequence.toString()+" : "+i+" : "+i1+" : "+i2);
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                //System.out.println(TAG+" >>> onTextChanged "+charSequence.toString()+" : "+i+" : "+i1+" : "+i2);
            }

            @Override
            public void afterTextChanged(Editable editable) {
                String str = edittext.getText().toString();
                Log.d("PLACE ORDER1 ", "afterTextChanged: "+str +":"+ editable);
                if (str.isEmpty()) return;
                //String str2 = PerfectDecimal(str, 6, 2);

                String str2 = PerfectDecimal(str, 11, 2);
                Log.d("PLACE ORDER3 ", "afterTextChanged: "+str2);
                if (!str2.equals(str)) {
                    Log.d("PLACE ORDER2 ", "afterTextChanged: "+str2);
                    edittext.setText(str2);
                    int pos = str2.length();
                    Log.d(TAG, "afterTextChanged:position "+pos);
                    edittext.setSelection(pos);
                }

                commaInEditText(this, edittext, str2);

            }
        });
 public String PerfectDecimal(String str, int MAX_BEFORE_POINT, int MAX_DECIMAL) {
        if (str.charAt(0) == '.') str = "0" + str;
        int max = str.length();

        String rFinal = "";
        boolean after = false;
        int i = 0, up = 0, decimal = 0;
        char t;
        while (i < max) {
            t = str.charAt(i);
            if (t != '.' && after == false) {
                up++;
                if (up > MAX_BEFORE_POINT) return rFinal;
            } else if (t == '.') {
                after = true;
            } else {
                decimal++;
                if (decimal > MAX_DECIMAL)
                    return rFinal;
            }
            rFinal = rFinal + t;
            i++;
        }

        return rFinal;
    }

    public synchronized void commaInEditText(TextWatcher textWatcher, EditText editText, String value) {
        Log.d("PLACE ORDER3 ", "commaInEditText afterTextChanged: "+value);
        editText.removeTextChangedListener(textWatcher);
//        editText.addTextChangedListener(null);

    enter code here
        try {
            String originalString = value.toString();
            String dotspilt = "";
            int cursorPos = editText.getSelectionStart();
            int textCount = originalString.length();

            if (originalString != null && !originalString.equals("")) {

                if (originalString.startsWith(".")) { //adds "0." when only "." is pressed on begining of writting
                    editText.setText("0.");
                    originalString = editText.getText().toString();
                }
                if (originalString.startsWith("0") && !originalString.startsWith("0.")) {
                    editText.setText(""); //Prevents "0" while starting but not "0."
                    originalString = editText.getText().toString();
                }

//                    Long longval;
                if (originalString.contains(",")) {
                    originalString = originalString.replaceAll(",", "");
                }

                if (originalString.contains(".")) {
                    try {
                        if (originalString.split("\\.").length == 2) {

                            dotspilt = "." + originalString.split("\\.")[1];

                            originalString = originalString.split("\\.")[0];

                        } else if (originalString.split("\\.").length == 1) {

                            originalString = originalString.split("\\.")[0];

                            dotspilt = "." + originalString.split("\\.")[1];

                        }
                    } catch (Exception e) {
                        dotspilt = ".";
                    }
                }

                //setting text after format to EditText
                editText.setText(StringStuff.commaINRDecorator(originalString) + dotspilt);

                try {
                    if (textCount == editText.getText().length()) {
                        Log.d(TAG, "commaInEditText:1 "+textCount+" :"+ editText.getText()+" ;"+cursorPos);
                        editText.setSelection(cursorPos);
                    } else if (textCount < editText.getText().length()) {
                        Log.d(TAG, "commaInEditText:2 "+textCount+" :"+ editText.getText()+" :"+cursorPos+" : "+editText.getText().length());
                        editText.setSelection(editText.getText().length() + 1);

                    } else if (textCount > editText.getText().length()) {`
`
                        editText.setSelection(cursorPos == 0 ? cursorPos : (cursorPos - 1));
                    }
                } catch (Exception e) {
                    editText.setSelection(editText.getText().length());
                    if (true) e.printStackTrace();
                }

            }
        } catch (Exception nfe) {
            nfe.printStackTrace();
        }

        editText.addTextChangedListener(textWatcher);

    }

0 个答案:

没有答案