来自Contry名称的Android国家代码使用微调器

时间:2014-06-23 07:55:05

标签: android spinner country-codes

在我的android应用程序中,手机号码验证部分是针对所有cuntries的I gt字符串数组,我们可以从spinner中选择所有国家/地区名称..我也有cuntry代码..我需要的是当用户选择时来自spinner的一个cuntry名称..它的特定ISD cde在edittext中显示...我需要做什么

我正在做我做的事情

<string-array name="country_arrays">
        <item>Afghanistan</item>
        <item>Aland Islands</item>
<item>Albania</item>
<item>Algeria</item>
<item>American Samoa</item>
<item>Andorra</item>
<item>Angola</item>
<item>Anguilla</item>
<item>Antarctica</item>
<item>Antigua and Barbuda</item>
<item>Argentina</item>
<item>Armenia</item>
<item>Aruba</item>
<item>Australia</item>
<item>Austria</item>
<item>Azerbaijan</item>
<item>Bahrain</item>
<item>Bangladesh</item>
<item>Barbados</item>
<item>Belarus</item>
<item>Belgium</item>
<item>Belize</item>
<item>Benin</item>
<item>Bermuda</item>
<item>Bhutan</item>
<item>Bolivia</item>
<item>Bosnia and Herzegovina</item>
<item>Botswana</item>
<item>Bouvet Island</item>
<item>Brazil</item>
<item>British Indian Ocean Territory</item>
<item>British Virgin Islands</item>
<item>Brunei</item>
<item>Bulgaria</item>
<item>Burkina Faso</item>
<item>Burundi</item>
<item>Cambodia</item>
<item>Cameroon</item>
<item>Canada</item>
<item>Cape Verde</item>
<item>Cayman Islands</item>
<item>Central African Republic</item>
<item>Chad</item>
<item>Chile</item>
<item>China</item>
<item>Christmas Island</item>
<item>Cocos (Keeling) Islands</item>
<item>Colombia</item>
<item>Comoros</item>
<item>Congo</item>
<item>Cook Islands</item>
<item>Costa Rica</item>
<item>Cote d\'Ivoire</item>
<item>Croatia</item>
<item>Cuba</item>
<item>Cyprus</item>
<item>Czech Republic</item>
<item>Democratic Republic of the Congo</item>
<item>Denmark</item>
<item>Djibouti</item>
<item>Dominica</item>
<item>Dominican Republic</item>
<item>East Timor</item>
<item>Ecuador</item>
<item>Egypt</item>
<item>El Salvador</item>
<item>Equatorial Guinea</item>
<item>Eritrea</item>
<item>Estonia</item>
<item>Ethiopia</item>
<item>Faeroe Islands</item>
<item>Falkland Islands</item>
<item>Fiji</item>
<item>Finland</item>
<item>Former Yugoslav Republic of Macedonia</item>
<item>France</item>
<item>French Guiana</item>
<item>French Polynesia</item>
<item>French Southern Territories</item>
<item>Gabon</item>
<item>Georgia</item>
<item>Germany</item>
<item>Ghana</item>
<item>Gibraltar</item>
<item>Greece</item>
<item>Greenland</item>
<item>Grenada</item>
<item>Guadeloupe</item>
<item>Guam</item>
<item>Guatemala</item>
<item>Guinea</item>
<item>Guinea-Bissau</item>
<item>Guyana</item>
<item>Haiti</item>
<item>Heard Island and McDonald Islands</item>
<item>Honduras</item>
<item>Hong Kong</item>
<item>Hungary</item>
<item>Iceland</item>
<item>India</item>
<item>Indonesia</item>
<item>Iran</item>
<item>Iraq</item>
<item>Ireland</item>
<item>Israel</item>
<item>Italy</item>
<item>Jamaica</item>
<item>Japan</item>
<item>Jordan</item>
<item>Kazakhstan</item>
<item>Kenya</item>
<item>Kiribati</item>
<item>Kuwait</item>
<item>Kyrgyzstan</item>
<item>Laos</item>
<item>Latvia</item>
<item>Lebanon</item>
<item>Lesotho</item>
<item>Liberia</item>
<item>Libya</item>
<item>Liechtenstein</item>
<item>Lithuania</item>
<item>Luxembourg</item>
<item>Macau</item>
<item>Madagascar</item>
<item>Malawi</item>
<item>Malaysia</item>
<item>Maldives</item>
<item>Mali</item>
<item>Malta</item>
<item>Marshall Islands</item>
<item>Martinique</item>
<item>Mauritania</item>
<item>Mauritius</item>
<item>Mayotte</item>
<item>Mexico</item>
<item>Micronesia</item>
<item>Moldova</item>
<item>Monaco</item>
<item>Mongolia</item>
<item>Montserrat</item>
<item>Morocco</item>
<item>Mozambique</item>
<item>Myanmar</item>
<item>Namibia</item>
<item>Nauru</item>
<item>Nepal</item>
<item>Netherlands</item>
<item>Netherlands Antilles</item>
<item>New Caledonia</item>
<item>New Zealand</item>
<item>Nicaragua</item>
<item>Niger</item>
<item>Nigeria</item>
<item>Niue</item>
<item>Norfolk Island</item>
<item>North Korea</item>
<item>Northern Marianas</item>
<item>Norway</item>
<item>Oman</item>
<item>Pakistan</item>
<item>Palau</item>
<item>Panama</item>
<item>Papua New Guinea</item>
<item>Paraguay</item>
<item>Peru</item>
<item>Philippines</item>
<item>Pitcairn Islands</item>
<item>Poland</item>
<item>Portugal</item>
<item>Puerto Rico</item>
<item>Qatar</item>
<item>Reunion</item>
<item>Romania</item>
<item>Russia</item>
<item>Rwanda</item>
<item>Sqo Tome and Principe</item>
<item>Saint Helena</item>
<item>Saint Kitts and Nevis</item>
<item>Saint Lucia</item>
<item>Saint Pierre and Miquelon</item>
<item>Saint Vincent and the Grenadines</item>
<item>Samoa</item>
<item>San Marino</item>
<item>Saudi Arabia</item>
<item>Senegal</item>
<item>Seychelles</item>
<item>Sierra Leone</item>
<item>Singapore</item>
<item>Slovakia</item>
<item>Slovenia</item>
<item>Solomon Islands</item>
<item>Somalia</item>
<item>South Africa</item>
<item>South Georgia and the South Sandwich Islands</item>
<item>South Korea</item>
<item>Spain</item>
<item>Sri Lanka</item>
<item>Sudan</item>
<item>Suriname</item>
<item>Svalbard and Jan Mayen</item>
<item>Swaziland</item>
<item>Sweden</item>
<item>Switzerland</item>
<item>Syria</item>
<item>Taiwan</item>
<item>Tajikistan</item>
<item>Tanzania</item>
<item>Thailand</item>
<item>The Bahamas</item>
<item>The Gambia</item>
<item>Togo</item>
<item>Tokelau</item>
<item>Tonga</item>
<item>Trinidad and Tobago</item>
<item>Tunisia</item>
<item>Turkey</item>
<item>Turkmenistan</item>
<item>Turks and Caicos Islands</item>
<item>Tuvalu</item>
<item>Virgin Islands</item>
<item>Uganda</item>
<item>Ukraine</item>
<item>United Arab Emirates</item>
<item>United Kingdom</item>
<item>United States</item>
<item>United States Minor Outlying Islands</item>
<item>Uruguay</item>
<item>Uzbekistan</item>
<item>Vanuatu</item>
<item>Vatican City</item>
<item>Venezuela</item>
<item>Vietnam</item>
<item>Wallis and Futuna</item>
<item>Western Sahara</item>
<item>Yemen</item>
<item>Yugoslavia</item>
<item>Zambia</item>
<item>Zimbabwe</item>

