繁体   English   中英

国家/地区代码未从 Android 中的国家/地区代码选择器获取

[英]Country code is not getting from country code picker in Android

我正在 Android 应用程序中开发注册活动。 我想显示国家/地区电话代码(从设备获取国家/地区)。 它可以显示包含国旗、国家电话代码、国家名称的国家列表。 我从https://github.com/mukeshsolanki/country-picker-android获取国家代码选择器的代码。 它包含完整的代码。 我想设置默认国家是在手机里。

TelephonyManager tm = (TelephonyManager)this.getSystemService(this.TELEPHONY_SERVICE);
    String countryCodeValue = tm.getNetworkCountryIso();
    System.out.println("country = "+country);

当我使用这个代码时,我得到了国家代码“in”。 但我想显示电话代码和标志,姓名。 当我打开屏幕。 我想自动显示它。

我的代码如下所示

主文件

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/colorBackground"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fitsSystemWindows="true">

<LinearLayout
    android:layout_gravity="center_vertical"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="56dp"
    android:paddingLeft="24dp"
    android:paddingRight="24dp">

    <ImageView
        android:background="@drawable/logo"
        android:layout_gravity="center_horizontal"
        android:layout_width="150dp"
        android:layout_height="150dp" />


    <Button
        android:text="Country"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/buttonCountry" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:backgroundTint="#d11f08"
        android:entries="@array/android_dropdown_arrays"
        android:padding="5dp"  />

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp">
        <EditText android:id="@+id/editTextPhone"
            android:layout_width="match_parent"
            android:layout_height="44dp"
            android:inputType="phone"
            android:hint="Password"/>
    </android.support.design.widget.TextInputLayout>

    <android.support.v7.widget.AppCompatButton
        android:id="@+id/buttonSubmit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:textColor="@color/colorAccent"
        android:layout_marginTop="24dp"
        android:layout_marginBottom="24dp"
        android:padding="12dp"
        android:text="Login"/>



</LinearLayout>
</ScrollView>

主要活动

public class MainActivity extends AppCompatActivity {

EditText editText;
Button button;
Button buttonCountry;

private Spinner spinner1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    editText = (EditText) findViewById(R.id.editTextPhone);

    button = (Button) findViewById(R.id.buttonSubmit);

    buttonCountry = (Button) findViewById(R.id.buttonCountry);
    spinner1 = (Spinner) findViewById(R.id.spinner1);

    TelephonyManager tm = (TelephonyManager)this.getSystemService(this.TELEPHONY_SERVICE);
    String countryCodeValue = tm.getNetworkCountryIso();
    System.out.println("country = "+countryCodeValue);
    //String mPhoneNumber = tm.getLine1Number();  //not getting phone number
    //System.out.println("phone no = "+mPhoneNumber);



    buttonCountry.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            spinner1.setOnItemSelectedListener(new ItemSelectedListener());

            final CountryPicker picker = CountryPicker.newInstance("Select Country");
            picker.show(getSupportFragmentManager(), "COUNTRY_PICKER");
            picker.setListener(new CountryPickerListener() {
                @Override
                public void onSelectCountry(String name, String code, String dialCode, int flagDrawableResID) {
                    // Implement your code here

                    Log.d("LOGTAG", "output1 : name = "+name+" code = "+code+" dialcode = "+dialCode+" flag = "+flagDrawableResID);
                    picker.dismiss();
                }
            });
        }
    });


    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });


}

public class ItemSelectedListener implements AdapterView.OnItemSelectedListener {
    //get strings of first item
    String firstItem = String.valueOf(spinner1.getSelectedItem());
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        if (firstItem.equals(String.valueOf(spinner1.getSelectedItem()))) {
            // ToDo when first item is selected
        } else {
            Toast.makeText(parent.getContext(),
                    "You have selected : " + parent.getItemAtPosition(pos).toString(),
                    Toast.LENGTH_LONG).show();
            // Todo when item is selected by the user
        }
    }

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

    }

}
}

参考: https : //github.com/hbb20/CountryCodePickerProject

尝试使用 Lat, long 获取国家/地区代码

Geocoder geocoder = new Geocoder(context, Locale.getDefault());

    try {
        List<Address> addressList = geocoder.getFromLocation(
                lati, longi, 1); // lati : Latitude ,longi : Longitude 
        if (addressList != null && addressList.size() > 0) {
            Address address = addressList.get(0);
            code=addressList.get(0).getCountryCode();
            System.out.println("code :: "+addressList.get(0).getCountryCode());

        }
    } catch (IOException e) {
        Log.e(TAG, "Unable connect to Geocoder", e);
    }

String locale = context.getResources().getConfiguration().locale.getCountry();

你可以使用这个库点击这里然后你可以做到这一点

 new DialogPlusBuilder().blurBackground()
               .buildCountriesListDialog(true,new DialogPlus.CountriesDialogListener() {
                   @Override
                   public void onItemClicked(CountryDataModel countryDataModel, DialogPlus dialogPlus) {
                       super.onItemClicked(countryDataModel, dialogPlus);
                       Toast.makeText(MainActivity.this,countryDataModel.getName()+ countryDataModel.getPhone_code(), Toast.LENGTH_SHORT).show();
                   }
               })
               .show(this.getSupportFragmentManager(), "Countries List Dialog");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM