5

看看我的代码:

txt_HomeNo.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            boolean flag = true;
            String eachBlock[] = txt_HomeNo.getText().toString().split("-");
            for (int i = 0; i < eachBlock.length; i++) {
                if (eachBlock[i].length() > 3) {
                    flag = false;
                }
            }

            if (flag) {

                txt_HomeNo.setOnKeyListener(new OnKeyListener() {

                    @Override
                    public boolean onKey(View v, int keyCode, KeyEvent event) {

                        if (keyCode == KeyEvent.KEYCODE_DEL)
                            keyDel = 1;
                        return false;
                    }
                });

                if (keyDel == 0) {

                    if (((txt_HomeNo.getText().length() + 1) % 4) == 0) {

                        if (txt_HomeNo.getText().toString().split("-").length <= 3) {
                            txt_HomeNo.setText(txt_HomeNo.getText() + "-");
                            txt_HomeNo.setSelection(txt_HomeNo.getText().length());
                        }
                    }
                    a = txt_HomeNo.getText().toString();
                } else {
                    a = txt_HomeNo.getText().toString();
                    keyDel = 0;
                }

            } else {
                txt_HomeNo.setText(a);
            }

        }

电话号码的最大长度只有 7。当我已经输入了 3 位数字时,它会附加破折号(这就是我想要发生的)但我的问题是接下来的 3 位数字也附加破折号(像这样511-871-:) ...我的问题是如何使用下一个 4 位数字进行编码,并在其上加上破折号。请帮我解决一下这个。谢谢!

4

7 回答 7

19

我想,我有简单的解决方案,请看随附的屏幕截图

在 2 位和 5 位之后附加 /以获得 DOB。

追加和删除都没有循环问题。 在此处输入图像描述

editeTextDob.addTextChangedListener(new TextWatcher() {
            int prevL = 0;

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                prevL = dob.getText().toString().length();
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                int length = editable.length();
                 if ((prevL < length) && (length == 2 || length == 5)) {
                    editable.append("/");
                }
            }
        });
于 2015-10-16T11:31:25.850 回答
13

尝试这个

@Override
public void afterTextChanged(Editable text) {     


    if (text.length() == 3 || text.length() == 7) {
        text.append('-');
    }


}

或所有这些

private boolean isFormatting;
private boolean deletingHyphen;
private int hyphenStart;
private boolean deletingBackward;

@Override
public void afterTextChanged(Editable text) {
    if (isFormatting)
        return;

    isFormatting = true;

    // If deleting hyphen, also delete character before or after it
    if (deletingHyphen && hyphenStart > 0) {
        if (deletingBackward) {
            if (hyphenStart - 1 < text.length()) {
                text.delete(hyphenStart - 1, hyphenStart);
            }
        } else if (hyphenStart < text.length()) {
            text.delete(hyphenStart, hyphenStart + 1);
        }
    }
    if (text.length() == 3 || text.length() == 7) {
        text.append('-');
    }

    isFormatting = false;
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    if (isFormatting)
        return;

    // Make sure user is deleting one char, without a selection
    final int selStart = Selection.getSelectionStart(s);
    final int selEnd = Selection.getSelectionEnd(s);
    if (s.length() > 1 // Can delete another character
            && count == 1 // Deleting only one character
            && after == 0 // Deleting
            && s.charAt(start) == '-' // a hyphen
            && selStart == selEnd) { // no selection
        deletingHyphen = true;
        hyphenStart = start;
        // Check if the user is deleting forward or backward
        if (selStart == start + 1) {
            deletingBackward = true;
        } else {
            deletingBackward = false;
        }
    } else {
        deletingHyphen = false;
    }
}
于 2013-06-07T05:41:37.043 回答
3

使用此代码此代码为我工作它会帮助你

        editText.addTextChangedListener(new TextWatcher() {
        int prevL = 0;
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            prevL = editText.getText().toString().length();
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            int length = s.length();
            if ((prevL < length) && (length == 2 || length == 5)) {
                String data = editText.getText().toString();
                editText.setText(data + "/");
                editText.setSelection(length + 1);


            }

        }
    });
于 2016-12-01T12:25:41.247 回答
1

改进@techroid 的答案:(kotlin 版本)

start如果字符是字母,则始终为 0 但count并且正确,这将解决问题after,而如果字符是数字,start则正确,而count始终为 1,并且after始终为 0。

要测试错误:

  • setDashMasking(yourInput, intArrayOf(3))
  • 类型:(asd这将在 d 之后附加破折号,因此它将变为asd-

然后尝试删除它,它无法删除,除非您输入123,否则@techroid 的答案将不起作用。

下面的代码将解决这个问题。

    private fun setDashMasking(input: AppCompatEditText, dashIndexes: IntArray) {
        input.addTextChangedListener(object : TextWatcher {

            private var isFormatting = false
            private var deletingHyphen = false
            private var hyphenStart = 0
            private var deletingBackward = false

            override fun afterTextChanged(editable: Editable) {
                if (isFormatting) return

                isFormatting = true

                // If deleting hyphen, also delete character before or after it
                if (deletingHyphen && hyphenStart > 0) {
                    if (deletingBackward) {
                        if (hyphenStart - 1 < editable.length) {
                            editable.delete(hyphenStart - 1, hyphenStart)
                        }
                    } else if (hyphenStart < editable.length) {
                        editable.delete(hyphenStart, hyphenStart + 1)
                    }
                }

                if (!deletingHyphen &&
                    dashIndexes.contains(editable.toString().length)
                ) {
                    editable.append('-')
                }

                isFormatting = false
            }

            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
                if (isFormatting) return

                // Make sure user is deleting one char, without a selection
                val selStart = Selection.getSelectionStart(s)
                val selEnd = Selection.getSelectionEnd(s)

                if (s.length > 1 // Can delete another character
                    && count - after == 1 // deleting only 1 character
                    && s[selStart - 1] == '-' // a hyphen
                    && selStart == selEnd
                ) { // no selection
                    deletingHyphen = true
                    hyphenStart = selStart - 1
                    // Check if the user is deleting forward or backward
                    deletingBackward = selStart == (selStart - 1) + 1
                } else {
                    deletingHyphen = false
                }
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                listener?.onChanged(s.toString())
            }
        })
    }
于 2020-06-03T04:06:37.657 回答
0

为时已晚,但可以帮助其他人

  edtMoneyIntMin.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) {
            try {
                int edtInteger = Integer.valueOf(edtMoneyIntMin.getText().toString());
                new DecimalFormat("1,000,000,000");
                String myString = NumberFormat.getInstance().format(edtInteger).replace(",", "/");
                HOUSE_COST_MIN = myString;
                txtMoneyIntMin.setText(myString);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            } catch (NullPointerException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
于 2017-07-13T09:06:14.630 回答
0

对于这种请求,我通常依赖于 editText Masking 技术。

这是一个运行良好的库:

https://github.com/egslava/edittext-mask

但我最喜欢处理删除字符的方法是:

compile 'com.redmadrobot:inputmask:2.3.0'

它在自述文件中有一个电话号码示例。

于 2017-11-06T05:04:01.577 回答
0

根据从 beforeTextChange 方法获取的先前文本的子字符串检查 onTextChanged 方法中的连字符。根据 afterTextChanged 方法中的要求更改条件。

于 2022-02-10T18:18:39.323 回答