简体   繁体   中英

Phone number formatting in Android

In my app, the user enters his phone number. For example, If he enters: 123456 I want him to see: 12 34 56 How can I do that?

Try this its working based on locale :

etNumber.addTextChangedListener(new PhoneNumberFormattingTextWatcher());

You can also use with country code. https://developer.android.com/reference/android/telephony/PhoneNumberFormattingTextWatcher.html

You should have a look on the PhoneNumberUtils in the android.telephony API: https://developer.android.com/reference/android/telephony/PhoneNumberUtils.html

Edit:

There you find the method formatNumber which you can use to display the phone number.

This method tries to format the number according to the rules for the country it is from. If this isn't possible it return the raw number.

public static String formatNumber (String phoneNumber)

This method tries to format the number according the given country's default convention (the country parameter is to set with a two letter country code like US , GB or DE . For further information look here: https://en.wikipedia.org/wiki/ISO_3166-1 ). If the number is not valid the method return null .

public static String formatNumber (String phoneNumber, String defaultCountryIso)

You can add text watcher to your edit text and check the size of text till 2 place and add space in it like as follows

callphoneEditText.addTextChangedListener(new CustomTextWatcherForPhoneNumber());

The following class does the required thing you needed

  public static class CustomTextWatcherForPhoneNumber implements TextWatcher {

        // Change this to what you want... ' ', '-' etc..
        private static final char space = ' ';

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

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

        @Override
        public void afterTextChanged(Editable s) {
            // Remove spacing char
            if (s.length() > 0 && (s.length() % 5) == 0) {
                final char c = s.charAt(s.length() - 1);
                if (space == c) {
                    s.delete(s.length() - 1, s.length());
                }
            }
            // Insert char where needed.
            if (s.length() > 0 && (s.length() % 5) == 0) {
                char c = s.charAt(s.length() - 1);
                // Only if its a digit where there should be a space we insert a
                // space
                if (Character.isDigit(c)
                        && TextUtils.split(s.toString(), String.valueOf(space)).length <= 2) { ****// At this point you will check values and add space between them****
                    s.insert(s.length() - 1, String.valueOf(space));
                }
            }
        }
    }

Check it and let me know if it is helpful to you or not

you can look at libphonenumber library from google code. https://github.com/googlei18n/libphonenumber .

I did a small app using that for android and it works pretty well.

The XX XX XX XX number format is usually used in France. so you can do something like

String formattedNumber = phoneUtil.parse(numberProto, "FR");

They also have a 'format as you type' feature, which may be what you are interested in too. do check out their examples.

//Formatting Phone Numbers 'as you type'

PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("US");
System.out.println(formatter.inputDigit('6'));  // Outputs "6"
...  // Input more digits
System.out.println(formatter.inputDigit('3'));  // Now outputs "650 253"

edit

you can use the link below https://rawgit.com/googlei18n/libphonenumber/master/javascript/i18n/phonenumbers/demo-compiled.html to try out what the code returns. (of interest is the output of the "asYouTypeFormatter"

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM