代码之家  ›  专栏  ›  技术社区  ›  Jack

countrycodepicker是否支持输入电话号码

  •  0
  • Jack  · 技术社区  · 7 年前

    我在用 CountryCodePicker 在我的代码中,用户可以选择他所在的国家并输入他的电话号码,这个控件是否支持输入电话号码,或者我是否必须使用 EditText 视野?

    还有,说我要用 编辑文本 ,此控件是否支持检查电话号码的有效性?

        <com.hbb20.CountryCodePicker
            android:id="@+id/ccpicker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            app:ccp_defaultLanguage="ENGLISH"
            app:ccp_defaultPhoneCode="54"
            app:ccp_showNameCode="false"
            app:ccp_rememberLastSelection="true" />
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Jack    7 年前

    我找到了答案,希望它能帮助别人。

    countrycodepicker支持绑定到edittext视图以输入电话号码,并支持覆盖以检查电话号码的有效性。

    在XML布局中添加承运商编号的CCP视图和编辑文本

                  <LinearLayout
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:gravity="center_vertical">
    
                      <com.hbb20.CountryCodePicker
                          android:id="@+id/ccp"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          app:ccp_countryPreference="us,in"
                          />
    
                      <EditText
                          android:id="@+id/editText_carrierNumber"
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:editable="false"
                          android:hint="phone"
                          android:inputType="phone"
                          android:singleLine="true" />
                  </LinearLayout>
    

    在活动/片段中添加ccp对象

    CountryCodePicker ccp;
    EditText editTextCarrierNumber;
    

    从布局中绑定ccp和承运商编号edittext

    ccp = (CountryCodePicker) findViewById(R.id.ccp);
    editTextCarrierNumber = (EditText)findViewById(R.id.editText_carrierNumber);
    

    将carriernumber edittext附加到ccp。

    ccp.registerCarrierNumberEditText(editTextCarrierNumber);
    

    有效性更改侦听器将在每次输入的数字有效性更改时获得回调。

    ccp.setPhoneNumberValidityChangeListener(new CountryCodePicker.PhoneNumberValidityChangeListener() {
                @Override
                public void onValidityChanged(boolean isValidNumber) {
                   // your code
                }
            });
    

    参考文献: https://github.com/hbb20/CountryCodePickerProject/wiki/Full-Number-Support#3-number-validation