Android的电子邮件的EditText的验证电子邮件、Android、EditText

2023-09-05 06:50:59 作者:会呼吸的鱼

我有这样的EditText定义:

I have this EditText definition:

<EditText android:layout_height="wrap_content" android:layout_width="fill_parent"
    android:inputType="textEmailAddress" android:id="@+id/EmailText"></EditText>

注意的EditText有一个电子邮件地址规范中定义的inputType。 Android的是否有任何内置的验证电子邮件地址输入类型,或做这一切都需要手动完成?它让我输入无效数据,所以我很好奇,它的目的。

Notice the EditText has the inputType defined with an email address specification. Does Android have anything built in to validate an email address input type, or does this all have to be done manually? It's allowing me to enter invalid data, so I'm curious as to its purpose.

感谢。

推荐答案

下面通过给输入类型的电子邮件您正在设置的电子邮件类型的意思是@和键盘上的。关键字会显示在按键板。

Here By giving input type Email you are setting the keyboard of email type means "@" and "." keyword will display on key board.

更好的解决办法是通过下面的函数来比较的电子邮件

the better solution is to compare the email by following function

public boolean isEmailValid(String email)
    {
         String regExpn =
             "^(([\\w-]+\\.)+[\\w-]+|([a-zA-Z]{1}|[\\w-]{2,}))@"
                 +"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
                   +"[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\."
                   +"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
                   +"[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
                   +"([a-zA-Z]+[\\w-]+\\.)+[a-zA-Z]{2,4})$";

     CharSequence inputStr = email;

     pattern = Pattern.compile(regExpn,Pattern.CASE_INSENSITIVE);
     matcher = pattern.matcher(inputStr);

     if(matcher.matches())
        return true;
     else
        return false;
}

如果该函数返回true,那么你的电子邮件地址是有效的,否则不能

if this function returns true then your email address is valid otherwise not