本文介紹了EditText 上的 Android 電子郵件驗證的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我有一個編輯文本,我想在我的 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 電子郵件驗證的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!