</string-array>



<Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:entries="@array/country_arrays"
        android:prompt="@string/country_prompt" />

6 个答案:

答案 0 :(得分:2)

您可以在strings.xml中声明一个数组数组

首先为每个国家/地区声明一个数组

<string-array name="data01">
    <item name="code">01</item>
    <item name="country">Albania</item>
</string-array>

<string-array name="data02">
    <item name="code">02</item>
    <item name="country">Algeria</item>
</string-array>
<!--
...
-->

然后声明数组数组

<array name="countries">
    <item>@array/data01</item>
    <item>@array/data02</item>
    <!--     ...        -->
</array>

在您的活动中完成此操作后,您只需解析此数组内容并保留对这两个国家/地区和代码的引用

Resources r = getResources();
TypedArray countrieCodes = r.obtainTypedArray(R.array.countries);

ArrayList<String> country = new ArrayList<String>();
ArrayList<String> code = new ArrayList<String>();

int cpt = countrieCodes.length();
for (int i = 0; i < cpt; ++i) {
    int id = countrieCodes.getResourceId(i, 0);
    code.add(r.getStringArray(id)[0]);
    country.add(r.getStringArray(id)[1]);
}

countrieCodes.recycle(); 

final ArrayList<String> fCode = code;
final ArrayList<String> fCountry = country;

final Spinner p = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, country);
p.setAdapter(dataAdapter);
p.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        String selectedCountry = (String)p.getSelectedItem();
        int selectedPosition = fCountry.indexOf(selectedCountry);
        String correspondingCode = fCode.get(selectedPosition);
        // Here is your corresponding country code
        System.out.println(correspondingCode);
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
    }
});

答案 1 :(得分:1)

package com.ketan.ramani;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatSpinner;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import java.util.ArrayList;

/**
 * Created by Ketan Ramani
 */

public class CountryCodeSpinner extends AppCompatSpinner {

    private ArrayList<CountryModel> arrayList;
    private CountryCodeAdapter countryCodeAdapter;

    public CountryCodeSpinner(Context context) {
        super(context);
    }

    public CountryCodeSpinner(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        fillCountryList();
        countryCodeAdapter = new CountryCodeAdapter(context, arrayList);
        //countryCodeAdapter = new CountryCodeAdapter(context, R.layout.spinner_country_code, arrayList);
        super.setAdapter(countryCodeAdapter);
    }

    public CountryCodeSpinner(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        fillCountryList();
        countryCodeAdapter = new CountryCodeAdapter(context, arrayList);
        //countryCodeAdapter = new CountryCodeAdapter(context, R.layout.spinner_country_code, arrayList);
        super.setAdapter(countryCodeAdapter);
    }

