我在我的代码中使用了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" />
发布于 2018-06-10 00:50:50
我找到了答案,我希望它能帮助其他人。
CountryCodePicker支持绑定到EditText视图以输入电话号码,并支持覆盖以检查电话号码的有效性。
将运营商编号的CCP视图和editText添加到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>
在活动/片段中添加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);
有效性更改监听器将在每次输入号码的有效性更改时获取callBack。
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
https://stackoverflow.com/questions/50705125
复制相似问题