Automatically add dash in phone number in Android

前端 未结 7 2322
-上瘾入骨i
-上瘾入骨i 2020-12-17 21:18

Instead of 5118710, it should be 511-8710. I\'d like to add a dash after the user the user inputted 3 digits already in the EditText. The maximum length of

相关标签:
7条回答
  • 2020-12-17 21:31

    I implemented a custom TextWatcher; this handles 10 and 11 digit phone numbers (i.e. 1-555-867-5309 and 555-867-5309). Allows adds, deletions, inserts, mass removal while maintaining proper cursor position.

        public class CustomPhoneTextWatcher implements TextWatcher {
    
        private final EditText editText;
        private String previousString;
    
        public CustomPhoneTextWatcher(EditText editText) {
            this.editText = 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 editable) {
            // if the previous editable ends with a dash and new is shorter than previous
            // additionally remove preceding character
            if (previousString != null && previousString.endsWith("-") && editable.toString().length() < previousString.length()) {
                previousString = editable.toString();
                String removedCharacterPriorToDash = editable.toString().substring(0, editable.length() - 1);
                editText.setText(removedCharacterPriorToDash);
                int position = editText.length();
                Editable etext = editText.getText();
                Selection.setSelection(etext, position);
            } else {
                previousString = editable.toString();
                String numericString = StringUtils.removeNonnumeric(editable.toString());
                int stringLength = numericString.length();
                boolean startsWithOne = numericString.startsWith("1");
                numericString = numericString.substring(0, Math.min(stringLength, 10 + (startsWithOne ? 1 : 0)));
    
                int lastHyphenIndex = 6 + (startsWithOne ? 1 : 0);
                int secondToLastHyphenIndex = 3 + (startsWithOne ? 1 : 0);
    
                if (stringLength >= lastHyphenIndex) {
                    numericString = numericString.substring(0, lastHyphenIndex) + "-" + numericString.substring(lastHyphenIndex, numericString.length());
                }
                if (stringLength >= secondToLastHyphenIndex) {
                    numericString = numericString.substring(0, secondToLastHyphenIndex) + "-" + numericString.substring(secondToLastHyphenIndex, numericString.length());
                }
                if (numericString.startsWith("1")) {
                    numericString = numericString.substring(0, 1) + "-" + numericString.substring(1, numericString.length());
                }
                if (!numericString.equals(editable.toString())) {
                    editText.setText(numericString);
                    int position = editText.length();
                    Editable etext = editText.getText();
                    Selection.setSelection(etext, position);
                }
            }
        }
    }
    

    StringUtils.removeNonnumeric(editable.toString()) is a call to this method:

       public static String removeNonnumeric(String text) {
            return text.replaceAll("[^\\d]", "");
        }
    
    0 讨论(0)
  • 2020-12-17 21:33

    Thanks for the all above answer.

    • The editText.setOnKeyListener() will never invoke when your device has only soft keyboard.
    • If we strictly follow the rule to add "-", then this code not always show desire result.

      editText.addTextChangedListener(new PhoneNumberFormattingTextWatcher());

    but above code is best solution for formatting phone no.

    Apart from above this solution, I write a code which work on all types of condition::

    phoneNumber.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) {
            }
    
            @Override
            public void afterTextChanged(Editable s) {
                if (len > phoneNumber.getText().length() ){
                    len--;
                    return;
                }
                len = phoneNumber.getText().length();
    
                if (len == 4 || len== 8) {
                    String number = phoneNumber.getText().toString();
                    String dash = number.charAt(number.length() - 1) == '-' ? "" : "-";
                    number = number.substring(0, (len - 1)) + dash + number.substring((len - 1), number.length());
                    phoneNumber.setText(number);
                    phoneNumber.setSelection(number.length());
                }
            }
        });
    

    this line of code required to add "-" on 3rd & 6th position of number. if (len == 4 || len== 8)

    0 讨论(0)
  • 2020-12-17 21:35

    Do it yourself by using OnEditTextChangedListener and insert dash by counting number of chars, Counting Chars in EditText Changed Listener

    0 讨论(0)
  • 2020-12-17 21:38

    The most straightforward solution is to use PhoneNumberFormattingTextWatcher which will format the number according to the system locale.

    XML:

    <EditText
        android:id="@+id/phone_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/enter_phone_number"
        android:inputType="phone" />
    

    Add addTextChangedListener() in your class:

    EditText phoneNumber = (EditText)findViewById(R.id.phone_number);
    phoneNumber.addTextChangedListener(new PhoneNumberFormattingTextWatcher());
    
    0 讨论(0)
  • 2020-12-17 21:42

    I have a few small changes to the solution of neo108 so it can work with both soft keyboard and hard keyboard, in my code for example the edittext will follow the rule to automatically add " " at position 5 and 9.

    txtPhone.addTextChangedListener(new TextWatcher() {
    
            int keyDel;
    
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
            }
    
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                txtPhone.setOnKeyListener(new View.OnKeyListener() {
                    @Override
                    public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
                        if (keyCode == KeyEvent.KEYCODE_DEL) {
                            keyDel = 1;
                        }
                        return false;
                    }
                });
    
                String currentString = txtPhone.getText().toString();
                int currentLength = txtPhone.getText().length();
    
                if (currentLength == 5 || currentLength == 9) {
                    keyDel = 1;
                }
    
                if (keyDel == 0) {
                    if (currentLength == 4 || currentLength == 8) {
                        txtPhone.setText(txtPhone.getText() + " ");
                        txtPhone.setSelection(txtPhone.getText().length());
                    }
                } else {
                    if (currentLength != 5 && currentLength != 9) {
                        keyDel = 0;
                    } else if ((currentLength == 5 || currentLength == 9)
                            && !" ".equals(currentString.substring(currentLength - 1, currentLength))) {
                        txtPhone.setText(currentString.substring(0, currentLength - 1) + " "
                                + currentString.substring(currentLength - 1, currentLength));
                        txtPhone.setSelection(txtPhone.getText().length());
                    }
                }
            }
    
    0 讨论(0)
  • 2020-12-17 21:43

    Implement the following modified addTextChangedListener for txt_HomeNo. The code below is checking if the length of the text entered is 3 and if it is then add the - to it. Not a very robust solution but it works!

    txt_HomeNo.addTextChangedListener(new TextWatcher() {
    
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
    
            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) {
                int len = txt_HomeNo.getText().length();
                if(len == 3) {
                    txt_HomeNo.setText(txt_HomeNo.getText() + "-");
                    txt_HomeNo.setSelection(txt_HomeNo.getText().length());
                }
            } else {
                keyDel = 0;
            }
        }
    
        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
        }
    
        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub
        }
    });
    
    0 讨论(0)
提交回复
热议问题