如何在Android Studio中使用libphonenumber库获取国家代码。

huangapple go评论69阅读模式
英文:

how to use libphonenumber in android studio to get country code

问题

基本上我不知道如何在Android Studio中使用libphonenumber库来获取国家代码,比如(+91)。
我已经尝试过了,但它只会给我返回输入号码的前两个数字。

    phoneUtil = PhoneNumberUtil.createInstance(Name.this);
    try {
        // 手机号必须以'+'开头
        Phonenumber.PhoneNumber numberProto = phoneUtil.parse("+" + phone, null);
        countryCode = numberProto.getCountryCode();
    } catch (NumberParseException e) {
        System.err.println("出现了NumberParseException异常:" + e.toString());
    }
英文:

Basically I don't know how to use libphonenumber in android studio to get country code like(+91)
I have tried and but it's giving me the first two letter of my input number.

    phoneUtil = PhoneNumberUtil.createInstance(Name.this);
    try {
        // phone must begin with '+'
        Phonenumber.PhoneNumber numberProto = phoneUtil.parse("+"+phone, null);
        countryCode= numberProto.getCountryCode();
    } catch (NumberParseException e) {
        System.err.println("NumberParseException was thrown: " + e.toString());
    }

答案1

得分: 1

this might be helpful for u

public static String getMobileNoWithCode(String mobileNo) {
    try {
        List<Country> countries = Countries.getCountries();
        PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
        for (Country country : countries) {
            Phonenumber.PhoneNumber phNumberProto = phoneUtil.parse(
                    mobileNo, country.getDCode());
            if (phoneUtil.isValidNumber(phNumberProto))
                return country.getDCode() + mobileNo;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

Countries.class

public class Countries {

    private static final Country[] COUNTRIES = {
        new Country("AF", "Afghanistan", "+93", "AFN"),
        // ... (other countries)
        new Country("SE", "Sweden", "+46", "SEK"),
        // ...
    };

    // ... (other methods)
}

(Note: The code provided is a translation of the original code snippets you provided.)

英文:

this might be helpful for u

    public static String getMobileNoWithCode(String mobileNo) {
    try {
        List&lt;Country&gt; countries=Countries.getCountries();
        PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
        for (Country country :countries){
        Phonenumber.PhoneNumber phNumberProto = phoneUtil.parse(
                mobileNo, country.getDCode());
        if (phoneUtil.isValidNumber(phNumberProto))
            return country.getDCode()+mobileNo;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return &quot;&quot;;
}

Countries.class

public class Countries {
private static final Country[] COUNTRIES = {
new Country(&quot;AF&quot;, &quot;Afghanistan&quot;, &quot;+93&quot;, &quot;AFN&quot;),
new Country(&quot;AX&quot;, &quot;Aland Islands&quot;, &quot;+358&quot;, &quot;EUR&quot;),
new Country(&quot;AL&quot;, &quot;Albania&quot;, &quot;+355&quot;, &quot;ALL&quot;),
new Country(&quot;DZ&quot;, &quot;Algeria&quot;, &quot;+213&quot;, &quot;DZD&quot;),
new Country(&quot;AS&quot;, &quot;American Samoa&quot;, &quot;+1&quot;, &quot;USD&quot;),
new Country(&quot;AD&quot;, &quot;Andorra&quot;, &quot;+376&quot;, &quot;EUR&quot;),
new Country(&quot;AO&quot;, &quot;Angola&quot;, &quot;+244&quot;, &quot;AOA&quot;),
new Country(&quot;AI&quot;, &quot;Anguilla&quot;, &quot;+1&quot;, &quot;XCD&quot;),
new Country(&quot;AQ&quot;, &quot;Antarctica&quot;, &quot;+672&quot;, &quot;USD&quot;),
new Country(&quot;AG&quot;, &quot;Antigua and Barbuda&quot;, &quot;+1&quot;, &quot;XCD&quot;),
new Country(&quot;AR&quot;, &quot;Argentina&quot;, &quot;+54&quot;, &quot;ARS&quot;),
new Country(&quot;AM&quot;, &quot;Armenia&quot;, &quot;+374&quot;, &quot;AMD&quot;),
new Country(&quot;AW&quot;, &quot;Aruba&quot;, &quot;+297&quot;, &quot;AWG&quot;),
new Country(&quot;AU&quot;, &quot;Australia&quot;, &quot;+61&quot;, &quot;AUD&quot;),
new Country(&quot;AT&quot;, &quot;Austria&quot;, &quot;+43&quot;, &quot;EUR&quot;),
new Country(&quot;AZ&quot;, &quot;Azerbaijan&quot;, &quot;+994&quot;, &quot;AZN&quot;),
new Country(&quot;BS&quot;, &quot;Bahamas&quot;, &quot;+1&quot;, &quot;BSD&quot;),
new Country(&quot;BH&quot;, &quot;Bahrain&quot;, &quot;+973&quot;, &quot;BHD&quot;),
new Country(&quot;BD&quot;, &quot;Bangladesh&quot;, &quot;+880&quot;, &quot;BDT&quot;),
new Country(&quot;BB&quot;, &quot;Barbados&quot;, &quot;+1&quot;, &quot;BBD&quot;),
new Country(&quot;BY&quot;, &quot;Belarus&quot;, &quot;+375&quot;, &quot;BYR&quot;),
new Country(&quot;BE&quot;, &quot;Belgium&quot;, &quot;+32&quot;, &quot;EUR&quot;),
new Country(&quot;BZ&quot;, &quot;Belize&quot;, &quot;+501&quot;, &quot;BZD&quot;),
new Country(&quot;BJ&quot;, &quot;Benin&quot;, &quot;+229&quot;, &quot;XOF&quot;),
new Country(&quot;BM&quot;, &quot;Bermuda&quot;, &quot;+1&quot;, &quot;BMD&quot;),
new Country(&quot;BT&quot;, &quot;Bhutan&quot;, &quot;+975&quot;, &quot;BTN&quot;),
new Country(&quot;BO&quot;, &quot;Bolivia, Plurinational State of&quot;, &quot;+591&quot;, &quot;BOB&quot;),
new Country(&quot;BA&quot;, &quot;Bosnia and Herzegovina&quot;, &quot;+387&quot;, &quot;BAM&quot;),
new Country(&quot;BQ&quot;, &quot;Bonaire&quot;, &quot;+599&quot;, &quot;USD&quot;),
new Country(&quot;BW&quot;, &quot;Botswana&quot;, &quot;+267&quot;, &quot;BWP&quot;),
new Country(&quot;BV&quot;, &quot;Bouvet Island&quot;, &quot;+47&quot;, &quot;NOK&quot;),
new Country(&quot;BR&quot;, &quot;Brazil&quot;, &quot;+55&quot;, &quot;BRL&quot;),
new Country(&quot;IO&quot;, &quot;British Indian Ocean Territory&quot;, &quot;+246&quot;, &quot;USD&quot;),
new Country(&quot;BN&quot;, &quot;Brunei Darussalam&quot;, &quot;+673&quot;, &quot;BND&quot;),
new Country(&quot;BG&quot;, &quot;Bulgaria&quot;, &quot;+359&quot;, &quot;BGN&quot;),
new Country(&quot;BF&quot;, &quot;Burkina Faso&quot;, &quot;+226&quot;, &quot;XOF&quot;),
new Country(&quot;BI&quot;, &quot;Burundi&quot;, &quot;+257&quot;, &quot;BIF&quot;),
new Country(&quot;KH&quot;, &quot;Cambodia&quot;, &quot;+855&quot;, &quot;KHR&quot;),
new Country(&quot;CM&quot;, &quot;Cameroon&quot;, &quot;+237&quot;, &quot;XAF&quot;),
new Country(&quot;CA&quot;, &quot;Canada&quot;, &quot;+1&quot;, &quot;CAD&quot;),
new Country(&quot;CV&quot;, &quot;Cape Verde&quot;, &quot;+238&quot;, &quot;CVE&quot;),
new Country(&quot;KY&quot;, &quot;Cayman Islands&quot;, &quot;+345&quot;, &quot;KYD&quot;),
new Country(&quot;CF&quot;, &quot;Central African Republic&quot;, &quot;+236&quot;, &quot;XAF&quot;),
new Country(&quot;TD&quot;, &quot;Chad&quot;, &quot;+235&quot;, &quot;XAF&quot;),
new Country(&quot;CL&quot;, &quot;Chile&quot;, &quot;+56&quot;, &quot;CLP&quot;),
new Country(&quot;CN&quot;, &quot;China&quot;, &quot;+86&quot;, &quot;CNY&quot;),
new Country(&quot;CX&quot;, &quot;Christmas Island&quot;, &quot;+61&quot;, &quot;AUD&quot;),
new Country(&quot;CC&quot;, &quot;Cocos (Keeling) Islands&quot;, &quot;+61&quot;, &quot;AUD&quot;),
new Country(&quot;CO&quot;, &quot;Colombia&quot;, &quot;+57&quot;, &quot;COP&quot;),
new Country(&quot;KM&quot;, &quot;Comoros&quot;, &quot;+269&quot;, &quot;KMF&quot;),
new Country(&quot;CD&quot;, &quot;Congo, The Democratic Republic of the&quot;, &quot;+243&quot;, &quot;CDF&quot;),
new Country(&quot;CG&quot;, &quot;Congo&quot;, &quot;+242&quot;, &quot;XAF&quot;),
new Country(&quot;CK&quot;, &quot;Cook Islands&quot;, &quot;+682&quot;, &quot;NZD&quot;),
new Country(&quot;CR&quot;, &quot;Costa Rica&quot;, &quot;+506&quot;, &quot;CRC&quot;),
new Country(&quot;HR&quot;, &quot;Croatia&quot;, &quot;+385&quot;, &quot;HRK&quot;),
new Country(&quot;CU&quot;, &quot;Cuba&quot;, &quot;+53&quot;, &quot;CUP&quot;),
new Country(&quot;CW&quot;, &quot;Curacao&quot;, &quot;+599&quot;, &quot;ANG&quot;),
new Country(&quot;CY&quot;, &quot;Cyprus&quot;, &quot;+357&quot;, &quot;EUR&quot;),
new Country(&quot;CZ&quot;, &quot;Czech Republic&quot;, &quot;+420&quot;, &quot;CZK&quot;),
new Country(&quot;DK&quot;, &quot;Denmark&quot;, &quot;+45&quot;, &quot;DKK&quot;),
new Country(&quot;DJ&quot;, &quot;Djibouti&quot;, &quot;+253&quot;, &quot;DJF&quot;),
new Country(&quot;DM&quot;, &quot;Dominica&quot;, &quot;+1&quot;, &quot;XCD&quot;),
new Country(&quot;DO&quot;, &quot;Dominican Republic&quot;, &quot;+1&quot;, &quot;DOP&quot;),
new Country(&quot;TL&quot;, &quot;East Timor&quot;, &quot;+670&quot;, &quot;USD&quot;),
new Country(&quot;EC&quot;, &quot;Ecuador&quot;, &quot;+593&quot;, &quot;USD&quot;),
new Country(&quot;EG&quot;, &quot;Egypt&quot;, &quot;+20&quot;, &quot;EGP&quot;),
new Country(&quot;SV&quot;, &quot;El Salvador&quot;, &quot;+503&quot;, &quot;SVC&quot;),
new Country(&quot;GQ&quot;, &quot;Equatorial Guinea&quot;, &quot;+240&quot;, &quot;XAF&quot;),
new Country(&quot;ER&quot;, &quot;Eritrea&quot;, &quot;+291&quot;, &quot;ERN&quot;),
new Country(&quot;EE&quot;, &quot;Estonia&quot;, &quot;+372&quot;, &quot;EUR&quot;),
new Country(&quot;ET&quot;, &quot;Ethiopia&quot;, &quot;+251&quot;, &quot;ETB&quot;),
new Country(&quot;FK&quot;, &quot;Falkland Islands (Malvinas)&quot;, &quot;+500&quot;, &quot;FKP&quot;),
new Country(&quot;FO&quot;, &quot;Faroe Islands&quot;, &quot;+298&quot;, &quot;DKK&quot;),
new Country(&quot;FJ&quot;, &quot;Fiji&quot;, &quot;+679&quot;, &quot;FJD&quot;),
new Country(&quot;FI&quot;, &quot;Finland&quot;, &quot;+358&quot;, &quot;EUR&quot;),
new Country(&quot;FR&quot;, &quot;France&quot;, &quot;+33&quot;, &quot;EUR&quot;),
new Country(&quot;GF&quot;, &quot;French Guiana&quot;, &quot;+594&quot;, &quot;EUR&quot;),
new Country(&quot;TF&quot;, &quot;French Southern Territories&quot;, &quot;+262&quot;, &quot;EUR&quot;),
new Country(&quot;PF&quot;, &quot;French Polynesia&quot;, &quot;+689&quot;, &quot;XPF&quot;),
new Country(&quot;GA&quot;, &quot;Gabon&quot;, &quot;+241&quot;, &quot;XAF&quot;),
new Country(&quot;GM&quot;, &quot;Gambia&quot;, &quot;+220&quot;, &quot;GMD&quot;),
new Country(&quot;GE&quot;, &quot;Georgia&quot;, &quot;+995&quot;, &quot;GEL&quot;),
new Country(&quot;DE&quot;, &quot;Germany&quot;, &quot;+49&quot;, &quot;EUR&quot;),
new Country(&quot;GH&quot;, &quot;Ghana&quot;, &quot;+233&quot;, &quot;GHS&quot;),
new Country(&quot;GI&quot;, &quot;Gibraltar&quot;, &quot;+350&quot;, &quot;GIP&quot;),
new Country(&quot;GR&quot;, &quot;Greece&quot;, &quot;+30&quot;, &quot;EUR&quot;),
new Country(&quot;GL&quot;, &quot;Greenland&quot;, &quot;+299&quot;, &quot;DKK&quot;),
new Country(&quot;GD&quot;, &quot;Grenada&quot;, &quot;+1&quot;, &quot;XCD&quot;),
new Country(&quot;GP&quot;, &quot;Guadeloupe&quot;, &quot;+590&quot;, &quot;EUR&quot;),
new Country(&quot;GU&quot;, &quot;Guam&quot;, &quot;+1&quot;, &quot;USD&quot;),
new Country(&quot;GT&quot;, &quot;Guatemala&quot;, &quot;+502&quot;, &quot;GTQ&quot;),
new Country(&quot;GG&quot;, &quot;Guernsey&quot;, &quot;+44&quot;, &quot;GGP&quot;),
new Country(&quot;GN&quot;, &quot;Guinea&quot;, &quot;+224&quot;, &quot;GNF&quot;),
new Country(&quot;GW&quot;, &quot;Guinea-Bissau&quot;, &quot;+245&quot;, &quot;XOF&quot;),
new Country(&quot;GY&quot;, &quot;Guyana&quot;, &quot;+595&quot;, &quot;GYD&quot;),
new Country(&quot;HT&quot;, &quot;Haiti&quot;, &quot;+509&quot;, &quot;HTG&quot;),
new Country(&quot;HM&quot;, &quot;Heard Island and McDonald Islands&quot;, &quot;+000&quot;, &quot;AUD&quot;),
new Country(&quot;VA&quot;, &quot;Holy See (Vatican City State)&quot;, &quot;+379&quot;, &quot;EUR&quot;),
new Country(&quot;HN&quot;, &quot;Honduras&quot;, &quot;+504&quot;, &quot;HNL&quot;),
new Country(&quot;HK&quot;, &quot;Hong Kong&quot;, &quot;+852&quot;, &quot;HKD&quot;),
new Country(&quot;HU&quot;, &quot;Hungary&quot;, &quot;+36&quot;, &quot;HUF&quot;),
new Country(&quot;IS&quot;, &quot;Iceland&quot;, &quot;+354&quot;, &quot;ISK&quot;),
Utils.getDefaultCountry(),
new Country(&quot;ID&quot;, &quot;Indonesia&quot;, &quot;+62&quot;, &quot;IDR&quot;),
new Country(&quot;IR&quot;, &quot;Iran, Islamic Republic of&quot;, &quot;+98&quot;, &quot;IRR&quot;),
new Country(&quot;IQ&quot;, &quot;Iraq&quot;, &quot;+964&quot;, &quot;IQD&quot;),
new Country(&quot;IE&quot;, &quot;Ireland&quot;, &quot;+353&quot;, &quot;EUR&quot;),
new Country(&quot;IM&quot;, &quot;Isle of Man&quot;, &quot;+44&quot;, &quot;GBP&quot;),
new Country(&quot;IL&quot;, &quot;Israel&quot;, &quot;+972&quot;, &quot;ILS&quot;),
new Country(&quot;IT&quot;, &quot;Italy&quot;, &quot;+39&quot;, &quot;EUR&quot;),
new Country(&quot;CI&quot;, &quot;Ivory Coast&quot;, &quot;+225&quot;, &quot;XOF&quot;),
new Country(&quot;JM&quot;, &quot;Jamaica&quot;, &quot;+1&quot;, &quot;JMD&quot;),
new Country(&quot;JP&quot;, &quot;Japan&quot;, &quot;+81&quot;, &quot;JPY&quot;),
new Country(&quot;JE&quot;, &quot;Jersey&quot;, &quot;+44&quot;, &quot;JEP&quot;),
new Country(&quot;JO&quot;, &quot;Jordan&quot;, &quot;+962&quot;, &quot;JOD&quot;),
new Country(&quot;KZ&quot;, &quot;Kazakhstan&quot;, &quot;+7&quot;, &quot;KZT&quot;),
new Country(&quot;KE&quot;, &quot;Kenya&quot;, &quot;+254&quot;, &quot;KES&quot;),
new Country(&quot;KI&quot;, &quot;Kiribati&quot;, &quot;+686&quot;, &quot;AUD&quot;),
new Country(&quot;XK&quot;, &quot;Kosovo&quot;, &quot;+383&quot;, &quot;EUR&quot;),
new Country(&quot;KW&quot;, &quot;Kuwait&quot;, &quot;+965&quot;, &quot;KWD&quot;),
new Country(&quot;KG&quot;, &quot;Kyrgyzstan&quot;, &quot;+996&quot;, &quot;KGS&quot;),
new Country(&quot;LA&quot;, &quot;Lao People&#39;s Democratic Republic&quot;, &quot;+856&quot;, &quot;LAK&quot;),
new Country(&quot;LV&quot;, &quot;Latvia&quot;, &quot;+371&quot;, &quot;LVL&quot;),
new Country(&quot;LB&quot;, &quot;Lebanon&quot;, &quot;+961&quot;, &quot;LBP&quot;),
new Country(&quot;LS&quot;, &quot;Lesotho&quot;, &quot;+266&quot;, &quot;LSL&quot;),
new Country(&quot;LR&quot;, &quot;Liberia&quot;, &quot;+231&quot;, &quot;LRD&quot;),
new Country(&quot;LY&quot;, &quot;Libyan Arab Jamahiriya&quot;, &quot;+218&quot;, &quot;LYD&quot;),
new Country(&quot;LI&quot;, &quot;Liechtenstein&quot;, &quot;+423&quot;, &quot;CHF&quot;),
new Country(&quot;LT&quot;, &quot;Lithuania&quot;, &quot;+370&quot;, &quot;LTL&quot;),
new Country(&quot;LU&quot;, &quot;Luxembourg&quot;, &quot;+352&quot;, &quot;EUR&quot;),
new Country(&quot;MO&quot;, &quot;Macao&quot;, &quot;+853&quot;, &quot;MOP&quot;),
new Country(&quot;MK&quot;, &quot;Macedonia, The Former Yugoslav Republic of&quot;, &quot;+389&quot;,
&quot;MKD&quot;),
new Country(&quot;MG&quot;, &quot;Madagascar&quot;, &quot;+261&quot;, &quot;MGA&quot;),
new Country(&quot;MW&quot;, &quot;Malawi&quot;, &quot;+265&quot;, &quot;MWK&quot;),
new Country(&quot;MY&quot;, &quot;Malaysia&quot;, &quot;+60&quot;, &quot;MYR&quot;),
new Country(&quot;MV&quot;, &quot;Maldives&quot;, &quot;+960&quot;, &quot;MVR&quot;),
new Country(&quot;ML&quot;, &quot;Mali&quot;, &quot;+223&quot;, &quot;XOF&quot;),
new Country(&quot;MT&quot;, &quot;Malta&quot;, &quot;+356&quot;, &quot;EUR&quot;),
new Country(&quot;MH&quot;, &quot;Marshall Islands&quot;, &quot;+692&quot;, &quot;USD&quot;),
new Country(&quot;MQ&quot;, &quot;Martinique&quot;, &quot;+596&quot;, &quot;EUR&quot;),
new Country(&quot;MR&quot;, &quot;Mauritania&quot;, &quot;+222&quot;, &quot;MRO&quot;),
new Country(&quot;MU&quot;, &quot;Mauritius&quot;, &quot;+230&quot;, &quot;MUR&quot;),
new Country(&quot;YT&quot;, &quot;Mayotte&quot;, &quot;+262&quot;, &quot;EUR&quot;),
new Country(&quot;MX&quot;, &quot;Mexico&quot;, &quot;+52&quot;, &quot;MXN&quot;),
new Country(&quot;FM&quot;, &quot;Micronesia, Federated States of&quot;, &quot;+691&quot;, &quot;USD&quot;),
new Country(&quot;MD&quot;, &quot;Moldova, Republic of&quot;, &quot;+373&quot;, &quot;MDL&quot;),
new Country(&quot;MC&quot;, &quot;Monaco&quot;, &quot;+377&quot;, &quot;EUR&quot;),
new Country(&quot;MN&quot;, &quot;Mongolia&quot;, &quot;+976&quot;, &quot;MNT&quot;),
new Country(&quot;ME&quot;, &quot;Montenegro&quot;, &quot;+382&quot;, &quot;EUR&quot;),
new Country(&quot;MS&quot;, &quot;Montserrat&quot;, &quot;+1&quot;, &quot;XCD&quot;),
new Country(&quot;MA&quot;, &quot;Morocco&quot;, &quot;+212&quot;, &quot;MAD&quot;),
new Country(&quot;MZ&quot;, &quot;Mozambique&quot;, &quot;+258&quot;, &quot;MZN&quot;),
new Country(&quot;MM&quot;, &quot;Myanmar&quot;, &quot;+95&quot;, &quot;MMK&quot;),
new Country(&quot;NA&quot;, &quot;Namibia&quot;, &quot;+264&quot;, &quot;NAD&quot;),
new Country(&quot;NR&quot;, &quot;Nauru&quot;, &quot;+674&quot;, &quot;AUD&quot;),
new Country(&quot;NP&quot;, &quot;Nepal&quot;, &quot;+977&quot;, &quot;NPR&quot;),
new Country(&quot;NL&quot;, &quot;Netherlands&quot;, &quot;+31&quot;, &quot;EUR&quot;),
new Country(&quot;NC&quot;, &quot;New Caledonia&quot;, &quot;+687&quot;, &quot;XPF&quot;),
new Country(&quot;NZ&quot;, &quot;New Zealand&quot;, &quot;+64&quot;, &quot;NZD&quot;),
new Country(&quot;NI&quot;, &quot;Nicaragua&quot;, &quot;+505&quot;, &quot;NIO&quot;),
new Country(&quot;NE&quot;, &quot;Niger&quot;, &quot;+227&quot;, &quot;XOF&quot;),
new Country(&quot;NG&quot;, &quot;Nigeria&quot;, &quot;+234&quot;, &quot;NGN&quot;),
new Country(&quot;NU&quot;, &quot;Niue&quot;, &quot;+683&quot;, &quot;NZD&quot;),
new Country(&quot;NF&quot;, &quot;Norfolk Island&quot;, &quot;+672&quot;, &quot;AUD&quot;),
new Country(&quot;MP&quot;, &quot;Northern Mariana Islands&quot;, &quot;+1&quot;, &quot;USD&quot;),
new Country(&quot;KP&quot;, &quot;North Korea&quot;, &quot;+850&quot;, &quot;KPW&quot;),
new Country(&quot;NO&quot;, &quot;Norway&quot;, &quot;+47&quot;, &quot;NOK&quot;),
new Country(&quot;OM&quot;, &quot;Oman&quot;, &quot;+968&quot;, &quot;OMR&quot;),
new Country(&quot;PK&quot;, &quot;Pakistan&quot;, &quot;+92&quot;, &quot;PKR&quot;),
new Country(&quot;PW&quot;, &quot;Palau&quot;, &quot;+680&quot;, &quot;USD&quot;),
new Country(&quot;PS&quot;, &quot;Palestinian Territory, Occupied&quot;, &quot;+970&quot;, &quot;ILS&quot;),
new Country(&quot;PA&quot;, &quot;Panama&quot;, &quot;+507&quot;, &quot;PAB&quot;),
new Country(&quot;PG&quot;, &quot;Papua New Guinea&quot;, &quot;+675&quot;, &quot;PGK&quot;),
new Country(&quot;PY&quot;, &quot;Paraguay&quot;, &quot;+595&quot;, &quot;PYG&quot;),
new Country(&quot;PE&quot;, &quot;Peru&quot;, &quot;+51&quot;, &quot;PEN&quot;),
new Country(&quot;PH&quot;, &quot;Philippines&quot;, &quot;+63&quot;, &quot;PHP&quot;),
new Country(&quot;PN&quot;, &quot;Pitcairn&quot;, &quot;+872&quot;, &quot;NZD&quot;),
new Country(&quot;PL&quot;, &quot;Poland&quot;, &quot;+48&quot;, &quot;PLN&quot;),
new Country(&quot;PT&quot;, &quot;Portugal&quot;, &quot;+351&quot;, &quot;EUR&quot;),
new Country(&quot;PR&quot;, &quot;Puerto Rico&quot;, &quot;+1&quot;, &quot;USD&quot;),
new Country(&quot;QA&quot;, &quot;Qatar&quot;, &quot;+974&quot;, &quot;QAR&quot;),
new Country(&quot;RO&quot;, &quot;Romania&quot;, &quot;+40&quot;, &quot;RON&quot;),
new Country(&quot;RU&quot;, &quot;Russia&quot;, &quot;+7&quot;, &quot;RUB&quot;),
new Country(&quot;RW&quot;, &quot;Rwanda&quot;, &quot;+250&quot;, &quot;RWF&quot;),
new Country(&quot;RE&quot;, &quot;Reunion&quot;, &quot;+262&quot;, &quot;EUR&quot;),
new Country(&quot;BL&quot;, &quot;Saint Barthelemy&quot;, &quot;+590&quot;, &quot;EUR&quot;),
new Country(&quot;SH&quot;, &quot;Saint Helena, Ascension and Tristan Da Cunha&quot;, &quot;+290&quot;,
&quot;SHP&quot;),
new Country(&quot;KN&quot;, &quot;Saint Kitts and Nevis&quot;, &quot;+1&quot;, &quot;XCD&quot;),
new Country(&quot;LC&quot;, &quot;Saint Lucia&quot;, &quot;+1&quot;, &quot;XCD&quot;),
new Country(&quot;MF&quot;, &quot;Saint Martin&quot;, &quot;+590&quot;, &quot;EUR&quot;),
new Country(&quot;PM&quot;, &quot;Saint Pierre and Miquelon&quot;, &quot;+508&quot;, &quot;EUR&quot;),
new Country(&quot;VC&quot;, &quot;Saint Vincent and the Grenadines&quot;, &quot;+1&quot;, &quot;XCD&quot;),
new Country(&quot;WS&quot;, &quot;Samoa&quot;, &quot;+685&quot;, &quot;WST&quot;),
new Country(&quot;SM&quot;, &quot;San Marino&quot;, &quot;+378&quot;, &quot;EUR&quot;),
new Country(&quot;ST&quot;, &quot;Sao Tome and Principe&quot;, &quot;+239&quot;, &quot;STD&quot;),
new Country(&quot;SA&quot;, &quot;Saudi Arabia&quot;, &quot;+966&quot;, &quot;SAR&quot;),
new Country(&quot;SN&quot;, &quot;Senegal&quot;, &quot;+221&quot;, &quot;XOF&quot;),
new Country(&quot;RS&quot;, &quot;Serbia&quot;, &quot;+381&quot;, &quot;RSD&quot;),
new Country(&quot;SC&quot;, &quot;Seychelles&quot;, &quot;+248&quot;, &quot;SCR&quot;),
new Country(&quot;SL&quot;, &quot;Sierra Leone&quot;, &quot;+232&quot;, &quot;SLL&quot;),
new Country(&quot;SG&quot;, &quot;Singapore&quot;, &quot;+65&quot;, &quot;SGD&quot;),
new Country(&quot;SX&quot;, &quot;Sint Maarten&quot;, &quot;+1&quot;, &quot;ANG&quot;),
new Country(&quot;SK&quot;, &quot;Slovakia&quot;, &quot;+421&quot;, &quot;EUR&quot;),
new Country(&quot;SI&quot;, &quot;Slovenia&quot;, &quot;+386&quot;, &quot;EUR&quot;),
new Country(&quot;SB&quot;, &quot;Solomon Islands&quot;, &quot;+677&quot;, &quot;SBD&quot;),
new Country(&quot;SO&quot;, &quot;Somalia&quot;, &quot;+252&quot;, &quot;SOS&quot;),
new Country(&quot;ZA&quot;, &quot;South Africa&quot;, &quot;+27&quot;, &quot;ZAR&quot;),
new Country(&quot;SS&quot;, &quot;South Sudan&quot;, &quot;+211&quot;, &quot;SSP&quot;),
new Country(&quot;GS&quot;, &quot;South Georgia and the South Sandwich Islands&quot;, &quot;+500&quot;,
&quot;GBP&quot;),
new Country(&quot;KR&quot;, &quot;South Korea&quot;, &quot;+82&quot;, &quot;KRW&quot;),
new Country(&quot;ES&quot;, &quot;Spain&quot;, &quot;+34&quot;, &quot;EUR&quot;),
new Country(&quot;LK&quot;, &quot;Sri Lanka&quot;, &quot;+94&quot;, &quot;LKR&quot;),
new Country(&quot;SD&quot;, &quot;Sudan&quot;, &quot;+249&quot;, &quot;SDG&quot;),
new Country(&quot;SR&quot;, &quot;Suriname&quot;, &quot;+597&quot;, &quot;SRD&quot;),
new Country(&quot;SJ&quot;, &quot;Svalbard and Jan Mayen&quot;, &quot;+47&quot;, &quot;NOK&quot;),
new Country(&quot;SZ&quot;, &quot;Swaziland&quot;, &quot;+268&quot;, &quot;SZL&quot;),
new Country(&quot;SE&quot;, &quot;Sweden&quot;, &quot;+46&quot;, &quot;SEK&quot;),
new Country(&quot;CH&quot;, &quot;Switzerland&quot;, &quot;+41&quot;, &quot;CHF&quot;),
new Country(&quot;SY&quot;, &quot;Syrian Arab Republic&quot;, &quot;+963&quot;, &quot;SYP&quot;),
new Country(&quot;TW&quot;, &quot;Taiwan&quot;, &quot;+886&quot;, &quot;TWD&quot;),
new Country(&quot;TJ&quot;, &quot;Tajikistan&quot;, &quot;+992&quot;, &quot;TJS&quot;),
new Country(&quot;TZ&quot;, &quot;Tanzania, United Republic of&quot;, &quot;+255&quot;, &quot;TZS&quot;),
new Country(&quot;TH&quot;, &quot;Thailand&quot;, &quot;+66&quot;, &quot;THB&quot;),
new Country(&quot;TG&quot;, &quot;Togo&quot;, &quot;+228&quot;, &quot;XOF&quot;),
new Country(&quot;TK&quot;, &quot;Tokelau&quot;, &quot;+690&quot;, &quot;NZD&quot;),
new Country(&quot;TO&quot;, &quot;Tonga&quot;, &quot;+676&quot;, &quot;TOP&quot;),
new Country(&quot;TT&quot;, &quot;Trinidad and Tobago&quot;, &quot;+1&quot;, &quot;TTD&quot;),
new Country(&quot;TN&quot;, &quot;Tunisia&quot;, &quot;+216&quot;, &quot;TND&quot;),
new Country(&quot;TR&quot;, &quot;Turkey&quot;, &quot;+90&quot;, &quot;TRY&quot;),
new Country(&quot;TM&quot;, &quot;Turkmenistan&quot;, &quot;+993&quot;, &quot;TMT&quot;),
new Country(&quot;TC&quot;, &quot;Turks and Caicos Islands&quot;, &quot;+1&quot;, &quot;USD&quot;),
new Country(&quot;TV&quot;, &quot;Tuvalu&quot;, &quot;+688&quot;, &quot;AUD&quot;),
new Country(&quot;UM&quot;, &quot;U.S. Minor Outlying Islands&quot;, &quot;+1&quot;, &quot;USD&quot;),
new Country(&quot;UG&quot;, &quot;Uganda&quot;, &quot;+256&quot;, &quot;UGX&quot;),
new Country(&quot;UA&quot;, &quot;Ukraine&quot;, &quot;+380&quot;, &quot;UAH&quot;),
new Country(&quot;AE&quot;, &quot;United Arab Emirates&quot;, &quot;+971&quot;, &quot;AED&quot;),
new Country(&quot;GB&quot;, &quot;United Kingdom&quot;, &quot;+44&quot;, &quot;GBP&quot;),
new Country(&quot;US&quot;, &quot;United States&quot;, &quot;+1&quot;, &quot;USD&quot;),
new Country(&quot;UY&quot;, &quot;Uruguay&quot;, &quot;+598&quot;, &quot;UYU&quot;),
new Country(&quot;UZ&quot;, &quot;Uzbekistan&quot;, &quot;+998&quot;, &quot;UZS&quot;),
new Country(&quot;VU&quot;, &quot;Vanuatu&quot;, &quot;+678&quot;, &quot;VUV&quot;),
new Country(&quot;VE&quot;, &quot;Venezuela, Bolivarian Republic of&quot;, &quot;+58&quot;, &quot;VEF&quot;),
new Country(&quot;VN&quot;, &quot;Vietnam&quot;, &quot;+84&quot;, &quot;VND&quot;),
new Country(&quot;VG&quot;, &quot;Virgin Islands, British&quot;, &quot;+1&quot;, &quot;USD&quot;),
new Country(&quot;VI&quot;, &quot;Virgin Islands, U.S.&quot;, &quot;+1&quot;, &quot;USD&quot;),
new Country(&quot;WF&quot;, &quot;Wallis and Futuna&quot;, &quot;+681&quot;, &quot;XPF&quot;),
new Country(&quot;EH&quot;, &quot;Western Sahara&quot;, &quot;+212&quot;, &quot;MAD&quot;),
new Country(&quot;YE&quot;, &quot;Yemen&quot;, &quot;+967&quot;, &quot;YER&quot;),
new Country(&quot;ZM&quot;, &quot;Zambia&quot;, &quot;+260&quot;, &quot;ZMW&quot;),
new Country(&quot;ZW&quot;, &quot;Zimbabwe&quot;, &quot;+263&quot;, &quot;USD&quot;),
};
public static List&lt;Country&gt; getCountries() {
return new ArrayList&lt;&gt;(Arrays.asList(COUNTRIES));
}}

Country.class

public class Country implements Parcelable {
public static final Creator&lt;Country&gt; CREATOR = new Creator&lt;Country&gt;() {
@Override
public Country createFromParcel(Parcel in) {
return new Country(in);
}
@Override
public Country[] newArray(int size) {
return new Country[size];
}
};
private String name;
private String code;
@SerializedName(&quot;dial_code&quot;)
private String dCode;
public Country(String code, String name, String dCode, String eur) {
this.code = code;
this.name = name;
this.dCode = dCode;
}
private Country(Parcel in) {
name = in.readString();
code = in.readString();
dCode = in.readString();
}
public String getName() {
return name;
}
public String getCode() {
return code;
}
public String getDCode() {
return dCode;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(code);
dest.writeString(dCode);
}}

答案2

得分: 0

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        var a: EditText = findViewById(R.id.editText)
        var b: EditText = findViewById(R.id.editText2)
        var c: Button = findViewById(R.id.button)

        c.setOnClickListener {
            var result = contactNumber(a.text.toString(), b.text.toString())
            Toast.makeText(this, result, Toast.LENGTH_SHORT).show()
        }

    }

    fun contactNumber(ph: String, code: String): String {
        var phoneNumUtil: PhoneNumberUtil = PhoneNumberUtil.createInstance(this@MainActivity)
        var ab = ""
        try {
            val phoneNumber: Phonenumber.PhoneNumber = phoneNumUtil.parse(ph, code)
            var a =
                phoneNumUtil.format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL)
            ab = a
            Log.d("phoneNumUtil", a)
        } catch (ex: Exception) {
            ab = ex.toString()
            Log.d("phoneNumUtilError", ex.toString())
        }
        return ab
    }
}
英文:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var a: EditText = findViewById(R.id.editText)
var b: EditText = findViewById(R.id.editText2)
var c: Button = findViewById(R.id.button)
c.setOnClickListener {
var result = contactNumber(a.text.toString(), b.text.toString())
Toast.makeText(this, result, Toast.LENGTH_SHORT).show()
}
}
fun contactNumber(ph: String, code: String): String {
var phoneNumUtil: PhoneNumberUtil = PhoneNumberUtil.createInstance(this@MainActivity)
var ab = &quot;&quot;
try {
val phoneNumber: Phonenumber.PhoneNumber = phoneNumUtil.parse(ph, code)
var a =
phoneNumUtil.format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL)
ab = a
Log.d(&quot;phoneNumUtil&quot;, a)
} catch (ex: Exception) {
ab = ex.toString()
Log.d(&quot;phoneNumUtilError&quot;, ex.toString())
}
return ab
}
}

huangapple
  • 本文由 发表于 2020年7月24日 03:16:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63061606.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定