PhoneNumberUtils.isGlobalPhoneNumber没有返回正确的结果

时间:2016-02-10 19:23:46

标签: android validation phonenumberutils

我在layout 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");
                    }
                }
            }
        });

我尝试在enterPhoneNumber字段中输入34,当我因为某些原因点击它时,它没有显示错误。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

因为“34”是PhoneNumberUtils.isGlobalPhoneNumber定义的全局号码,而后者又检查“34”是否与模式匹配:

private static final Pattern GLOBAL_PHONE_NUMBER_PATTERN =
            Pattern.compile("[\\+]?[0-9.-]+");