我在布局 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");
}
}
}
});
我尝试输入34
enterPhoneNumber 字段,当我出于某种原因敲出它时,它没有显示错误。
有任何想法吗?