2016-11-11 60 views
0

我正在開發Android應用程序中的註冊活動。我想顯示國家的電話代碼(從設備獲取國家)。它可以顯示包含國旗,國家電話代碼,國名的國家列表。我從https://github.com/mukeshsolanki/country-picker-android得到國家代碼選擇器的代碼。它包含完整的代碼。我想在手機中設置默認國家。國家代碼不是從Android中的國家/地區代碼選取器中獲取的

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

當我使用此代碼時,我得到了國家代碼「in」。但我想顯示電話代碼和標誌,名稱。當我打開屏幕。我想自動顯示它。

我的代碼如下所示

main.xml中

<?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> 

Main.Activity

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

+0

看看這個鏈接https://github.com/dlukashev/android-phone-number-with-flags。這可能會幫助你。 –

+0

這可能是一個很好的解決方案:https://github.com/joielechong/CountryCodePicker/ –

回答

0

試圖讓國家代碼使用緯度,長

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); 
    } 
0

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