删除自动添加的字符到自定义PhoneNumberTextWatcher(XXX)XXX-XXXX时发出问题

时间:2017-02-21 05:19:23

标签: android android-edittext

在格式化(XXX) XXX-XXXX格式的文本时,我的代码工作正常。但是当删除字符时,它会在到达字符-/()时停止。如果我再次将光标移到任何数字字符-/()将自动删除。

这是我用过的代码。

public class PhoneNumberTextWatcher implements TextWatcher {

    private static final String TAG = PhoneNumberTextWatcher.class
            .getSimpleName();
    private EditText edTxt;
    private boolean isDelete;

    public PhoneNumberTextWatcher(EditText edTxtPhone) {
        this.edTxt = edTxtPhone;
        edTxt.setOnKeyListener(new View.OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_DEL) {
                    isDelete = true;
                }
                return false;
            }
        });
    }

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

    }

    public void beforeTextChanged(CharSequence s, int start, int count,
                                  int after) {
    }

    public void afterTextChanged(Editable s) {
        if (isDelete) {
            isDelete = false;
            return;
        }
        String val = s.toString();
        String a = "";
        String b = "";
        String c = "";
        if (val != null && val.length() > 0) {
            val = val.replace("-", "");
            val = val.replace("(", "");
            val = val.replace(" ", "");
            val = val.replace(")", "");
            if (val.length() >= 3) {
                a = val.substring(0, 3);
            } else if (val.length() < 3) {
                a = val.substring(0, val.length());
            }
            if (val.length() >= 6) {
                b = val.substring(3, 6);
                c = val.substring(6, val.length());
            } else if (val.length() > 3 && val.length() < 6) {
                b = val.substring(3, val.length());
            }
            StringBuffer stringBuffer = new StringBuffer();
            if (a != null && a.length() > 0) {
                if (a.length() == 3) {
                    stringBuffer.append("("+a+")"+" ");
                }
                else{
                    stringBuffer.append(a);
                }
            }
            if (b != null && b.length() > 0) {
                stringBuffer.append(b);
                if (b.length() == 3) {
                    stringBuffer.append("-");
                }
            }
            if (c != null && c.length() > 0) {
                stringBuffer.append(c);
            }
            edTxt.removeTextChangedListener(this);
            edTxt.setText(stringBuffer.toString());
            edTxt.setSelection(edTxt.getText().toString().length());
            edTxt.addTextChangedListener(this);
        } else {
            edTxt.removeTextChangedListener(this);
            edTxt.setText("");
            edTxt.addTextChangedListener(this);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

无需聆听Delete密钥。只需存储edittext的先前值,并在调用afterTextChanged时将其与new进行比较。以下代码在我的项目中运行良好。我已将其修改为(XXX) XXX-XXXX格式。您可以修改逻辑以指定何时删除特殊字符()-

<强> PhoneNumberTextWatcher:

public class PhoneNumberTextWatcher implements TextWatcher {

    private EditText phoneNumberEditText;
    private String phoneNumber = "";

    public PhoneNumberTextWatcher(EditText editText) {
        phoneNumberEditText = editText;
    }

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

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

    @Override
    public void afterTextChanged(Editable s) {
        String newValue = s.toString();

        if (newValue.length() > phoneNumber.length()) {
            phoneNumber = s.toString();

            if (phoneNumber.length() == 4) {
                if ('(' != phoneNumber.charAt(0)) {
                    phoneNumberEditText.setText("(" + phoneNumber.substring(0, phoneNumber.length() - 1) + ") " + phoneNumber.substring(phoneNumber.length() - 1));
                    phoneNumberEditText.setSelection(phoneNumber.length());
                }
            } else if (phoneNumber.length() == 10) {
                phoneNumberEditText.setText(phoneNumber.substring(0, phoneNumber.length() - 1) + "-" + phoneNumber.substring(phoneNumber.length() - 1));
                phoneNumberEditText.setSelection(phoneNumber.length());
            }
        }
        else if (newValue.length() < phoneNumber.length()) {
            phoneNumber = s.toString();

            if (phoneNumber.length() == 10) {
                phoneNumberEditText.setText(phoneNumber.substring(0, phoneNumber.length() - 1));
                phoneNumberEditText.setSelection(phoneNumber.length());
            }
            else if (phoneNumber.length() == 6) {
                phoneNumberEditText.setText(phoneNumber.substring(1, phoneNumber.length() - 2));
                phoneNumberEditText.setSelection(phoneNumber.length());
            }
        }
    }
}