    private void fillCountryList() {
        //https://emojipedia.org/flags/
        arrayList = new ArrayList<>();
        arrayList.clear();
        arrayList.add(new CountryModel("af", "Afghanistan", "+93", ""));
        arrayList.add(new CountryModel("al", "Albania", "+355", ""));
        arrayList.add(new CountryModel("dz", "Algeria", "+213", ""));
        arrayList.add(new CountryModel("as", "American Samoa", "+1684", ""));
        arrayList.add(new CountryModel("ad", "Andorra", "+376", ""));
        arrayList.add(new CountryModel("ao", "Angola", "+244", ""));
        arrayList.add(new CountryModel("ai", "Anguilla", "+1264", ""));
        arrayList.add(new CountryModel("aq", "Antarctica", "+672", ""));
        arrayList.add(new CountryModel("ag", "Antigua and Barbuda", "+1268", ""));
        arrayList.add(new CountryModel("ar", "Argentina", "+54", ""));
        arrayList.add(new CountryModel("am", "Armenia", "+374", ""));
        arrayList.add(new CountryModel("aw", "Aruba", "+297", ""));
        arrayList.add(new CountryModel("au", "Australia", "+61", ""));
        arrayList.add(new CountryModel("at", "Austria", "+43", ""));
        arrayList.add(new CountryModel("az", "Azerbaijan", "+994", ""));
        arrayList.add(new CountryModel("bs", "Bahamas", "+1242", ""));
        arrayList.add(new CountryModel("bh", "Bahrain", "+973", ""));
        arrayList.add(new CountryModel("bd", "Bangladesh", "+880", ""));
        arrayList.add(new CountryModel("bb", "Barbados", "+1242", ""));
        arrayList.add(new CountryModel("by", "Belarus", "+375", ""));
        arrayList.add(new CountryModel("be", "Belgium", "+32", ""));
        arrayList.add(new CountryModel("bz", "Belize", "+501", ""));
        arrayList.add(new CountryModel("bj", "Benin", "+229", ""));
        arrayList.add(new CountryModel("bm", "Bermuda", "+1441", ""));
        arrayList.add(new CountryModel("bt", "Bhutan", "+975", ""));
        arrayList.add(new CountryModel("bo", "Bolivia", "+591", ""));
        arrayList.add(new CountryModel("ba", "Bosnia And Herzegovina", "+387", ""));
        arrayList.add(new CountryModel("bw", "Botswana", "+267", ""));
        arrayList.add(new CountryModel("br", "Brazil", "+55", ""));
        arrayList.add(new CountryModel("io", "British Indian Ocean Territory", "+246", ""));
        arrayList.add(new CountryModel("vg", "British Virgin Islands", "+1284", ""));
        arrayList.add(new CountryModel("bn", "Brunei Darussalam", "+673", ""));
        arrayList.add(new CountryModel("bg", "Bulgaria", "+359", ""));
        arrayList.add(new CountryModel("bf", "Burkina Faso", "+226", ""));
        arrayList.add(new CountryModel("bi", "Burundi", "+257", ""));
        arrayList.add(new CountryModel("kh", "Cambodia", "+855", ""));
        arrayList.add(new CountryModel("cm", "Cameroon", "+237", ""));
        arrayList.add(new CountryModel("ca", "Canada", "+1", ""));
        arrayList.add(new CountryModel("cv", "Cape Verde", "+238", ""));
        arrayList.add(new CountryModel("ky", "Cayman Islands", "+345", ""));
        arrayList.add(new CountryModel("cf", "Central African Republic", "+236", ""));
        arrayList.add(new CountryModel("td", "Chad", "+235", ""));
        arrayList.add(new CountryModel("cl", "Chile", "+56", ""));
        arrayList.add(new CountryModel("cn", "China", "+86", ""));
        arrayList.add(new CountryModel("cx", "Christmas Island", "+61", ""));
        arrayList.add(new CountryModel("cc", "Cocos (keeling) Islands", "+61", ""));
        arrayList.add(new CountryModel("co", "Colombia", "+57", ""));
        arrayList.add(new CountryModel("km", "Comoros", "+269", ""));
        arrayList.add(new CountryModel("ck", "Cook Islands", "+682", ""));
        arrayList.add(new CountryModel("cr", "Costa Rica", "+506", ""));
        arrayList.add(new CountryModel("hr", "Croatia", "+385", ""));
        arrayList.add(new CountryModel("cu", "Cuba", "+53", ""));
        arrayList.add(new CountryModel("cy", "Cyprus", "+357", ""));
        arrayList.add(new CountryModel("cz", "Czech Republic", "+420", ""));
        arrayList.add(new CountryModel("ci", "Côte D'ivoire", "+225", ""));
        arrayList.add(new CountryModel("cd", "Democratic Republic of the Congo", "+243", ""));
        arrayList.add(new CountryModel("dk", "Denmark", "+45", ""));
        arrayList.add(new CountryModel("dj", "Djibouti", "+253", ""));
        arrayList.add(new CountryModel("dm", "Dominica", "+1767", ""));
        arrayList.add(new CountryModel("do", "Dominican Republic", "+1849", ""));
        arrayList.add(new CountryModel("ec", "Ecuador", "+593", ""));
        arrayList.add(new CountryModel("eg", "Egypt", "+20", ""));
        arrayList.add(new CountryModel("sv", "El Salvador", "+503", ""));
        arrayList.add(new CountryModel("gq", "Equatorial Guinea", "+240", ""));
        arrayList.add(new CountryModel("er", "Eritrea", "+291", ""));
        arrayList.add(new CountryModel("ee", "Estonia", "+372", ""));
        arrayList.add(new CountryModel("et", "Ethiopia", "+251", ""));
        arrayList.add(new CountryModel("fk", "Falkland Islands (malvinas)", "+500", ""));
        arrayList.add(new CountryModel("fo", "Faroe Islands", "+298", ""));
        arrayList.add(new CountryModel("fj", "Fiji", "+679", ""));
        arrayList.add(new CountryModel("fi", "Finland", "+358", ""));
        arrayList.add(new CountryModel("fr", "France", "+33", ""));
        arrayList.add(new CountryModel("gf", "French Guiana", "+594", ""));
        arrayList.add(new CountryModel("pf", "French Polynesia", "+689", ""));
        arrayList.add(new CountryModel("ga", "Gabon", "+241", ""));
        arrayList.add(new CountryModel("gm", "Gambia", "+220", ""));
        arrayList.add(new CountryModel("ge", "Georgia", "+995", ""));
        arrayList.add(new CountryModel("de", "Germany", "+49", ""));
        arrayList.add(new CountryModel("gh", "Ghana", "+233", ""));
        arrayList.add(new CountryModel("gi", "Gibraltar", "+350", ""));
        arrayList.add(new CountryModel("gr", "Greece", "+30", ""));
        arrayList.add(new CountryModel("gl", "Greenland", "+299", ""));
        arrayList.add(new CountryModel("gd", "Grenada", "+1473", ""));
        arrayList.add(new CountryModel("gp", "Guadeloupe", "+590", ""));
        arrayList.add(new CountryModel("gu", "Guam", "+1671", ""));
        arrayList.add(new CountryModel("gt", "Guatemala", "+502", ""));
        arrayList.add(new CountryModel("gg", "Guernsey", "+44", ""));
        arrayList.add(new CountryModel("gn", "Guinea", "+224", ""));
        arrayList.add(new CountryModel("gw", "Guinea-Bissau", "+245", ""));
        arrayList.add(new CountryModel("gy", "Guyana", "+592", ""));
        arrayList.add(new CountryModel("ht", "Haiti", "+509", ""));
        arrayList.add(new CountryModel("va", "Holy See (Vatican City State)", "+379", ""));
        arrayList.add(new CountryModel("hn", "Honduras", "+504", ""));
        arrayList.add(new CountryModel("hk", "Hong Kong", "+852", ""));
        arrayList.add(new CountryModel("hu", "Hungary", "+36", ""));
        arrayList.add(new CountryModel("is", "Iceland", "+354", ""));
        arrayList.add(new CountryModel("in", "India", "+91", ""));
        arrayList.add(new CountryModel("id", "Indonesia", "+62", ""));
        arrayList.add(new CountryModel("ir", "Iran", "+98", ""));
        arrayList.add(new CountryModel("iq", "Iraq", "+964", ""));
        arrayList.add(new CountryModel("ie", "Ireland", "+353", ""));
        arrayList.add(new CountryModel("im", "Isle Of Man", "+44", ""));
        arrayList.add(new CountryModel("il", "Israel", "+972", ""));
        arrayList.add(new CountryModel("it", "Italy", "+39", ""));
        arrayList.add(new CountryModel("jm", "Jamaica", "+1876", ""));
        arrayList.add(new CountryModel("jp", "Japan", "+81", ""));
        arrayList.add(new CountryModel("je", "Jersey", "+44", ""));
        arrayList.add(new CountryModel("jo", "Jordan", "+962", ""));
        arrayList.add(new CountryModel("kz", "Kazakhstan", "+7", ""));
        arrayList.add(new CountryModel("ke", "Kenya", "+254", ""));
        arrayList.add(new CountryModel("ki", "Kiribati", "+686", ""));
        arrayList.add(new CountryModel("xk", "Kosovo", "+383", ""));
        arrayList.add(new CountryModel("kw", "Kuwait", "+965", ""));
        arrayList.add(new CountryModel("kg", "Kyrgyzstan", "+996", ""));
        arrayList.add(new CountryModel("la", "Lao People's Democratic Republic", "+856", ""));
        arrayList.add(new CountryModel("lv", "Latvia", "+371", ""));
        arrayList.add(new CountryModel("lb", "Lebanon", "+961", ""));
        arrayList.add(new CountryModel("ls", "Lesotho", "+266", ""));
        arrayList.add(new CountryModel("lr", "Liberia", "+231", ""));
        arrayList.add(new CountryModel("ly", "Libya", "+218", ""));
        arrayList.add(new CountryModel("li", "Liechtenstein", "+423", ""));
        arrayList.add(new CountryModel("lt", "Lithuania", "+370", ""));
        arrayList.add(new CountryModel("lu", "Luxembourg", "+352", ""));
        arrayList.add(new CountryModel("mo", "Macao Sar China", "+853", ""));
        arrayList.add(new CountryModel("mk", "Macedonia", "+389", ""));
        arrayList.add(new CountryModel("mg", "Madagascar", "+261", ""));
        arrayList.add(new CountryModel("mw", "Malawi", "+265", ""));
        arrayList.add(new CountryModel("my", "Malaysia", "+60", ""));
        arrayList.add(new CountryModel("mv", "Maldives", "+960", ""));
        arrayList.add(new CountryModel("ml", "Mali", "+223", ""));
        arrayList.add(new CountryModel("mt", "Malta", "+356", ""));
        arrayList.add(new CountryModel("mh", "Marshall Islands", "+692", ""));
        arrayList.add(new CountryModel("mq", "Martinique", "+596", ""));
        arrayList.add(new CountryModel("mr", "Mauritania", "+222", ""));
        arrayList.add(new CountryModel("mu", "Mauritius", "+230", ""));
        arrayList.add(new CountryModel("yt", "Mayotte", "+262", ""));
        arrayList.add(new CountryModel("mx", "Mexico", "+52", ""));
        arrayList.add(new CountryModel("fm", "Micronesia", "+691", ""));
        arrayList.add(new CountryModel("md", "Moldova", "+373", ""));
        arrayList.add(new CountryModel("mc", "Monaco", "+377", ""));
        arrayList.add(new CountryModel("mn", "Mongolia", "+976", ""));
        arrayList.add(new CountryModel("me", "Montenegro", "+382", ""));
        arrayList.add(new CountryModel("ms", "Montserrat", "+1664", ""));
        arrayList.add(new CountryModel("ma", "Morocco", "+212", ""));
        arrayList.add(new CountryModel("mz", "Mozambique", "+258", ""));
        arrayList.add(new CountryModel("mm", "Myanmar (Burma)", "+95", ""));
        arrayList.add(new CountryModel("na", "Namibia", "+264", ""));
        arrayList.add(new CountryModel("nr", "Nauru", "+674", ""));
        arrayList.add(new CountryModel("np", "Nepal", "+977", ""));
        arrayList.add(new CountryModel("nl", "Netherlands", "+31", ""));
        arrayList.add(new CountryModel("nc", "New Caledonia", "+687", ""));
        arrayList.add(new CountryModel("nz", "New Zealand", "+64", ""));
        arrayList.add(new CountryModel("ni", "Nicaragua", "+505", ""));
        arrayList.add(new CountryModel("ne", "Niger", "+227", ""));
        arrayList.add(new CountryModel("ng", "Nigeria", "+234", ""));
        arrayList.add(new CountryModel("nu", "Niue", "+683", ""));
        arrayList.add(new CountryModel("nf", "Norfolk Island", "+1670", ""));
        arrayList.add(new CountryModel("kp", "North Korea", "+672", ""));
        arrayList.add(new CountryModel("mp", "Northern Mariana Islands", "+850", ""));
        arrayList.add(new CountryModel("no", "Norway", "+47", ""));
        arrayList.add(new CountryModel("om", "Oman", "+968", ""));
        arrayList.add(new CountryModel("pk", "Pakistan", "+92", ""));
        arrayList.add(new CountryModel("pw", "Palau", "+680", ""));
        arrayList.add(new CountryModel("ps", "Palestinian Territory, Occupied", "+970", ""));
        arrayList.add(new CountryModel("pa", "Panama", "+507", ""));
        arrayList.add(new CountryModel("pg", "Papua New Guinea", "+675", ""));
        arrayList.add(new CountryModel("py", "Paraguay", "+595", ""));
        arrayList.add(new CountryModel("pe", "Peru", "+51", ""));
        arrayList.add(new CountryModel("ph", "Philippines", "+63", ""));
        arrayList.add(new CountryModel("pn", "Pitcairn Islands", "+870", ""));
        arrayList.add(new CountryModel("pl", "Poland", "+48", ""));
        arrayList.add(new CountryModel("pt", "Portugal", "+351", ""));
        arrayList.add(new CountryModel("pr", "Puerto Rico", "+1939", ""));
        arrayList.add(new CountryModel("qa", "Qatar", "+974", ""));
        arrayList.add(new CountryModel("cg", "Republic of the Congo - Brazzaville", "+242", ""));
        arrayList.add(new CountryModel("ro", "Romania", "+40", ""));
        arrayList.add(new CountryModel("ru", "Russian Federation", "+7", ""));
        arrayList.add(new CountryModel("rw", "Rwanda", "+250", ""));
        arrayList.add(new CountryModel("re", "Réunion", "+262", ""));
        arrayList.add(new CountryModel("bl", "Saint Barthélemy", "+590", ""));
        arrayList.add(new CountryModel("sh", "Saint Helena", "+290", ""));
        arrayList.add(new CountryModel("kn", "Saint Kitts & Nevis", "+1869", ""));
        arrayList.add(new CountryModel("lc", "Saint Lucia", "+1758", ""));
        arrayList.add(new CountryModel("mf", "Saint Martin", "+590", ""));
        arrayList.add(new CountryModel("pm", "Saint Pierre & Miquelon", "+508", ""));
        arrayList.add(new CountryModel("vc", "Saint Vincent & The Grenadines", "+1784", ""));
        arrayList.add(new CountryModel("ws", "Samoa", "+685", ""));
        arrayList.add(new CountryModel("sm", "San Marino", "+378", ""));
        arrayList.add(new CountryModel("st", "Sao Tome & Principe", "+239", ""));
        arrayList.add(new CountryModel("sa", "Saudi Arabia", "+966", ""));
        arrayList.add(new CountryModel("sn", "Senegal", "+221", ""));
        arrayList.add(new CountryModel("rs", "Serbia", "+381", ""));
        arrayList.add(new CountryModel("sc", "Seychelles", "+248", ""));
        arrayList.add(new CountryModel("sl", "Sierra Leone", "+232", ""));
        arrayList.add(new CountryModel("sg", "Singapore", "+65", ""));
        arrayList.add(new CountryModel("sx", "Sint Maarten", "+1", ""));
        arrayList.add(new CountryModel("sk", "Slovakia", "+421", ""));
        arrayList.add(new CountryModel("si", "Slovenia", "+386", ""));
        arrayList.add(new CountryModel("sb", "Solomon Islands", "+677", ""));
        arrayList.add(new CountryModel("so", "Somalia", "+252", ""));
        arrayList.add(new CountryModel("za", "South Africa", "+27", ""));
        arrayList.add(new CountryModel("gs", "South Africa (South Georgia & South Sandwich Islands)", "+500", ""));
        arrayList.add(new CountryModel("kr", "South Korea", "+82", ""));
        arrayList.add(new CountryModel("ss", "South Sudan", "+211", ""));
        arrayList.add(new CountryModel("es", "Spain", "+34", ""));
        arrayList.add(new CountryModel("lk", "Sri Lanka", "+94", ""));
        arrayList.add(new CountryModel("sd", "Sudan", "+249", ""));
        arrayList.add(new CountryModel("sr", "Suriname", "+597", ""));
        arrayList.add(new CountryModel("sz", "Swaziland", "+268", ""));
        arrayList.add(new CountryModel("se", "Sweden", "+46", ""));
        arrayList.add(new CountryModel("ch", "Switzerland", "+41", ""));
        arrayList.add(new CountryModel("sy", "Syrian Arab Republic", "+963", ""));
        arrayList.add(new CountryModel("tw", "Taiwan", "+886", ""));
        arrayList.add(new CountryModel("tj", "Tajikistan", "+992", ""));
        arrayList.add(new CountryModel("tz", "Tanzania", "+255", ""));
        arrayList.add(new CountryModel("th", "Thailand", "+66", ""));
        arrayList.add(new CountryModel("tl", "Timor-Leste", "+670", ""));
        arrayList.add(new CountryModel("tg", "Togo", "+228", ""));
        arrayList.add(new CountryModel("tk", "Tokelau", "+690", ""));
        arrayList.add(new CountryModel("to", "Tonga", "+676", ""));
        arrayList.add(new CountryModel("tt", "Trinidad & Tobago", "+1868", ""));
        arrayList.add(new CountryModel("tn", "Tunisia", "+216", ""));
        arrayList.add(new CountryModel("tr", "Turkey", "+90", ""));
        arrayList.add(new CountryModel("tm", "Turkmenistan", "+993", ""));
        arrayList.add(new CountryModel("tc", "Turks & Caicos Islands", "+1649", ""));
        arrayList.add(new CountryModel("tv", "Tuvalu", "+688", ""));
        arrayList.add(new CountryModel("ug", "Uganda", "+256", ""));
        arrayList.add(new CountryModel("ua", "Ukraine", "+380", ""));
        arrayList.add(new CountryModel("ae", "United Arab Emirates", "+971", ""));
        arrayList.add(new CountryModel("gb", "United Kingdom", "+44", ""));
        arrayList.add(new CountryModel("us", "United States", "+1", ""));
        arrayList.add(new CountryModel("uy", "Uruguay", "+598", ""));
        arrayList.add(new CountryModel("vi", "US Virgin Islands", "+1340", ""));
        arrayList.add(new CountryModel("uz", "Uzbekistan", "+998", ""));
        arrayList.add(new CountryModel("vu", "Vanuatu", "+678", ""));
        arrayList.add(new CountryModel("ve", "Venezuela", "+58", ""));
        arrayList.add(new CountryModel("vn", "Vietnam", "+84", ""));
        arrayList.add(new CountryModel("wf", "Wallis And Futuna", "+681", ""));
        arrayList.add(new CountryModel("ye", "Yemen", "+967", ""));
        arrayList.add(new CountryModel("zm", "Zambia", "+260", ""));
        arrayList.add(new CountryModel("zw", "Zimbabwe", "+263", ""));
        arrayList.add(new CountryModel("ax", "Åland Islands", "+358", ""));
    }

