android edittext email,Android上EditText上的电子邮件地址验证

1cb930fcda8428d79463661bb7713df4.png

慕姐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();}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值