如何在 Android 的 EditText 中檢查電子郵件地址有效性


此示例演示瞭如何在 Android 的 EditText 中檢查電子郵件地址有效性。

步驟 1 - 在 Android Studio 中建立一個新專案,轉到檔案 ⇒ 新建專案,並填寫所有必需的詳細資訊以建立新專案。

步驟 2 - 將以下程式碼新增到 res/layout/activity_main.xml 中。

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   android:id = "@+id/parent"
   xmlns:tools = "http://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity"
   android:gravity = "center"
   android:orientation = "vertical">
   <EditText
      android:id = "@+id/emailId"
      android:hint = "Enter Email id"
      android:layout_margin = "20dp"
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content" />
   <Button
      android:id = "@+id/text"
      android:textSize = "18sp"
      android:textAlignment = "center"
      android:layout_width = "wrap_content"
      android:textColor = "#000"
      android:text = "Check validation"
      android:layout_height = "wrap_content" />
</LinearLayout>

在上面的程式碼中,我們使用了 EditText 和 Button。當用戶點選按鈕時,它將檢查 EditText 的資料並驗證該資料。

步驟 3 - 將以下程式碼新增到 src/MainActivity.java 中

package com.example.andy.myapplication;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
   int view = R.layout.activity_main;
   Button button;
   EditText emailId;
   String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\.+[a-z]+";
   @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(view);
      button = findViewById(R.id.text);
      emailId = findViewById(R.id.emailId);
      button.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            if(emailId.getText().toString().isEmpty()) {
               Toast.makeText(getApplicationContext(),"enter email address",Toast.LENGTH_SHORT).show();
            }else {
               if (emailId.getText().toString().trim().matches(emailPattern)) {
                  Toast.makeText(getApplicationContext(),"valid email address",Toast.LENGTH_SHORT).show();
               } else {
                  Toast.makeText(getApplicationContext(),"Invalid email address", Toast.LENGTH_SHORT).show();
               }
            }
         }
      });
   }
}

在上面的程式碼中,我們正在驗證 EditText 資料,如下所示 -

String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\.+[a-z]+";

....................................

if(emailId.getText().toString().isEmpty()) {
   Toast.makeText(getApplicationContext(),"enter email address",Toast.LENGTH_SHORT).show();
}else {
   if (emailId.getText().toString().trim().matches(emailPattern)) {
      Toast.makeText(getApplicationContext(),"valid email address",Toast.LENGTH_SHORT).show();
   } else {
      Toast.makeText(getApplicationContext(),"Invalid email address", Toast.LENGTH_SHORT).show();
   }
}

讓我們嘗試執行您的應用程式。我假設您已將您的實際 Android 移動裝置連線到您的電腦。要從 Android Studio 執行應用程式,請開啟您的一個專案活動檔案,然後點選工具欄中的執行  圖示。選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕 -

在上述結果中,我們輸入了有效的電子郵件 ID 並點選了按鈕。它顯示了正確的驗證訊息,表明這是一個有效的電子郵件地址。現在輸入錯誤的電子郵件 ID 並點選按鈕。它將顯示如下所示的訊息 -

點選 此處 下載專案程式碼

更新於: 2019-07-30

3K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.