    class CountryModel {

        private String iso, phoneCode, countryName, countryFlag;

        public CountryModel(String iso, String countryName, String phoneCode, String countryFlag) {
            this.iso = iso;
            this.countryName = countryName;
            this.phoneCode = phoneCode;
            this.countryFlag = countryFlag;
        }

        public String getIso() {
            return iso;
        }

        public void setIso(String iso) {
            this.iso = iso;
        }

        public String getPhoneCode() {
            return phoneCode;
        }

        public void setPhoneCode(String phoneCode) {
            this.phoneCode = phoneCode;
        }

        public String getCountryName() {
            return countryName;
        }

        public void setCountryName(String countryName) {
            this.countryName = countryName;
        }

        public String getCountryFlag() {
            return countryFlag;
        }

        public void setCountryFlag(String countryFlag) {
            this.countryFlag = countryFlag;
        }
    }

    public class CountryCodeAdapter extends ArrayAdapter<CountryModel> {
        private LayoutInflater inflater;
        private Context context;
        private ArrayList<CountryModel> arrayList;
        private int resource;

        public CountryCodeAdapter(@NonNull Context context, ArrayList<CountryModel> arrayList) {
            super(context, 0, arrayList);
            this.inflater = LayoutInflater.from(context);
            this.context = context;
            this.arrayList = arrayList;
        }

