【问题标题】:Custom phone number format (XXX-XXX-XXXX) TextWatcher getting stuck自定义电话号码格式 (XXX-XXX-XXXX) TextWatcher 卡住
【发布时间】:2016-06-04 22:14:47
【问题描述】:

一切正常: 我想要的电话号码EditText中的格式(XXX-XXX-XXXX)有效。

问题是: 删除字符 "-" 时,不能删除。我卡住了。

PhoneNumberTextWatcher.java

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

public class PhoneNumberTextWatcher implements TextWatcher {

    private static final String TAG = PhoneNumberTextWatcher.class
            .getSimpleName();
    private EditText edTxt;
    public 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 (event.getAction() == KeyEvent.KEYCODE_DEL) {
                    isDelete = true;
                    LogUtils.Log_e("ISDELETE", String.valueOf(isDelete));
                }
                return false;
            }
        });
    }
l.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) {
                stringBuffer.append(a);
                if (a.length() == 3) {
                    stringBuffer.append("-");
                }
            }
            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);
        }

    }
}
    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) {
        LogUtils.Log_e("ISDELETE", String.valueOf(isDelete));
        if (isDelete) {
            isDelete = false;
            return;
        }
        LogUtils.Log_e("ISDELETE", String.valueOf(isDelete));
        String val = s.toString();
        String a = "";
        String b = "";
        String c = "";
        if (val != null && val.length() > 0) {
            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) {
                stringBuffer.append(a);
                if (a.length() == 3) {
                    stringBuffer.append("-");
                }
            }
            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);
        }

    }
}

我正在尝试TextWatcher 使用以下代码:

edtOrderCardPhone.addTextChangedListener(new PhoneNumberTextWatcher(edtOrderCardPhone));

查看相关问题here。 谁能帮忙解决问题!

【问题讨论】:

    标签: android android-textwatcher onkeylistener addtextchangedlistener


    【解决方案1】:

    我找到了使用以下代码更改我的PhoneNumberTextWatcher 的解决方案。

    import android.text.Editable;
    import android.text.TextWatcher;
    import android.widget.EditText;
    
    public class PhoneNumberTextWatcher implements TextWatcher {
    
        private static final String TAG = "PhoneNumberTextWatcher";
        private EditText editText;
    
        public PhoneNumberTextWatcher(EditText edTxtPhone) {
            this.editText = edTxtPhone;
        }
    
        public void onTextChanged(CharSequence s, int cursorPosition, int before,
                                  int count) {
    
            if (before == 0 && count == 1) {  //Entering values
    
                String val = s.toString();
                String a = "";
                String b = "";
                String c = "";
                if (val != null && val.length() > 0) {
                    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) {
                        stringBuffer.append(a);
    
                    }
                    if (b != null && b.length() > 0) {
                        stringBuffer.append("-");
                        stringBuffer.append(b);
    
                    }
                    if (c != null && c.length() > 0) {
                        stringBuffer.append("-");
                        stringBuffer.append(c);
                    }
                    editText.removeTextChangedListener(this);
                    editText.setText(stringBuffer.toString());
                    if (cursorPosition == 3 || cursorPosition == 7) {
                        cursorPosition = cursorPosition + 2;
                    } else {
                        cursorPosition = cursorPosition + 1;
                    }
                    if (cursorPosition <= editText.getText().toString().length()) {
                        editText.setSelection(cursorPosition);
                    } else {
                        editText.setSelection(editText.getText().toString().length());
                    }
                    editText.addTextChangedListener(this);
                } else {
                    editText.removeTextChangedListener(this);
                    editText.setText("");
                    editText.addTextChangedListener(this);
                }
    
            }
    
            if (before == 1 && count == 0) {  //Deleting values
    
                String val = s.toString();
                String a = "";
                String b = "";
                String c = "";
    
                if (val != null && val.length() > 0) {
                    val = val.replace("-", "");
                    if (cursorPosition == 3) {
                        val = removeCharAt(val, cursorPosition - 1, s.toString().length() - 1);
                    } else if (cursorPosition == 7) {
                        val = removeCharAt(val, cursorPosition - 2, s.toString().length() - 2);
                    }
                    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) {
                        stringBuffer.append(a);
    
                    }
                    if (b != null && b.length() > 0) {
                        stringBuffer.append("-");
                        stringBuffer.append(b);
    
                    }
                    if (c != null && c.length() > 0) {
                        stringBuffer.append("-");
                        stringBuffer.append(c);
                    }
                    editText.removeTextChangedListener(this);
                    editText.setText(stringBuffer.toString());
                    if (cursorPosition == 3 || cursorPosition == 7) {
                        cursorPosition = cursorPosition - 1;
                    }
                    if (cursorPosition <= editText.getText().toString().length()) {
                        editText.setSelection(cursorPosition);
                    } else {
                        editText.setSelection(editText.getText().toString().length());
                    }
                    editText.addTextChangedListener(this);
                } else {
                    editText.removeTextChangedListener(this);
                    editText.setText("");
                    editText.addTextChangedListener(this);
                }
    
            }
    
    
        }
    
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
        }
    
        public void afterTextChanged(Editable s) {
    
    
        }
    
        public static String removeCharAt(String s, int pos, int length) {
    
            String value = "";
            if (length > pos) {
                value = s.substring(pos + 1);
            }
            return s.substring(0, pos) + value;
        }
    }
    

    感谢B Bhanu Chander solution

    【讨论】:

    • 可以转换成xxx-xx-xx-xx-xx吗?
    【解决方案2】:

    你可以这样做:

    private EditText editText;
    

    onCreate()

    editText = (EditText) findViewById(R.id.editText);
    editText.addTextChangedListener(new PhoneNumberFormattingTextWatcher());
    

    屏幕截图:

    希望这会对你有所帮助。

    【讨论】:

    • 感谢重播。通过使用 PhoneNumberFormattingTextWatcher() ,我得到了 XXXX XXX XXX 这种格式。但我要求的格式是 XXX-XXX-XXXX 。我已经得到了我作为答案发布的解决方案。
    • @pRaNaY,不,你可以找到相同的屏幕截图。
    • 我得到了你的答案。但我的要求是自定义电话号码在电话号码的 3、6 位后有连字符 (-)。这是我通过我的代码实现的。
    猜你喜欢
    • 2013-03-19
    • 2017-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-01
    • 2011-09-02
    • 2016-12-24
    • 1970-01-01
    相关资源
    最近更新 更多