慕姐8265434
要执行电子邮件验证,我们有很多方法,但是最简单,最简单的方法是两种方法。1-使用EditText(....).addTextChangedListener它持续在EditText boxie email_id 中的每个输入上触发都是无效或有效的/** * Email Validation ex:- tech@end.com*/final EditText emailValidate = (EditText)findViewById(R.id.textMessage); final TextView textView = (TextView)findViewById(R.id.text); String email = emailValidate.getText().toString().trim();String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";emailValidate .addTextChangedListener(new TextWatcher() { public void afterTextChanged(Editable s) { if (email.matches(emailPattern) && s.length() > 0) { Toast.makeText(getApplicationContext(),"valid email address",Toast.LENGTH_SHORT).show(); // or textView.setText("valid email"); } else { Toast.makeText(getApplicationContext(),"Invalid email address",Toast.LENGTH_SHORT).show(); //or textView.setText("invalid email"); } } public void beforeTextChanged(CharSequence s, int start, int count, int after) { // other stuffs } public void onTextChanged(CharSequence s, int start, int before, int count) { // other stuffs } }); 2- 最简单的使用if-else条件的方法。使用getText()获取EditText框字符串,然后与为电子邮件提供的模式进行比较。如果模式不匹配或无法匹配,请单击按钮的onTo消息。它不会在EditText框中每次输入字符时触发。简单示例如下所示。final EditText emailValidate = (EditText)findViewById(R.id.textMessage); final TextView textView = (TextView)findViewById(R.id.text); String email = emailValidate.getText().toString().trim();String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";// onClick of button perform this simplest code.if (email.matches(emailPattern)){Toast.makeText(getApplicationContext(),"valid email address",Toast.LENGTH_SHORT).show();}else {Toast.makeText(getApplicationContext(),"Invalid email address", Toast.LENGTH_SHORT).show();}