        public CountryCodeAdapter(@NonNull Context context, @LayoutRes int resource, ArrayList<CountryModel> arrayList) {
            super(context, resource, 0, arrayList);
            this.inflater = LayoutInflater.from(context);
            this.context = context;
            this.arrayList = arrayList;
            this.resource = resource;
        }

        @Override
        public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            return createItemView(position, convertView, parent);
        }

        @Override
        public @NonNull
        View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            return createItemView(position, convertView, parent);
        }

        private View createItemView(int position, View convertView, ViewGroup parent) {

            final View view;

            CountryModel model = arrayList.get(position);

            if (resource == 0) {
                LinearLayoutCompat rootLayout = new LinearLayoutCompat(context);
                rootLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                rootLayout.setBackgroundColor(Color.WHITE);

                AppCompatTextView ccTextView = new AppCompatTextView(context);
                ccTextView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                ccTextView.setBackgroundColor(Color.WHITE);
                ccTextView.setTextColor(Color.BLACK);
                ccTextView.setPadding(10, 5, 10, 5);
                ccTextView.setTextSize(15);
                ccTextView.setText(model.getCountryFlag() + "  " + model.getCountryName() + "  (" + model.getPhoneCode() + ")");
                rootLayout.addView(ccTextView);
                view = rootLayout;
            } else {
                view = inflater.inflate(resource, parent, false);
                AppCompatTextView tvCountry = (AppCompatTextView) view.findViewById(R.id.tvCountry);
                tvCountry.setText(model.getCountryFlag() + "  " + model.getCountryName() + "  (" + model.getPhoneCode() + ")");
            }

            return view;
        }
    }
}

