首页 > 解决方案 > CountryCodePicker 是否支持输入电话号码

问题描述

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

另外,说我必须使用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" />

标签: android

解决方案


我找到了答案,希望对其他人有所帮助。

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

将运营商编号的 CCP 视图和编辑文本添加到 XML 布局

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

在 Activity / Fragment 中添加 CCP 对象

CountryCodePicker ccp;
EditText editTextCarrierNumber;

从布局中绑定 CCP 和 Carrier Number editText

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

将 CarrierNumber editText 附加到 CCP。

ccp.registerCarrierNumberEditText(editTextCarrierNumber);

每次输入号码的有效性更改时,Validity Change Listener 都会收到回调。

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


推荐阅读