android: how do I format number as phone with parentheses

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-30 04:13:13

问题


I have a number that I need to format as a telephone number. If I do

 PhoneNumberUtils.formatNumber(numStr);

Then I get

888-555-1234

But what I need to get is

(888) 555-1234

How do I get the second one? Is there a standard android way?


回答1:


If you know the country for which you want to do it, you can use Google's open source library libphonenumber . Here is how you can format it:

String numberStr = "8885551234"
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
  PhoneNumber numberProto = phoneUtil.parse(numberStr, "US");
  //Since you know the country you can format it as follows:
  System.out.println(phoneUtil.format(numberProto, PhoneNumberFormat.NATIONAL));
} catch (NumberParseException e) {
  System.err.println("NumberParseException was thrown: " + e.toString());
}

If you don't know the country then for numberStr use E.164 format phone number and in place of country code use null.




回答2:


Don't know if you found what you were looking for, but I ended up writing a little method that takes the length of a string (since the phone numbers I get come from a web service and can be a variety of formats). I believe it should work (so far all my test cases have been with the first two options -- haven't tested the other two yet).

public static String FormatStringAsPhoneNumber(String input) {
    String output;
    switch (input.length()) {
        case 7:
            output = String.format("%s-%s", input.substring(0,3), input.substring(3,7));
            break;
        case 10:
            output = String.format("(%s) %s-%s", input.substring(0,3), input.substring(3,6), input.substring(6,10));
            break;
        case 11:
            output = String.format("%s (%s) %s-%s", input.substring(0,1) ,input.substring(1,4), input.substring(4,7), input.substring(7,11));
            break;
        case 12:
            output = String.format("+%s (%s) %s-%s", input.substring(0,2) ,input.substring(2,5), input.substring(5,8), input.substring(8,12));
            break;
        default:
            return null;
    }
    return output;
}



回答3:


You simply use this and get you want :

new PhoneNumberFormattingTextWatcher()

or Have look at this url :

http://developer.android.com/reference/android/telephony/PhoneNumberFormattingTextWatcher.html




回答4:


If you have the String "888-555-1234" - by using PhoneNumberUtils.formatNumber(numStr); you can simply do this:

String numStr = "888-555-1234";

numStr = "(" + numStr.substring(0,3) + ") " + numStr.substring(4);

System.out.print(numStr); // (888) 555-1234

However, this is hard coded. You would need to make sure the String had a full 10 digits before doing so.




回答5:


Try to use regex. This will help you. As for me, i use this:

                        var result = "+1 888-555-1234"


                        if (Pattern.compile("^\\+[\\d]+\\s[\\d]{1,3}\\s[\\d]+").matcher(result).find()) {
                            result = result.replaceFirst(" ", "(").replaceFirst(" ", ")").replace(" ","-")
                        }

                        if(Pattern.compile("^\\+[\\d]+\\s[\\d]{1,3}-[\\d]+").matcher(result).find()){
                            result = result.replaceFirst(" ", "(").replaceFirst("-", ")")
                        }
                        Timber.d("$result")

output: +1(888)555-1234



来源:https://stackoverflow.com/questions/21270686/android-how-do-i-format-number-as-phone-with-parentheses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!