spinner_country_code.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:orientation="vertical">

    <android.support.v7.widget.AppCompatTextView
        android:id="@+id/tvCountry"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@null"
        android:paddingLeft="9dp"
        android:paddingTop="3dp"
        android:paddingRight="9dp"
        android:paddingBottom="3dp"
        android:textColor="@android:color/black"
        android:textColorHint="@android:color/black"
        android:textSize="15sp"
        tools:text="Spinner Default Title" />

</android.support.v7.widget.LinearLayoutCompat>

在XML中使用类

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@android:color/white"
    tools:context=".MainActivity">

    <com.ketan.ramani.CountryCodeSpinner
        android:id="@+id/ccs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:spinnerMode="dropdown"/>

</LinearLayout>

在JAVA中使用班级

private CountryCodeSpinner ccs;
ccs = findViewById(R.id.ccs);
ccs.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        CountryCodeSpinner.CountryModel model = (CountryCodeSpinner.CountryModel) parent.getAdapter().getItem(position);
        Log.e("KetanRamani", ": " + model.getPhoneCode());
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
});

使用以下代码获取国家/地区代码值

CountryCodeSpinner.CountryModel model = (CountryCodeSpinner.CountryModel) ccs.getSelectedItem();
Log.e("KetanRamani", ": " + model.getPhoneCode() + " " + model.getCountryName());

