使用微调器从国家名称中获取 Android 国家/地区代码

2024-04-23

在我的android应用程序中,有一个手机号码验证部分,我可以为所有国家提供字符串数组,我们可以从微调器中选择所有国家/地区名称..并且我也有国家/地区代码..我需要的是当用户选择一个国家/地区名称时来自 spinner ..其特定的 ISD cde 没有显示在编辑文本中...我需要为此做什么

我正在付出我所做的

<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" />

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());
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用微调器从国家名称中获取 Android 国家/地区代码 的相关文章

  • 已发布的 Flutter 应用程序在启动时崩溃

    编辑 此问题的解决方案是将您的 flutter 版本升级到较新的 dev 版本 then 1 7 0 您还可以上传单独的 APK 版本 但我个人不喜欢这个选项 请确保您没有从 flutter github 开发存储库下载 错误的构建 因为那
  • 单击 RecyclerView 内的 ImageView 时更改图像资源

    每当我单击它时 单击图像 而不是项目 我都会尝试更改回收器视图内特定位置的设备图像资源 我尝试将 setOnClickListener 放入 onBindViewHolder 方法中 但只有最后一个项目受到影响 这是我的回收者视图 http
  • 应用程序实例是否始终在任何活动之前创建?

    在 Android 中 您可以通过扩展 Application 类并在 Manifest 中声明名称来提供您自己的 Application 类实现 我的问题是 这个实现是否总是在初始活动之前创建 或者活动可以在应用程序实例有时间创建之前启动
  • Android Studio - 错误:未捕获翻译错误:com.android.dx.cf.code.SimException:本地 0001:无效

    我刚刚使用 Android Studio 设置了一台新计算机 并从 bitbucket 导入了我的项目 问题是我现在在尝试构建项目时遇到此错误 信息 Gradle 任务 app clean app generateDebugSources
  • 如何使用 adb 在设备上安装现有的 Android 应用程序?

    在开发过程中 我使用类似的东西 adb s 192 168 1 77 5555 uninstall com myApp app adb s 192 168 1 77 5555 install path to android debug ap
  • Google Firebase - 如何删除崩溃报告?

    我最终失明了吗 还是没有明显的方法可以通过 Google Firebase Web 控制台删除 Firebase 崩溃报告 我的 Android 应用程序已成功记录报告 但现在出现大量 开发崩溃 占用了我在控制台中的大部分视图 这使得找到实
  • SQLite (Android):使用 ORDER BY 更新查询

    Android SQLite 我想要在 myTable 中的其他行之间插入行在android中使用SQLite 为此 我尝试增加从第 3 行开始的所有行的 id 这样 我就可以在位置 3 处插入新行 myTable 的主键是列 id 表中没
  • 按钮未显示在屏幕上

    我创建了一个应用程序 其中显示带有图像和文本的列表视图 我在页面末尾添加按钮 但这没有显示在屏幕上 我是 Android 新手 我该如何解决这个问题 这是我的 UI XML 代码
  • 如何使用闹钟音量和 setAudioAttributes 来播放铃声?

    所以我正在尝试了解音频属性 这是我到目前为止所拥有的 alarm getSound will return a proper URI to pick a ringtone Ringtone tone RingtoneManager getR
  • 连接到不可发现的蓝牙设备

    我正在开发一个安卓应用程序 只是一个一般性问题 是否可以连接到公开不可发现的设备 提前致谢 如果您之前已与该设备配对 则即使该设备未处于可发现模式 也可以再次连接到该设备 参见这篇文章 以编程方式连接到配对的蓝牙设备 https stack
  • React Native Android 发布 apk 是调试,而不是发布

    我有一个现有的 Android 应用程序 我已根据以下内容将 React Native v0 30 活动添加到项目中docs http facebook github io react native releases next docs i
  • 在 WebView 中完成 AdBlock

    我即将在我的 Android 应用程序中推出 WebView AdBlocking 我想知道这是否会有效地阻止广告 或者在 Webview 本身内是否还有更多工作要做 我尚未修改 基本上我有一个存储在 Android 资产中的主机文件 其中
  • 为什么按钮上的 maxWidth 不起作用以及如何解决它?

    我的布局上有两个按钮 在大屏幕设备 平板电脑 上我想限制它们的宽度 这样它们看起来就不会很荒谬 我希望使用 maxWidth 属性 但它显然在我的场景中没有任何作用 这是布局定义 按钮使用布局的整个宽度 忽略 maxWidth 中的任何值
  • Android - 当不在栏顶部时推送通知空白

    我在使用 Android 推送通知时遇到一个小问题 如果有 3 个通知 并且只有其中一个显示标题和消息 位于酒吧顶部的那个 如果有人知道可能是什么问题 请告诉我 请参阅此链接上的图像 这就是我接收通知的方式http postimg org
  • 从多个选项卡中的编辑文本字段获取文本

    我正在尝试创建一个使用选项卡作为输入表单的 Android 应用程序 基本上 我希望对其进行设置 以便用户可以在一个选项卡上输入一些信息 然后提交该信息 或者转到另一个选项卡并输入更多信息 然后从两个选项卡提交信息 我正在使用操作栏和片段来
  • Android Web Intent 问题

    G day 免责声明 我不是 Android 开发人员 我正在对我所描述的问题进行质量检查 我用来描述这个问题的技术术语可能是错误的 我正在测试一个 Android 应用程序 该应用程序在其清单中描述它可以使用 type 的地址处理 Web
  • 如何在android中的操作栏中创建Edittext?

    我们可以在操作栏中使用编辑文本吗 在阅读了 Google 中的大量资源后 我找不到如何在操作栏中创建编辑文本 谁能告诉我该怎么做 您可以设置自定义View为了ActionBar像这样 getActionBar setCustomView R
  • 通过列表视图检查动态生成的复选框时遇到问题

    我知道其他成员已经提出了这个问题 一些成员也给出了解决方案 但问题是我没有找到任何适合我的应用程序的解决方案 我正在创建一个应用程序 其中我有一个屏幕 它将显示动态列表视图 其中包含列表项 复选框和三个文本视图 一个用于候选人姓名 另外两个
  • 如何在Android Compose中使用otf类型字体文件?

    我正在学习使用 Android Jetpack Compose 现在我有一个正则 otf字体文件在资产 字体 我想在文本中使用它 java lang RuntimeException Font asset not found commonu
  • 更改Android菜单的背景颜色[重复]

    这个问题在这里已经有答案了 我正在尝试将标准浅灰色更改为浅绿色 似乎没有一个简单的方法可以做到这一点 例如 通过 Android 主题 但我找到了一个解决方法 如本页所述 http tinyurl com 342dgn3 http tiny

随机推荐