【问题标题】:identifying Email from edittext Android从edittext Android识别电子邮件
【发布时间】:2015-11-06 00:33:57
【问题描述】:

我想要一种使用我的用户名或电子邮件登录我的 Parse 帐户的方法。 我现在拥有的代码仅适用于我的用户名。我想知道一种方法来识别文本字段是否有用户名或电子邮件。

这里是代码,

private class SignInOnClickListener implements View.OnClickListener {

    @Override
    public void onClick(View v) {
        // Get the username and password from the view
        final String username_email = mUsernameEmailEtxt.getText().toString().toLowerCase();
        final String password = mPasswordEtxt.getText().toString();

        if (isFormInputValid(username_email, password)) {
            if (android.util.Patterns.EMAIL_ADDRESS.matcher(username_email).matches()) { // HERE!
                final String email = mUsernameEmailEtxt.getText().toString();
                ParseUser.logInInBackground(email, password, new LogInCallback() {
                    public void done(ParseUser user, ParseException e) {
                        if (user != null) {
                            // Hooray! The user is logged in.
                            Intent intent = new Intent(getBaseContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                            finish();
                        }
                    }
                });
            } else {
                ParseUser.logInInBackground(username_email, password, new LogInCallback() {

                    public void done(ParseUser user, ParseException e) {
                        if (user != null) {
                            // Hooray! The user is logged in.
                            Intent intent = new Intent(getBaseContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                            finish();
                        } else {
                            findViewById(R.id.error).setVisibility(View.VISIBLE);
                        }
                    }
                });
            }
        }
    }
}

private class SignInOnClickListener implements View.OnClickListener {

    @Override
    public void onClick(View v) {
        // Get the username and password from the view
        final String username_email = mUsernameEmailEtxt.getText().toString().toLowerCase();
        final String password = mPasswordEtxt.getText().toString();

        if (isFormInputValid(username_email, password)) {
            if (android.util.Patterns.EMAIL_ADDRESS.matcher(username_email).matches()) { // HERE!
                final String email = mUsernameEmailEtxt.getText().toString();
                ParseUser.logInInBackground(email, password, new LogInCallback() {
                    public void done(ParseUser user, ParseException e) {
                        if (user != null) {
                            // Hooray! The user is logged in.
                            Intent intent = new Intent(getBaseContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                            finish();
                        }
                    }
                });
            } else {
                ParseUser.logInInBackground(username_email, password, new LogInCallback() {

                    public void done(ParseUser user, ParseException e) {
                        if (user != null) {
                            // Hooray! The user is logged in.
                            Intent intent = new Intent(getBaseContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                            finish();
                        } else {
                            findViewById(R.id.error).setVisibility(View.VISIBLE);
                        }
                    }
                });
            }
        }
    }
}

更新:我已经发布了日志并进行了一些更改(学分:@kevin),检测到电子邮件但拒绝使用它登录。

这是代码,

 private class SignInOnClickListener implements View.OnClickListener {

    @Override
    public void onClick(View v) {
        // Get the username and password from the view
        final String username_email = mUsernameEmailEtxt.getText().toString().toLowerCase();
        final String password = mPasswordEtxt.getText().toString();
        final String email = mUsernameEmailEtxt.getText().toString().toLowerCase();

        if (isFormInputValid(username_email, password)) {
            if (username_email.indexOf('@') != -1) { // HERE!
                Log.d("detector", "username_email detected as email:" + email.toString());
                ParseUser.logInInBackground(email, password, new LogInCallback() {
                    public void done(ParseUser user, ParseException e) {
                        if (user != null) {
                            // Hooray! The user is logged in.
                            Intent intent = new Intent(getBaseContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                            finish();
                        }
                    }
                });
            } else {
                Log.d("detector", "username_email detected as username:" + username_email.toString());
                ParseUser.logInInBackground(username_email, password, new LogInCallback() {
                    public void done(ParseUser user, ParseException e) {
                        if (user != null) {
                            // Hooray! The user is logged in.
                            Intent intent = new Intent(getBaseContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                            finish();
                        } else {
                            findViewById(R.id.error).setVisibility(View.VISIBLE);
                            Log.d("error", "username or email invalid");
                        }

                    }
                });
            }

        }
    }
}

【问题讨论】:

  • 检查 emailid 或 username 并相应地调用 verify 方法

标签: java android parse-platform android-edittext


【解决方案1】:

这并不难做到,您应该使用 Android 中已经提供的内置模式来完美地检查电子邮件 ID。

public final static boolean isEmailIDValid(CharSequence email) {
    if (email == null) 
        return false;

    return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}

不太复杂,并且可以在 API 8 以上的所有 Android 版本上完美运行。我一直在支持 API 14 及以上的任何应用程序中使用它,从未遇到任何问题。

【讨论】:

  • 这是他最初使用的条件。我开始怀疑问题不在于条件。
  • @AritraRoy 你从哪里复制的? android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();是你自己写的吗?
  • @Rustam 是的,我在我的应用程序中使用了很长时间。但是,如果您不打算相信,我不会尝试。你可以想你想要的。谢谢。
【解决方案2】:

验证电子邮件地址是 extremely complicated。那篇文章建议您只需尝试向该地址发送一封电子邮件,这并不完全适合您的用例,但请按照这些思路进行思考。也许做一个粗略的验证,比如测试该字段是否包含@,如果有,请尝试将其用作电子邮件。

【讨论】:

  • 我试过了。仍然拒绝识别为电子邮件。 ' if (!username_email.matches("@")) { // 这里!'
  • 试试if(username_email.indexOf('@') != -1) ...
  • @Chehanr 测试失败的方式并不多。你确定是测试失败了吗?您是否添加了日志语句以确定正在执行的代码?
  • 好吧,它确实会检测到文本字段何时包含电子邮件!但仍然拒绝登录我的解析帐户。 ` 08-13 12:54:07.102 17634-17634/com.chehanr.sample D/detector: username_email 检测为电子邮件`
【解决方案3】:

这并不复杂,你可以试试this,如果电子邮件只包含低大写字母、@ 和“.”,它就会给你正确的答案。

【讨论】:

    【解决方案4】:

    使用 Apache Validation Library 。它小巧、简洁且能胜任。

    userEditText.addTextChangedListener(new TextWatcher() {
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
        }
    
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            textView.setVisibility(View.VISIBLE);
        }
    
        public void afterTextChanged(Editable s) {
            EmailValidator emailValidator = EmailValidator.getInstance();
            if (!emailValidator.isValid(s.toString())) {
              //Is Email
            } else {
             //Is username   
            }
        }
    });
    

    注意:希望您已经考虑了边缘情况,因为根据您的要求,您的用户名也可以是电子邮件

    【讨论】:

      猜你喜欢
      • 2013-11-25
      • 1970-01-01
      • 2012-03-10
      • 2014-09-18
      • 1970-01-01
      • 1970-01-01
      • 2021-04-14
      • 2021-12-21
      • 1970-01-01
      相关资源
      最近更新 更多