2016-08-09 62 views
2

enter image description hereMobilfunk- Eingabefeld auf Android

Ich möchte Handy-Input wie diese erstellen. Es enthält Länderauswahl, Mobilfunknummer und Telefonnummer. Ich habe Länderauswahl und Telefonnummer erstellt.

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

Wie EditText Feld für Trägercode zu machen? Er muss die Nummer des ausgewählten Landes erkennen. Wenn der Benutzer beispielsweise Israel (+972) auswählt und dann 50 eingibt, muss der Fokus auf EditText mit id = et_number umgeschaltet werden.

Antwort

0

Ich tat es auf diese Weise

Hier ist mein Layout (innen PercentRelativeLayout setzen)

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

Hier ist, wie es

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

arbeitet Und hier ist eine Funktion und Pre lesen Handy-Nummer von 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; 
}