>我在布局xml中定义了一个EditText,如下所示:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="@+id/enter_phone_field"
android:layout_below="@id/enter_name_field"
android:layout_alignRight="@id/destination_value_label"
android:layout_alignEnd="@id/destination_value_label" />
我已经实现了它的OnFocusChangeListener,如下所示:
enterPhoneNumber = (EditText)findViewById(R.id.enter_phone_field);
// phone number lost focus listener
enterPhoneNumber.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus){
if (PhoneNumberUtils.isGlobalPhoneNumber(enterPhoneNumber.getText().toString())){
//valid phone number detected
Log.i("PhoneNumberValidated", enterPhoneNumber.getText().toString());
}
else {
enterPhoneNumber.setError("Please enter valid phone number");
}
}
}
});
我尝试在enter电话号码字段中输入34
,当我由于某些原因点击它时,它没有显示错误。
有什么想法吗?
因为根据PhoneNumberUtils.isGlobalPhoneNumber的定义,"34"是全局数字,它反过来检查"34"是否与模式匹配:
private static final Pattern GLOBAL_PHONE_NUMBER_PATTERN =
Pattern.compile("[\+]?[0-9.-]+");