Giới thiệu
Trong màn hình đăng nhập, thường cần xác thực địa chỉ email người dùng nhập vào EditText. Ví dụ này minh họa cách kiểm tra định dạng email bằng Regular Expression (Regex).
Bước 1: Tạo dự án mới
Mở Android Studio, chọn File → New Project, điền thông tin cần thiết.
Bước 2: Thiết kế giao diện (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context=".MainActivity">
<EditText
android:id="@+id/email"
android:hint="Email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:layout_margin="16dp"/>
<Button
android:id="@+id/valid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kiểm tra"
android:layout_marginTop="16dp"/>
</LinearLayout>
Sử dụng android:inputType="textEmailAddress" để hiển thị bàn phím phù hợp.
Bước 3: Viết logic (src/MainActivity.java)
package com.example.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainActivity extends AppCompatActivity {
// Regex email đơn giản (RFC 5322 đơn giản hóa)
private static final String EMAIL_REGEX = "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText emailEditText = findViewById(R.id.email);
Button validButton = findViewById(R.id.valid);
// Compile pattern một lần để tái sử dụng
final Pattern pattern = Pattern.compile(EMAIL_REGEX);
validButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = emailEditText.getText().toString().trim();
if (email.isEmpty()) {
Toast.makeText(MainActivity.this, "Vui lòng nhập email", Toast.LENGTH_LONG).show();
return;
}
Matcher matcher = pattern.matcher(email);
if (matcher.matches()) {
Toast.makeText(MainActivity.this, "Email hợp lệ", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "Định dạng email không hợp lệ", Toast.LENGTH_LONG).show();
}
}
});
}
}
Chạy ứng dụng
Kết nối thiết bị Android và chạy ứng dụng.

Không nhập gì và nhấn Kiểm tra → "Vui lòng nhập email"

Nhập email sai định dạng → "Định dạng email không hợp lệ"

Nhập email đúng → "Email hợp lệ"
Cải tiến so với mã gốc:Lưu ý: Android cung cấp
- Dùng
matcher.matches()thay vìfind()để khớp toàn bộ chuỗi- Regex cập nhật:
{2,}cho phép TLD dài hơn 4 ký tự (ví dụ: .technology, .photography)- Compile
Patternmột lần ngoài listener để tối ưu- Thêm
trim()loại bỏ khoảng trắng đầu/cuối- Sử dụng
inputType="textEmailAddress"cho UX tốt hơnandroid.util.Patterns.EMAIL_ADDRESS(API 1+) có sẵn, có thể dùng thay regex tùy chỉnh.