0

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

我尝试输入34enterPhoneNumber 字段,当我出于某种原因敲出它时,它没有显示错误。

有任何想法吗?

4

1 回答 1

0

因为“34”是 PhoneNumberUtils.isGlobalPhoneNumber 定义的全局号码,它反过来检查“34”是否与模式匹配:

private static final Pattern GLOBAL_PHONE_NUMBER_PATTERN =
            Pattern.compile("[\\+]?[0-9.-]+");
于 2016-02-10T19:59:08.053 回答