OUTPUT

答案 2 :(得分:0)

所以你想在每个国家展示ISD代码吗?它可以使用IF语句或Switch完成。但肯定会需要很多代码。您可以像这样使用数组或arrayList:

String[] ISD_CODE = {"+639","+123",......} //example only



   public void onItemSelected(AdapterView<?> adapter, View view,
                int position, long id) {
               final String result = ISD_CODE[position];
    }

当然它必须与您的国家/地区数组中的位置相同,但我建议您必须使用HashMap。

答案 3 :(得分:0)

Country Code Picker (CCP)库可能会对您有所帮助。它将为您节省样板代码。您只需要在布局xml中放置CountryCodePicker视图。

这是CCP视图enter image description here

来自PlayStore的

Download Sample application

您的视图将与您的手机editText类似。 CCP view

点击CCP将打开一个对话框,其中包含可供选择的国家/地区列表。在这里,您可以使用国家/地区名称,代码名称和电话代码搜索国家/地区。enter image description here 可以轻松阅读所选国家/地区详细信有关详细信息,请转到Library's Github page

该库可以通过组合国家代码和运营商号码为您提供完整的号码,但它不会验证号码的结构。

答案 4 :(得分:0)

请参阅以下代码,它可能会对您有所帮助

Locale[] locale = Locale.getAvailableLocales();
    ArrayList<String> countries = new ArrayList<String>();
    String country;
    for( Locale loc : locale ){
        country = loc.getDisplayCountry();
        if( country.length() > 0 && !countries.contains(country) ){
            countries.add( country );
        }
    }
    Collections.sort(countries, String.CASE_INSENSITIVE_ORDER);

    Spinner citizenship = (Spinner)findViewById(R.id.input_citizenship);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, countries);
    citizenship.setAdapter(adapter);## 

答案 5 :(得分:0)

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;



