Java 从edittext Android识别电子邮件

Java 从edittext Android识别电子邮件,java,android,parse-platform,android-edittext,Java,Android,Parse Platform,Android Edittext,我想要一个方法来登录到我的解析帐户与我的用户名或我的电子邮件。 我现在拥有的代码只适用于我的用户名。我想知道一种识别文本字段是否有用户名或电子邮件的方法 这是密码 private class SignInOnClickListener implements View.OnClickListener { @Override public void onClick(View v) { // Get the username and password from the

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

这是密码

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");
                        }

                    }
                });
            }

        }
    }
}

这是为了验证电子邮件地址。那篇文章建议你只需试着向这个地址发送一封电子邮件,这并不完全适合你的使用情况,但要按照这些思路思考。可能会做一个粗略的验证,比如测试字段是否包含
@
,如果包含,则尝试将其用作电子邮件。

这并不复杂,您可以尝试,如果电子邮件只包含低容量字母,@和“.”,它会为您提供真实信息.

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

使用。它小巧、简洁,可以胜任这项工作

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

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

检查emailid或用户名和呼叫验证方法是否与我尝试过的方法一致。仍然拒绝将身份识别为电子邮件如果(!username_email.matches(“@”){//HERE!'请尝试
if(username_email.indexOf('@')!=-1)…
@Chehanr测试失败的方式不多。你确定是测试失败了吗?你是否添加了日志语句来确定正在执行的代码?它确实会检测文本字段何时包含电子邮件!但仍然拒绝登录我的解析帐户。`08-13 12:54:07.102 17634-17634/com.Chehanr.sam光电探测器﹕ username\u email检测为email`这是他最初使用的条件。我开始怀疑问题不在条件上。@AritraRoy从哪里复制的?
android.util.Patterns.email\u ADDRESS.matcher(email.matches());
你自己写的吗?@Rustam是的,我在我的应用程序中使用了很长一段时间。但是如果你不想相信,我不会尝试。你可以想什么就想什么。谢谢。