2016-08-09 15 views
2

enter image description hereAndroid携帯キャリア入力欄

このような携帯電話入力を作成したいと思います。国の選択、キャリアコード、電話番号が含まれています。私は国のセレクターと電話番号を作成しました。

  <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:gravity="center" 
      app:layout_marginTopPercent="65%" 
      app:layout_widthPercent="70%"> 

      <com.hbb20.CountryCodePicker 
       android:id="@+id/country_code" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       app:contentColor="@color/white" 
       app:defaultNameCode="IL" 
       app:hideNameCode="true" 
       app:textSize="24sp" /> 

      <EditText 
       android:id="@+id/et_number" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:digits="1234567890" 
       android:inputType="number" 
       android:maxLength="15" 
       android:textColor="@color/white" 
       android:textSize="24sp" /> 
     </LinearLayout> 

キャリアコードのEditTextフィールドを作成するにはどうすればよいですか?選択した国の携帯電話番号を認識する必要があります。たとえば、イスラエル(+ 972)を選択して50を入力する場合、フォーカスはid = et_numberのEditTextに切り替える必要があります。

答えて

0

私はこの方法

がここにここに私のレイアウト(PercentRelativeLayoutの内側に置く)

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:gravity="center" 
    app:layout_marginTopPercent="67%" 
    app:layout_widthPercent="80%"> 

    <com.hbb20.CountryCodePicker 
     android:id="@+id/country_code" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:contentColor="@color/white" 
     app:defaultNameCode="IL" 
     app:hideNameCode="true" 
     app:textSize="24sp" /> 

    <EditText 
     android:id="@+id/et_carrier" 
     android:layout_width="60dp" 
     android:layout_height="wrap_content" 
     android:digits="1234567890" 
     android:inputType="number" 
     android:maxLength="3" 
     android:textColor="@color/white" 
     android:textSize="24sp" /> 

    <EditText 
     android:id="@+id/et_number" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:imeOptions="actionDone" 
     android:digits="1234567890" 
     android:inputType="number" 
     android:maxLength="10" 
     android:textColor="@color/white" 
     android:textSize="24sp" /> 
</LinearLayout> 

はそれが

country_code = (CountryCodePicker) findViewById(R.id.country_code); 
et_carrier = (EditText) findViewById(R.id.et_carrier); 
et_number = (EditText) findViewById(R.id.et_number); 

// Get country code from sim 
defaultRegion = PhoneNumberUtils.getDefaultCountryIso(getApplicationContext()); 

// Set country code to spinner 
country_code.setCountryForNameCode(defaultRegion); 

// set white underline in number edit text 
et_number.getBackground().mutate().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP); 
et_carrier.getBackground().mutate().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP); 

// jump to number edit when carrier code is filled 
et_carrier.addTextChangedListener(new TextWatcher() { 
    @Override 
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 

    } 

    @Override 
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 

    } 

    @Override 
    public void afterTextChanged(Editable editable) { 
    if (editable.length() == 3) { 
     et_number.requestFocus(); 
     et_number.setSelection(et_number.getText().length()); 
    } 

    } 
}); 

// jump to carrier edit when number is deleted 
et_number.addTextChangedListener(new TextWatcher() { 
@Override 
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 

} 

@Override 
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 
    if (TextUtils.isEmpty(et_number.getText())) { 
     et_carrier.requestFocus(); 
     et_carrier.setSelection(et_carrier.getText().length()); 
} 

} 

@Override 
public void afterTextChanged(Editable editable) { 
} 
}); 

そして、ここでは読み、事前にする機能ですどのように動作するかですそれをやりましたSIMからの携帯番号の入力

/** 
* Try to prefill SIM mobile number. 
* If permissions are not granted, request them. 
*/ 
private void tryAndPrefillPhoneNumber() { 
    if (checkCallingOrSelfPermission(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) { 
     TelephonyManager manager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
     et_carrier.setText(formatPhoneNumber(manager.getLine1Number(), defaultRegion, "carrier")); 
     et_number.setText(formatPhoneNumber(manager.getLine1Number(), defaultRegion, "number")); 

    } else { 
     ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, 0); 
    } 
} 

/** 
* Try to prefill number, or show explanation to user 
* why permissions need to be granted 
* 
* @param requestCode 
* @param permissions 
* @param grantResults 
*/ 
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
     tryAndPrefillPhoneNumber(); 
    } else { 
     if (ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[0])) { 

      message = getString(R.string.permission_phone_number_text); 

      showToast(message); 
     } 
    } 
} 

private String formatPhoneNumber(String phoneString, String countryIso, String type) { 

    try { 
     // information needed to search for phone phone using google lib 
     PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); 
     Phonenumber.PhoneNumber phoneNumber = phoneUtil.parse(phoneString, countryIso); 
     int carrierLength = phoneUtil.getLengthOfNationalDestinationCode(phoneNumber); 

     if (type != null) { 
      if (type.equals("local")) { 
       // parse the phone number without country code 
       return String.valueOf(phoneNumber.getNationalNumber()); 

      } else if (type.equals("carrier")) { 
       // parse mobile carrier number 
       return String.valueOf(phoneNumber.getNationalNumber()).substring(0, carrierLength); 

      } else if (type.equals("number")) { 
       // parse phone number without country code and carrier number 
       return String.valueOf(phoneNumber.getNationalNumber()).substring(carrierLength); 

      } else { 
       //parse full number 
       return phoneUtil.format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL); 
      } 
     } 

    } catch (NumberParseException e) { 
     e.printStackTrace(); 
    } 

    // if not phone number found return 
    return null; 
}