android - 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" />
解决方案
我找到了答案,希望对其他人有所帮助。
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
推荐阅读
- r - 根据类型有效替换大型数据集中的负值
- python - 如何在 CKAN 扩展中自定义 authz.py 文件?
- hpcc - 如何在 HPCC / ECL 中将 SET 转换为 DATASET?
- java - 无法使用弹簧执行器 - 创建 bean CacheMetricsRegistrarConfiguration 时出错
- javascript - 获取匹配数组元素的总值
- java - 这是否可以从单个 JVM 运行独立的 Java REPL?
- javascript - 锚标签在页面下方起作用,但不向上
- sql - 执行此 sql 后不再从套接字读取数据
- autocad-plugin - 在 AutoLISP 中列出并提示特定对象实体
- mysql - 如何以与 Hibernate 模型中指定的顺序相同的顺序组织数据库列?