public class NextActivity extends Activity implements AdapterView.OnItemSelectedListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_next);
        String[] countery = {"Afghanistan","Albania"
                ,"Algeria"
                ,"Andorra"
                ,"Angola"
                ,"Anguilla"
                ,"Antigua & Barbuda"
                ,"Argentina"
                ,"Armenia"
                ,"Australia"
                ,"Austria"
                ,"Azerbaijan"
                ,"Bahamas"
                ,"Bahrain"
                ,"Bangladesh"
                ,"Barbados"
                ,"Belarus"
                ,"Belgium"
                ,"Belize"
                ,"Benin"
                ,"Bermuda"
                ,"Bhutan"
                ,"Bolivia"
                ,"Bosnia & Herzegovina"
                ,"Botswana"
                ,"Brazil"
                ,"Brunei Darussalam"
                ,"Bulgaria"
                ,"Burkina Faso"
                ,"Myanmar/Burma"
                ,"Burundi"
                ,"Cambodia"
                ,"Cameroon"
                ,"Canada"
                ,"Cape Verde"
                ,"Cayman Islands"
                ,"Central African Republic"
                ,"Chad"
                ,"Chile"
                ,"China"
                ,"Colombia"
                ,"Comoros"
                ,"Congo"
                ,"Costa Rica"
                ,"Croatia"
                ,"Cuba"
                ,"Cyprus"
                ,"Czech Republic"
                ,"Democratic Republic of the Congo"
                ,"Denmark"
                ,"Djibouti"
                ,"Dominican Republic"
                ,"Dominica"
                ,"Ecuador"
                ,"Egypt"
                ,"El Salvador"
                ,"Equatorial Guinea"
                ,"Eritrea"
                ,"Estonia"
                ,"Ethiopia"
                ,"Fiji"
                ,"Finland"
                ,"France"
                ,"French Guiana"
                ,"Gabon"
                ,"Gambia"
                ,"Georgia"
                ,"Germany"
                ,"Ghana"
                ,"Great Britain"
                ,"Greece"
                ,"Grenada"
                ,"Guadeloupe"
                ,"Guatemala"
                ,"Guinea"
                ,"Guinea-Bissau"
                ,"Guyana"
                ,"Haiti"
                ,"Honduras"
                ,"Hungary"
                ,"Iceland"
                ,"India"
                ,"Indonesia"
                ,"Iran"
                ,"Iraq"
                ,"Israel and the Occupied Territories"
                ,"Italy"
                ,"Ivory Coast (Cote d'Ivoire)"
                ,"Jamaica"
                ,"Japan"
                ,"Jordan"
                ,"Kazakhstan"
                ,"Kenya"
                ,"Kosovo"
                ,"Kuwait"
                ,"Kyrgyz Republic (Kyrgyzstan)"
                ,"Laos"
                ,"Latvia"
                ,"Lebanon"
                ,"Lesotho"
                ,"Liberia"
                ,"Libya"
                ,"Liechtenstein"
                ,"Lithuania"
                ,"Luxembourg"
                ,"Republic of Macedonia"
                ,"Madagascar"
                ,"Malawi"
                ,"Malaysia"
                ,"Maldives"
                ,"Mali"
                ,"Malta"
                ,"Martinique"
                ,"Mauritania"
                ,"Mauritius"
                ,"Mayotte"
                ,"Mexico"
                ,"Moldova, Republic of Monaco"
                ,"Mongolia"
                ,"Montenegro"
                ,"Montserrat"
                ,"Morocco"
                ,"Mozambique"
                ,"Namibia"
                ,"Nepal"
                ,"Netherlands"
                ,"New Zealand"
                ,"Nicaragua"
                ,"Niger"
                ,"Nigeria"
                ,"Korea, Democratic Republic of (North Korea)"
                ,"Norway"
                ,"Oman"
                ,"Pacific Islands"
                ,"Pakistan"
                ,"Panama"
                ,"Papua New Guinea"
                ,"Paraguay"
                ,"Peru"
                ,"Philippines"
                ,"Poland"
                ,"Portugal"
                ,"Puerto Rico"
                ,"Qatar"
                ,"Reunion"
                ,"Romania"
                ,"Russian Federation"
                ,"Rwanda"
                ,"Saint Kitts and Nevis"
                ,"Saint Lucia"
                ,"Saint Vincent's & Grenadines"
                ,"Samoa"
                ,"Sao Tome and Principe"
                ,"Saudi Arabia"
                ,"Senegal"
                ,"Serbia"
                ,"Seychelles"
                ,"Sierra Leone"
                ,"Singapore"
                ,"Slovak Republic "
                ,"Slovenia"
                ,"Solomon Islands"
                ,"Somalia"
                ,"South Africa"
                ,"Korea, Republic of (South Korea)"
                ,"South Sudan"
                ,"Spain"
                ,"Sri Lanka"
                ,"Sudan"
                ,"Suriname"
                ,"Swaziland"
                ,"Sweden"
                ,"Switzerland"
                ,"Syria"
                ,"Tajikistan"
                ,"Tanzania"
                ,"Thailand"
                ,"Timor Leste"
                ,"Togo"
                ,"Trinidad & Tobago"
                ,"Tunisia"
                ,"Turkey"
                ,"Turkmenistan"
                ,"Turks & Caicos Islands"
                ,"Uganda"
                ,"Ukraine"
                ,"United Arab Emirates"
                ,"United States of America (USA)"
                ,"Uruguay"
                ,"Uzbekistan"
                ,"Venezuela"
                ,"Vietnam"
                ,"Virgin Islands (UK)"
                ,"Virgin Islands (US)"
                ,"Yemen"
                ,"Zambia"
                ,"Zimbabwe"};
        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        spinner.setOnItemSelectedListener(this);
        ArrayAdapter aa = new ArrayAdapter(this, android.R.layout.simple_spinner_item, country);
        aa.setDropDownViewResource(android.R.layout.simple_spinner_item);
        spinner.setAdapter(aa);
        Button button = (Button) findViewById(R.id.button2);

    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}