📜  在Android中实施电子邮件验证程序

📅  最后修改于: 2021-05-13 15:42:50             🧑  作者: Mango

先决条件: Android中带有示例的Java的EditText小部件

需要电子邮件验证以通知用户用户输入的电子邮件格式无效。如果用户未以错误的格式输入电子邮件,则将为EditText给出错误字段。在本文中,已逐步讨论了如何在Android中实现电子邮件验证程序。请看以下图片,以了解本文讨论的内容。注意,我们将使用Java语言实现该项目。

在Android中实施电子邮件验证程序

在Android中实现电子邮件验证程序的步骤

步骤1:创建一个空的活动项目

  • 创建一个空的活动Android Studio项目。选择Java作为编程语言。
  • 要了解如何创建一个空的活动Android Studio项目,请参阅Android |如何在Android Studio中创建/启动新项目?

步骤2:使用activity_main.xml文件

  • 在应用程序的主布局中,仅实现了两个小部件。一个是“电子邮件编辑文本”字段,另一个是按钮,单击该按钮时将检查输入的电子邮件。
  • activity_main.xml文件中调用以下代码以实现UI布局。
XML


  
    
    
  
    
    


Java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
  
public class MainActivity extends AppCompatActivity {
  
    // EditText filed for Email
    EditText etMail;
  
    // Button to validate the Email address
    Button bValidate;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // register all the UI elements 
          // with their appropriate IDs
        etMail = findViewById(R.id.emailField);
        bValidate = findViewById(R.id.validateButton);
  
        // handle the VALIDATE button to show the toast
          // message whether the entered email is valid or not
        bValidate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                emailValidator(etMail);
            }
        });
    }
  
    // the function which triggered when the VALIDATE button is clicked
    // which validates the email address entered by the user
    public void emailValidator(EditText etMail) {
  
        // extract the entered data from the EditText
        String emailToText = etMail.getText().toString();
  
        // Android offers the inbuilt patterns which the entered 
          // data from the EditText field needs to be compared with
        // In this case the the entered data needs to compared with
          // the EMAIL_ADDRESS, which is implemented same below
        if (!emailToText.isEmpty() && Patterns.EMAIL_ADDRESS.matcher(emailToText).matches()) {
            Toast.makeText(this, "Email Verified !", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "Enter valid Email address !", Toast.LENGTH_SHORT).show();
        }
    }
}


输出界面:

在Android中实施电子邮件验证程序

步骤3:使用MainActivity。 Java文件

  • 在这种情况下,模式EMAIL_ADDRESS用于演示。
  • 但是,还有5种模式可以验证用户的输入。那些是:
  • 有关Android中预定义模式的更多信息,请参考模式。
  • 在MainActivity中调用以下代码。在这种情况下,用于实现电子邮件验证程序的Java文件。添加了注释以便更好地理解。

Java

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
  
public class MainActivity extends AppCompatActivity {
  
    // EditText filed for Email
    EditText etMail;
  
    // Button to validate the Email address
    Button bValidate;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // register all the UI elements 
          // with their appropriate IDs
        etMail = findViewById(R.id.emailField);
        bValidate = findViewById(R.id.validateButton);
  
        // handle the VALIDATE button to show the toast
          // message whether the entered email is valid or not
        bValidate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                emailValidator(etMail);
            }
        });
    }
  
    // the function which triggered when the VALIDATE button is clicked
    // which validates the email address entered by the user
    public void emailValidator(EditText etMail) {
  
        // extract the entered data from the EditText
        String emailToText = etMail.getText().toString();
  
        // Android offers the inbuilt patterns which the entered 
          // data from the EditText field needs to be compared with
        // In this case the the entered data needs to compared with
          // the EMAIL_ADDRESS, which is implemented same below
        if (!emailToText.isEmpty() && Patterns.EMAIL_ADDRESS.matcher(emailToText).matches()) {
            Toast.makeText(this, "Email Verified !", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "Enter valid Email address !", Toast.LENGTH_SHORT).show();
        }
    }
}

输出:在模拟器上运行

想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处,前往由我们的专家精心策划的指南,以使您立即做好行业准备!