如何使用PhoneNumberUtils格式化电话号码?

时间:2023-01-14 14:19:44

How can I format a phone number using PhoneNumberUtils?

如何使用PhoneNumberUtils格式化电话号码?

E.g.: 1234567890(123) 456-7890

例如:1234567890→(123)456-7890

5 个解决方案

#1


52  

At its most basic:

最基本的:

String formattedNumber = PhoneNumberUtils.formatNumber(unformattedNumber);

This will automatically format the number according to the rules for the country the number is from.

这将根据该号码所在国家/地区的规则自动格式化数字。

You can also format Editable text in-place using:

您还可以使用以下格式对可编辑文本进行格式化:

PhoneNumberUtils.formatNumber(Editable text, int defaultFormattingType);

Take a look at PhoneNumberUtils for more options.

查看PhoneNumberUtils以获取更多选项。

#2


20  

I opted to use google's version (https://github.com/googlei18n/libphonenumber) because then the min SDK can be lower (I think it isn't in Android SDK until 21).

我选择使用谷歌的版本(https://github.com/googlei18n/libphonenumber)因为然后最小的SDK可以更低(我认为它直到21年才在Android SDK中)。

Use is something like this:

使用是这样的:

PhoneNumberUtil pnu = PhoneNumberUtil.getInstance();
Phonenumber.PhoneNumber pn = pnu.parse("1234567890", "US");
String pnE164 = pnu.format(pn, PhoneNumberUtil.PhoneNumberFormat.E164);

In Android Studio one need add this to dependencies in build.gradle:

在Android Studio中,需要将其添加到build.gradle中的依赖项:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    ...
    compile 'com.googlecode.libphonenumber:libphonenumber:7.2.2'
}

#3


10  

formatNumber got deprecated on LOLLIPOP, after that you need to add the locale as an extra argument.

在LOLLIPOP上不推荐使用formatNumber,之后您需要将语言环境添加为额外参数。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  return PhoneNumberUtils.formatNumber(yourStringPhone,Locale.getDefault().getCountry());
} else {
//Deprecated method
  return PhoneNumberUtils.formatNumber(yourStringPhone); 
}

#4


7  

It looks like this library from Google might be a better solution for doing customized formatting in Android.

看起来Google的这个库可能是在Android中进行自定义格式化的更好解决方案。

API docs: https://github.com/googlei18n/libphonenumber/blob/master/README.md

API文档:https://github.com/googlei18n/libphonenumber/blob/master/README.md

#5


0  

Standalone solution for format raw phone number, if you don`t know your country and need support on pre-lollipop.

格式原始电话号码的独立解决方案,如果您不了解您所在的国家/地区并且需要前棒棒糖支持。

fun formatNumberCompat(rawPhone: String?, countryIso: String = ""): String {
    if (rawPhone == null) return ""

    var countryName = countryIso
    if (countryName.isBlank()) {
        countryName = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Resources.getSystem().configuration.locales[0].country
        } else {
            Resources.getSystem().configuration.locale.country
        }
    }

    return if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        PhoneNumberUtils.formatNumber(rawPhone)
    } else {
        PhoneNumberUtils.formatNumber(rawPhone, countryName)
    }
}

#1


52  

At its most basic:

最基本的:

String formattedNumber = PhoneNumberUtils.formatNumber(unformattedNumber);

This will automatically format the number according to the rules for the country the number is from.

这将根据该号码所在国家/地区的规则自动格式化数字。

You can also format Editable text in-place using:

您还可以使用以下格式对可编辑文本进行格式化:

PhoneNumberUtils.formatNumber(Editable text, int defaultFormattingType);

Take a look at PhoneNumberUtils for more options.

查看PhoneNumberUtils以获取更多选项。

#2


20  

I opted to use google's version (https://github.com/googlei18n/libphonenumber) because then the min SDK can be lower (I think it isn't in Android SDK until 21).

我选择使用谷歌的版本(https://github.com/googlei18n/libphonenumber)因为然后最小的SDK可以更低(我认为它直到21年才在Android SDK中)。

Use is something like this:

使用是这样的:

PhoneNumberUtil pnu = PhoneNumberUtil.getInstance();
Phonenumber.PhoneNumber pn = pnu.parse("1234567890", "US");
String pnE164 = pnu.format(pn, PhoneNumberUtil.PhoneNumberFormat.E164);

In Android Studio one need add this to dependencies in build.gradle:

在Android Studio中,需要将其添加到build.gradle中的依赖项:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    ...
    compile 'com.googlecode.libphonenumber:libphonenumber:7.2.2'
}

#3


10  

formatNumber got deprecated on LOLLIPOP, after that you need to add the locale as an extra argument.

在LOLLIPOP上不推荐使用formatNumber,之后您需要将语言环境添加为额外参数。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  return PhoneNumberUtils.formatNumber(yourStringPhone,Locale.getDefault().getCountry());
} else {
//Deprecated method
  return PhoneNumberUtils.formatNumber(yourStringPhone); 
}

#4


7  

It looks like this library from Google might be a better solution for doing customized formatting in Android.

看起来Google的这个库可能是在Android中进行自定义格式化的更好解决方案。

API docs: https://github.com/googlei18n/libphonenumber/blob/master/README.md

API文档:https://github.com/googlei18n/libphonenumber/blob/master/README.md

#5


0  

Standalone solution for format raw phone number, if you don`t know your country and need support on pre-lollipop.

格式原始电话号码的独立解决方案,如果您不了解您所在的国家/地区并且需要前棒棒糖支持。

fun formatNumberCompat(rawPhone: String?, countryIso: String = ""): String {
    if (rawPhone == null) return ""

    var countryName = countryIso
    if (countryName.isBlank()) {
        countryName = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Resources.getSystem().configuration.locales[0].country
        } else {
            Resources.getSystem().configuration.locale.country
        }
    }

    return if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        PhoneNumberUtils.formatNumber(rawPhone)
    } else {
        PhoneNumberUtils.formatNumber(rawPhone, countryName)
    }
}