EditText 上的 Android 电子邮件验证

2023-03-25移动开发问题
7

本文介绍了EditText 上的 Android 电子邮件验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个编辑文本,我想在我的 Editttext 中编写电子邮件验证这是一个xml代码

I have one edittext and I would to to write email validation in my Editttext this is a xml code

<EditText
        android:id="@+id/mail"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_alignLeft="@+id/phone"
        android:layout_below="@+id/phone"
        android:layout_marginRight="33dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/edit_background"
        android:ems="10"
        android:hint="E-Mail"
        android:inputType="textEmailAddress"
        android:paddingLeft="20dp"
        android:textColor="#7e7e7e"
        android:textColorHint="#7e7e7e" >
</EditText>

这是一个java代码

emailInput = mail.getText().toString().trim();

emailPattern = "^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$";

if (emailInput.matches(emailPattern)) {
    Toast.makeText(getActivity(), "valid email address", Toast.LENGTH_SHORT).show();
} else {
    Toast.makeText(getActivity(), "Invalid email address", Toast.LENGTH_SHORT).show();
    mail.setBackgroundResource(R.drawable.edit_red_line);
}

我无法验证.Toast 消息始终是无效的电子邮件地址".我做错了什么?

I can't validation. The toast message is always "Invalid email address". What am I doing wrong?

推荐答案

为什么不用:

public final static boolean isValidEmail(CharSequence target) {
  return !TextUtils.isEmpty(target) && android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
}

按照建议这里.

这篇关于EditText 上的 Android 电子邮件验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何在 COCOS2d Android 中使用 CClistview?
How can I use CClistview in COCOS2d Android?(如何在 COCOS2d Android 中使用 CClistview?)...
2024-08-12 移动开发问题
5

cocos2d-android:如何显示分数
cocos2d-android: how to display score(cocos2d-android:如何显示分数)...
2024-08-11 移动开发问题
7

Sqlite 数据库未从资产文件夹 Android 复制
Sqlite database not copied from asset folder Android(Sqlite 数据库未从资产文件夹 Android 复制)...
2024-04-15 移动开发问题
8

SQLite 数据库副本在由设备而不是模拟器生成时出现损坏
SQLite Database Copy Appears Corrupted When Generated by Device and not Emulator(SQLite 数据库副本在由设备而不是模拟器生成时出现损坏)...
2024-04-15 移动开发问题
4

安卓文件拷贝
Android file copy(安卓文件拷贝)...
2024-04-15 移动开发问题
6

Android如何在android中检测Edittext的Copy事件
Android how to detect Copy event of Edittext in android(Android如何在android中检测Edittext的Copy事件)...
2024-04-15 移动开发问题
5