从 edittext 识别电子邮件 Android

identifying Email from edittext Android

我想要一种使用我的用户名或电子邮件登录我的 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");
                        }

                    }
                });
            }

        }
    }
}

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

这并不复杂,您可以尝试 this,如果电子邮件只包含低位大写字母、@ 和“.”,它会告诉您正确。

这并不难,您应该使用 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 及更高版本的任何应用程序中使用它,从来没有遇到任何问题。

使用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   
        }
    }
});

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